-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.go
38 lines (31 loc) · 1.12 KB
/
aes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
}