1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-30 07:46:41 -04:00
v2fly/common/crypto/chacha20.go

22 lines
471 B
Go
Raw Normal View History

2016-02-23 11:33:54 -05:00
package crypto
import (
"crypto/cipher"
"github.com/aead/chacha20"
2016-02-23 11:33:54 -05:00
)
2016-07-23 07:04:44 -04:00
// NewChaCha20Stream creates a new Chacha/20 cipher stream. Caller must ensure that key is 32-bytes long and iv is either 8 or 12 bytes.
func NewChaCha20Stream(key []byte, iv []byte) cipher.Stream {
var keyArray [32]byte
var nonce [12]byte
copy(keyArray[:], key)
switch len(iv) {
case 8:
copy(nonce[4:], iv)
case 12:
copy(nonce[:], iv)
}
2016-07-23 07:04:44 -04:00
return chacha20.NewCipher(&nonce, &keyArray)
2016-02-23 11:33:54 -05:00
}