diff --git a/README.md b/README.md index 29f0d4b..27bd9c7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/algorithms/aes/Algorithm.go b/algorithms/aes/Algorithm.go new file mode 100644 index 0000000..46e1302 --- /dev/null +++ b/algorithms/aes/Algorithm.go @@ -0,0 +1,10 @@ +package aes + +// Algorithm +type Algorithm struct{} + +// New +func New() Algorithm { + // Return Algorithm + return Algorithm{} +} diff --git a/algorithms/aes/Decrypt.go b/algorithms/aes/Decrypt.go new file mode 100644 index 0000000..a4e151f --- /dev/null +++ b/algorithms/aes/Decrypt.go @@ -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 +} diff --git a/algorithms/aes/Encrypt.go b/algorithms/aes/Encrypt.go new file mode 100644 index 0000000..0aa3528 --- /dev/null +++ b/algorithms/aes/Encrypt.go @@ -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 +}