1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-06 17:25:27 -05:00

complete implementation of shadowsocks ota

This commit is contained in:
Darien Raymond
2016-01-29 15:43:45 +00:00
parent 7f5184e943
commit 57ff7ba923
6 changed files with 134 additions and 24 deletions

55
proxy/shadowsocks/ota.go Normal file
View File

@@ -0,0 +1,55 @@
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
}
}