1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00

feat(libcrypto): move into the Portainer repository EE-5476 (#10230)

This commit is contained in:
andres-portainer 2023-09-01 17:27:19 -03:00 committed by GitHub
parent 9a234204fa
commit 090fa4aeb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 194 additions and 7 deletions

35
pkg/libcrypto/decrypt.go Normal file
View file

@ -0,0 +1,35 @@
package libcrypto
import (
"crypto/aes"
"crypto/cipher"
"errors"
)
// Decrypt decrypts data using 256-bit AES-GCM. This both hides the content of
// the data and provides a check that it hasn't been altered. Expects input
// form nonce|ciphertext|tag where '|' indicates concatenation.
// Creates a 32bit hash of the key before decrypting the data.
func Decrypt(data []byte, key []byte) ([]byte, error) {
hashKey := Hash32Bit(key)
block, err := aes.NewCipher(hashKey)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
if len(data) < gcm.NonceSize() {
return nil, errors.New("malformed ciphertext")
}
return gcm.Open(nil,
data[:gcm.NonceSize()],
data[gcm.NonceSize():],
nil,
)
}