v/vlib/x/crypto/chacha20
2024-03-24 19:54:39 +02:00
..
chacha.v vlib.x: fix typos, uniformize readme formatting (#21085) 2024-03-24 19:54:39 +02:00
chacha_test.v x.crypto.chacha20: remove deprecated math.max_u32 in favour of builtin max_u32, remove unneceseary bits, reorder (#20838) 2024-02-15 06:40:07 +02:00
README.md vlib.x: fix typos, uniformize readme formatting (#21085) 2024-03-24 19:54:39 +02:00
xchacha.v
xchacha_test.v

module chacha20

chacha20

Chacha20 (and XChacha20) stream cipher encryption algorithm in pure V. Its mostly based on RFC 8439 and inspired by Go version of the same library.

Examples

module main

import encoding.hex
import x.crypto.chacha20

fn main() {
	// example of random key
	// you should make sure the key (and nonce) are random enough.
	// The security guarantees of the ChaCha20 require that the same nonce
	// value is never used twice with the same key.
	key := hex.decode('bf32a829ebf86d23f6a32a74ef0333401e54a6b2900d35bfadef82c5d49da15f')!
	nonce := hex.decode('a7d7cf3405631f25cc1054bd')!

	input := 'Good of gambler'.bytes()

	// encrypt and the decrypt back
	output := chacha20.encrypt(key, nonce, input)!
	input_back := chacha20.decrypt(key, nonce, output)!

	// should true
	assert input == input_back
}