Encrypt takes a plaintext message and a 32 byte key and returns an authenticated ciphertext.
(plaintext, key []byte)
| 25 | |
| 26 | // Encrypt takes a plaintext message and a 32 byte key and returns an authenticated ciphertext. |
| 27 | func 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 | /* |
| 47 | Decrypt decrypts a given ciphertext with a given 32 byte key and writes the result to the start of a given buffer. |
searching dependent graphs…