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.
39 lines
1.1 KiB
39 lines
1.1 KiB
package crypto
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
|
|
"v2ray.com/core/common"
|
|
)
|
|
|
|
// NewAesDecryptionStream creates a new AES encryption stream based on given key and IV.
|
|
// Caller must ensure the length of key and IV is either 16, 24 or 32 bytes.
|
|
func NewAesDecryptionStream(key []byte, iv []byte) cipher.Stream {
|
|
return NewAesStreamMethod(key, iv, cipher.NewCFBDecrypter)
|
|
}
|
|
|
|
// NewAesEncryptionStream creates a new AES description stream based on given key and IV.
|
|
// Caller must ensure the length of key and IV is either 16, 24 or 32 bytes.
|
|
func NewAesEncryptionStream(key []byte, iv []byte) cipher.Stream {
|
|
return NewAesStreamMethod(key, iv, cipher.NewCFBEncrypter)
|
|
}
|
|
|
|
func NewAesStreamMethod(key []byte, iv []byte, f func(cipher.Block, []byte) cipher.Stream) cipher.Stream {
|
|
aesBlock, err := aes.NewCipher(key)
|
|
common.Must(err)
|
|
return f(aesBlock, iv)
|
|
}
|
|
|
|
func NewAesCTRStream(key []byte, iv []byte) cipher.Stream {
|
|
return NewAesStreamMethod(key, iv, cipher.NewCTR)
|
|
}
|
|
|
|
func NewAesGcm(key []byte) cipher.AEAD {
|
|
block, err := aes.NewCipher(key)
|
|
common.Must(err)
|
|
aead, err := cipher.NewGCM(block)
|
|
common.Must(err)
|
|
return aead
|
|
}
|