MCPcopy Create free account
hub / github.com/devspace-sh/devspace / EncryptAES

Function EncryptAES

pkg/util/encryption/util.go:29–65  ·  view source on GitHub ↗

EncryptAES encrypts the given data with the given key

(key, data []byte)

Source from the content-addressed store, hash-verified

27
28// EncryptAES encrypts the given data with the given key
29func EncryptAES(key, data []byte) ([]byte, error) {
30 // Ensure key is 32 bytes long
31 key = PadKey(key)
32
33 // generate a new aes cipher using our 32 byte long key
34 c, err := aes.NewCipher(key)
35 // if there are any errors, handle them
36 if err != nil {
37 return nil, err
38 }
39
40 // gcm or Galois/Counter Mode, is a mode of operation
41 // for symmetric key cryptographic block ciphers
42 // - https://en.wikipedia.org/wiki/Galois/Counter_Mode
43 gcm, err := cipher.NewGCM(c)
44 // if any error generating new GCM
45 // handle them
46 if err != nil {
47 return nil, err
48 }
49
50 // creates a new byte array the size of the nonce
51 // which must be passed to Seal
52 nonce := make([]byte, gcm.NonceSize())
53 // populates our nonce with a cryptographically secure
54 // random sequence
55 if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
56 return nil, err
57 }
58
59 // here we encrypt our text using the Seal function
60 // Seal encrypts and authenticates plaintext, authenticates the
61 // additional data and appends the result to dst, returning the updated
62 // slice. The nonce must be NonceSize() bytes long and unique for all
63 // time, for a given key.
64 return gcm.Seal(nonce, nonce, data, nil), nil
65}
66
67// DecryptAES decrypts the given data with the given key
68func DecryptAES(key, data []byte) ([]byte, error) {

Callers 3

SaveMethod · 0.92
SaveMethod · 0.92
TestEncryptDecryptAESFunction · 0.85

Calls 1

PadKeyFunction · 0.85

Tested by 1

TestEncryptDecryptAESFunction · 0.68