Added the AES algorithm.

main
Tylan Tyson 1 year ago
parent e1b3e943ce
commit 8af466f337

@ -13,6 +13,7 @@ GCL is a Go library designed to offer a robust collection of cryptographic algor
GCL offers support for a diverse range of cryptographic algorithms, ensuring that your cryptographic requirements are comprehensively addressed. Our library includes:
- [X] AES
- [X] ARGON2
- [X] BLAKE2
- [X] SHA256

@ -0,0 +1,10 @@
package aes
// Algorithm
type Algorithm struct{}
// New
func New() Algorithm {
// Return Algorithm
return Algorithm{}
}

@ -0,0 +1,38 @@
package aes
import (
// Standard
"crypto/aes"
"crypto/cipher"
"encoding/base64"
)
// Decrypt
func (Algorithm) Decrypt(data string, key []byte) ([]byte, error) {
// Decoded Data
decodedData, err := base64.RawStdEncoding.DecodeString(data)
// Handle Error
if err != nil {
return []byte(""), err
}
// Block
block, err := aes.NewCipher(key)
// Handle Error
if err != nil {
return []byte(""), err
}
// GCM
gcm, err := cipher.NewGCM(block)
// Handle Error
if err != nil {
return []byte(""), err
}
// Decrypted Data
decryptedData, err := gcm.Open(nil, decodedData[:gcm.NonceSize()], decodedData[gcm.NonceSize():], nil)
// Handle Error
if err != nil {
return []byte(""), err
}
// Return Decrypted Data
return decryptedData, err
}

@ -0,0 +1,34 @@
package aes
import (
// Standard
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
)
// Encrypt
func (Algorithm) Encrypt(data []byte, key []byte) (string, error) {
// Block
block, err := aes.NewCipher(key)
// Handle Error
if err != nil {
return "", err
}
// GCM
gcm, err := cipher.NewGCM(block)
// Handle Error
if err != nil {
return "", err
}
// Nonce
nonce := make([]byte, gcm.NonceSize())
_, err = rand.Read(nonce)
// Handle Error
if err != nil {
return "", err
}
// Return Encrypted Data
return base64.RawStdEncoding.EncodeToString(gcm.Seal(nonce, nonce, []byte(data), nil)), err
}
Loading…
Cancel
Save