1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 18:00:43 +00:00
This commit is contained in:
Darien Raymond 2016-12-13 09:32:29 +01:00
parent 76776ddf99
commit a9790d59f0
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 10 additions and 0 deletions

View File

@ -4,32 +4,39 @@ import (
"hash/fnv"
"crypto/md5"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/serial"
)
// Authenticate authenticates a byte array using Fnv hash.
func Authenticate(b []byte) uint32 {
fnv1hash := fnv.New32a()
fnv1hash.Write(b)
return fnv1hash.Sum32()
}
// FnvAuthenticator is an AEAD based on Fnv hash.
type FnvAuthenticator struct {
}
// NonceSize implements AEAD.NonceSize().
func (v *FnvAuthenticator) NonceSize() int {
return 0
}
// Overhead impelements AEAD.Overhead().
func (v *FnvAuthenticator) Overhead() int {
return 4
}
// Seal implements AEAD.Seal().
func (v *FnvAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
dst = serial.Uint32ToBytes(Authenticate(plaintext), dst)
return append(dst, plaintext...)
}
// Open implements AEAD.Open().
func (v *FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
if serial.BytesToUint32(ciphertext[:4]) != Authenticate(ciphertext[4:]) {
return dst, crypto.ErrAuthenticationFailed
@ -37,6 +44,7 @@ func (v *FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) (
return append(dst, ciphertext[4:]...), nil
}
// GenerateChacha20Poly1305Key generates a 32-byte key from a given 16-byte array.
func GenerateChacha20Poly1305Key(b []byte) []byte {
key := make([]byte, 32)
t := md5.Sum(b)

View File

@ -30,6 +30,7 @@ func hashTimestamp(t protocol.Timestamp) []byte {
return bytes
}
// ClientSession stores connection session info for VMess client.
type ClientSession struct {
requestBodyKey []byte
requestBodyIV []byte
@ -40,6 +41,7 @@ type ClientSession struct {
idHash protocol.IDHash
}
// NewClientSession creates a new ClientSession.
func NewClientSession(idHash protocol.IDHash) *ClientSession {
randomBytes := make([]byte, 33) // 16 + 16 + 1
rand.Read(randomBytes)