mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-11-10 23:03:39 +00:00
refine chacha20 initialization
This commit is contained in:
parent
128e866021
commit
e304e2761d
@ -6,24 +6,16 @@ import (
|
|||||||
"github.com/aead/chacha20"
|
"github.com/aead/chacha20"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeNonce(nonce *[chacha20.NonceSize]byte, iv []byte) {
|
// 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) {
|
switch len(iv) {
|
||||||
case 8:
|
case 8:
|
||||||
copy(nonce[4:], iv)
|
copy(nonce[4:], iv)
|
||||||
case 12:
|
case 12:
|
||||||
copy(nonce[:], iv)
|
copy(nonce[:], iv)
|
||||||
default:
|
|
||||||
panic("bad nonce length")
|
|
||||||
}
|
}
|
||||||
}
|
return chacha20.NewCipher(&nonce, &keyArray)
|
||||||
|
|
||||||
func NewChaCha20Stream(key []byte, iv []byte) cipher.Stream {
|
|
||||||
var Key [32]byte
|
|
||||||
var Nonce [12]byte
|
|
||||||
if len(key) != 32 {
|
|
||||||
panic("bad key length")
|
|
||||||
}
|
|
||||||
copy(Key[:], key)
|
|
||||||
makeNonce(&Nonce, iv)
|
|
||||||
return chacha20.NewCipher(&Nonce, &Key)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user