wms-be/pkg/utils/aes.go

97 lines
2.2 KiB
Go

package utils
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
)
const (
// todo
KEY = "8e71bbce7451ba2835de5aea73e4f3f96821455240823d2fd8174975b8321bfc!"
)
// https://www.melvinvivas.com/how-to-encrypt-and-decrypt-data-using-aes
func AESEncrypt(stringToEncrypt string) (encryptedString string, err error) {
//Since the key is in string, we need to convert decode it to bytes
key, err := hex.DecodeString(KEY)
if err != nil {
return "", err
}
plaintext := []byte(stringToEncrypt)
//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
//Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode
//https://golang.org/pkg/crypto/cipher/#NewGCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
//Create a nonce. Nonce should be from GCM
nonce := make([]byte, aesGCM.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
//Encrypt the data using aesGCM.Seal
//Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix.
ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil)
return fmt.Sprintf("%x", ciphertext), nil
}
func AESDecrypt(encryptedString string) (decryptedString string, err error) {
defer func() {
if r := recover(); r != nil {
decryptedString = ""
err = errors.New("error in decrypting")
}
}()
key, err := hex.DecodeString(KEY)
if err != nil {
return "", errors.New("error in decoding key")
}
enc, err := hex.DecodeString(encryptedString)
if err != nil {
return "", errors.New("error in decoding encrypted string")
}
//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
//Create a new GCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
//Get the nonce size
nonceSize := aesGCM.NonceSize()
//Extract the nonce from the encrypted data
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]
//Decrypt the data
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", nil
}
return string(plaintext), nil
}