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.
v2ray-core/common/crypto/chacha20.go

22 lines
471 B

package crypto
import (
"crypto/cipher"
"github.com/aead/chacha20"
)
// 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)
}
return chacha20.NewCipher(&nonce, &keyArray)
}