1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-20 04:04:15 -04:00
v2fly/proxy/shadowsocks/ota.go
2016-01-29 15:43:45 +00:00

56 lines
1.0 KiB
Go

package shadowsocks
import (
"crypto/hmac"
"crypto/sha1"
"github.com/v2ray/v2ray-core/common/serial"
)
const (
AuthSize = 10
)
type KeyGenerator func() []byte
type Authenticator struct {
key KeyGenerator
}
func NewAuthenticator(keygen KeyGenerator) *Authenticator {
return &Authenticator{
key: keygen,
}
}
func (this *Authenticator) AuthSize() int {
return AuthSize
}
func (this *Authenticator) Authenticate(auth []byte, data []byte) []byte {
hasher := hmac.New(sha1.New, this.key())
hasher.Write(data)
res := hasher.Sum(nil)
return append(auth, res[:AuthSize]...)
}
func HeaderKeyGenerator(key []byte, iv []byte) func() []byte {
return func() []byte {
newKey := make([]byte, 0, len(key)+len(iv))
newKey = append(newKey, key...)
newKey = append(newKey, iv...)
return newKey
}
}
func ChunkKeyGenerator(iv []byte) func() []byte {
chunkId := 0
return func() []byte {
newKey := make([]byte, 0, len(iv)+4)
newKey = append(newKey, iv...)
newKey = append(newKey, serial.IntLiteral(chunkId).Bytes()...)
chunkId++
return newKey
}
}