MCPcopy
hub / github.com/gtank/cryptopasta / Encrypt

Function Encrypt

encrypt.go:37–55  ·  view source on GitHub ↗

Encrypt encrypts data using 256-bit AES-GCM. This both hides the content of the data and provides a check that it hasn't been altered. Output takes the form nonce|ciphertext|tag where '|' indicates concatenation.

(plaintext []byte, key *[32]byte)

Source from the content-addressed store, hash-verified

35// the data and provides a check that it hasn't been altered. Output takes the
36// form nonce|ciphertext|tag where '|' indicates concatenation.
37func Encrypt(plaintext []byte, key *[32]byte) (ciphertext []byte, err error) {
38 block, err := aes.NewCipher(key[:])
39 if err != nil {
40 return nil, err
41 }
42
43 gcm, err := cipher.NewGCM(block)
44 if err != nil {
45 return nil, err
46 }
47
48 nonce := make([]byte, gcm.NonceSize())
49 _, err = io.ReadFull(rand.Reader, nonce)
50 if err != nil {
51 return nil, err
52 }
53
54 return gcm.Seal(nonce, nonce, plaintext, nil), nil
55}
56
57// Decrypt decrypts data using 256-bit AES-GCM. This both hides the content of
58// the data and provides a check that it hasn't been altered. Expects input

Callers 2

TestEncryptDecryptGCMFunction · 0.85
BenchmarkAESGCMFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestEncryptDecryptGCMFunction · 0.68
BenchmarkAESGCMFunction · 0.68