MCPcopy
hub / github.com/awnumar/memguard / Encrypt

Function Encrypt

core/crypto.go:27–44  ·  view source on GitHub ↗

Encrypt takes a plaintext message and a 32 byte key and returns an authenticated ciphertext.

(plaintext, key []byte)

Source from the content-addressed store, hash-verified

25
26// Encrypt takes a plaintext message and a 32 byte key and returns an authenticated ciphertext.
27func Encrypt(plaintext, key []byte) ([]byte, error) {
28 // Check the length of the key is correct.
29 if len(key) != 32 {
30 return nil, ErrInvalidKeyLength
31 }
32
33 // Get a reference to the key's underlying array without making a copy.
34 k := (*[32]byte)(unsafe.Pointer(&key[0]))
35
36 // Allocate space for and generate a nonce value.
37 var nonce [24]byte
38 if err := Scramble(nonce[:]); err != nil {
39 Panic(err)
40 }
41
42 // Encrypt m and return the result.
43 return secretbox.Seal(nonce[:], plaintext, &nonce, k), nil
44}
45
46/*
47Decrypt decrypts a given ciphertext with a given 32 byte key and writes the result to the start of a given buffer.

Callers 2

NewEnclaveFunction · 0.85
TestEncryptDecryptFunction · 0.85

Calls 3

ScrambleFunction · 0.85
PanicFunction · 0.85
SealMethod · 0.80

Tested by 1

TestEncryptDecryptFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…