1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-10-12 12:04:13 -04:00

simplify crypto related code

This commit is contained in:
Darien Raymond
2018-09-12 15:43:36 +02:00
parent 09b601528b
commit 81c9968188
3 changed files with 18 additions and 22 deletions

View File

@@ -1,8 +1,6 @@
package encoding
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"hash/fnv"
@@ -105,10 +103,8 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
common.Must(buffer.AppendSupplier(serial.WriteHash(fnv1a)))
}
timestampHash := md5.New()
common.Must2(timestampHash.Write(hashTimestamp(timestamp)))
iv := timestampHash.Sum(nil)
aesStream := crypto.NewAesEncryptionStream(account.ID.CmdKey(), iv)
iv := md5.Sum(hashTimestamp(timestamp))
aesStream := crypto.NewAesEncryptionStream(account.ID.CmdKey(), iv[:])
aesStream.XORKeyStream(buffer.Bytes(), buffer.Bytes())
common.Must2(writer.Write(buffer.Bytes()))
return nil
@@ -153,9 +149,7 @@ func (c *ClientSession) EncodeRequestBody(request *protocol.RequestHeader, write
return &buf.SequentialWriter{Writer: cryptionWriter}
case protocol.SecurityType_AES128_GCM:
block, _ := aes.NewCipher(c.requestBodyKey[:])
aead, _ := cipher.NewGCM(block)
aead := crypto.NewAesGcm(c.requestBodyKey[:])
auth := &crypto.AEADAuthenticator{
AEAD: aead,
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
@@ -163,7 +157,8 @@ func (c *ClientSession) EncodeRequestBody(request *protocol.RequestHeader, write
}
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType(), padding)
case protocol.SecurityType_CHACHA20_POLY1305:
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(c.requestBodyKey[:]))
aead, err := chacha20poly1305.New(GenerateChacha20Poly1305Key(c.requestBodyKey[:]))
common.Must(err)
auth := &crypto.AEADAuthenticator{
AEAD: aead,
@@ -250,8 +245,7 @@ func (c *ClientSession) DecodeResponseBody(request *protocol.RequestHeader, read
return buf.NewReader(c.responseReader)
case protocol.SecurityType_AES128_GCM:
block, _ := aes.NewCipher(c.responseBodyKey[:])
aead, _ := cipher.NewGCM(block)
aead := crypto.NewAesGcm(c.responseBodyKey[:])
auth := &crypto.AEADAuthenticator{
AEAD: aead,