MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / Encrypt

Function Encrypt

pkg/crypto/encryption.go:33–48  ·  view source on GitHub ↗

Encrypt encrypts a plain text message. Uses AES128 keys in GCM (Galois/Counter Mode). Since GCM uses a nonce, the encrypted message will be different each time the operation is run for the same set of inputs. The returned cipher is in the format |nonce(12)|tag(16)|encrypted(plaintextLen)|.

(key types.AES128Key, plaintext []byte)

Source from the content-addressed store, hash-verified

31// Since GCM uses a nonce, the encrypted message will be different each time the operation is run for the same set of inputs.
32// The returned cipher is in the format |nonce(12)|tag(16)|encrypted(plaintextLen)|.
33func Encrypt(key types.AES128Key, plaintext []byte) ([]byte, error) {
34 cipherBlock, err := aes.NewCipher(key[:])
35 if err != nil {
36 return nil, err
37 }
38 gcm, err := cipher.NewGCM(cipherBlock)
39 if err != nil {
40 return nil, err
41 }
42 nonce := make([]byte, gcm.NonceSize())
43 _, err = io.ReadFull(rand.Reader, nonce)
44 if err != nil {
45 return nil, err
46 }
47 return gcm.Seal(nonce, nonce, plaintext, nil), nil
48}
49
50// Decrypt decrypts an encrypted message.
51// Uses AES128 keys in GCM (Galois/Counter Mode).

Callers 2

TestEncryptDecryptFunction · 0.85
EncryptMethod · 0.85

Calls

no outgoing calls

Tested by 1

TestEncryptDecryptFunction · 0.68