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:
55
proxy/shadowsocks/ota.go
Normal file
55
proxy/shadowsocks/ota.go
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user