mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 17:38:41 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
40
common/crypto/aes.go
Normal file
40
common/crypto/aes.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
|
||||
"github.com/xtls/xray-core/v1/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)
|
||||
}
|
||||
|
||||
// NewAesCTRStream creates a stream cipher based on AES-CTR.
|
||||
func NewAesCTRStream(key []byte, iv []byte) cipher.Stream {
|
||||
return NewAesStreamMethod(key, iv, cipher.NewCTR)
|
||||
}
|
||||
|
||||
// NewAesGcm creates a AEAD cipher based on AES-GCM.
|
||||
func NewAesGcm(key []byte) cipher.AEAD {
|
||||
block, err := aes.NewCipher(key)
|
||||
common.Must(err)
|
||||
aead, err := cipher.NewGCM(block)
|
||||
common.Must(err)
|
||||
return aead
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue