Skip to content

Latest commit

 

History

History
41 lines (34 loc) · 1.5 KB

ReadMe.md

File metadata and controls

41 lines (34 loc) · 1.5 KB

ChaCha20 public domain encryption and decryption in Go

Public domain chacha20.go implements the ChaCha20 encryption and decryption algorithm by D. J. Bernstein. chacha20.go is derived from Bernstein's public domain ref implementation. chacha20.go implements crypto/cipher.Stream and io.Read interfaces. chacha20.go and chacha20_test.go are in the public domain and may be used for any purpose.

This version uses goroutines, yielding 8X the performance on a 3.504 GHz 12-processor Mac Studio with M2 Max.

ChaCha20 has been widely adopted.

Example use:

import "github.com/charltoncr/chacha20"
import "crypto/rand"
// ...
var m = []byte("This is a test.")    // put a real message in m here
c := make([]byte, len(m))
key := make([]byte, 32)    // 16 is acceptable; 32 is recommended
rand.Read(key)          // use your real key here
iv := make([]byte, 8)   // must be 8  
rand.Read(iv)           // use your real initialization vector here
ctx := chacha20.New(key, iv) // create a chacha20 context
ctx.Encrypt(m, c)       // encrypt m into c
// ...
ctx = chacha20.New(key, iv)   // must use same key/iv as encrypt to decrypt
ctx.Decrypt(c, m)       // decrypt c back into m
// ...

chacha20.go can also perform as ChaCha8 and ChaCha12 by using SetRounds(8) or SetRounds(12).