mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.0 KiB
56 lines
1.0 KiB
9 years ago
|
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
|
||
|
}
|
||
|
}
|