Skip to content

Commit

Permalink
added OOP style for encrypt/decrypt data with AES-256-GCM (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
irdaislakhuafa authored Dec 19, 2023
1 parent 7e83e2d commit 79828bf
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cryptography/aes256gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,32 @@ import (
"github.com/irdaislakhuafa/go-sdk/errors"
)

// Encrypt data with AES-256-GCM algorithm
type AES256GCM interface {
// Encrypt data with AES-256-GCM algorithm
Encrypt(text []byte) ([]byte, error)

// Decrypt data with AES-256-GCM algorithm
Decrypt(text []byte) ([]byte, error)
}

type aes256gcm struct {
key []byte
}

// Create new encrypt/decrypt with AES-256-GCM algoritm
func NewAES256GCM(key []byte) AES256GCM {
return &aes256gcm{key: key}
}

func (a *aes256gcm) Encrypt(text []byte) ([]byte, error) {
return EncryptAES256GCM(text, a.key)
}

func (a *aes256gcm) Decrypt(text []byte) ([]byte, error) {
return DecryptAES256GCM(text, a.key)
}

// Encrypt data with AES-256-GCM algorithm, like `AES256GCM.Encrypt()` but fully functional
func EncryptAES256GCM(text, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
Expand All @@ -36,7 +61,7 @@ func EncryptAES256GCM(text, key []byte) ([]byte, error) {
return []byte(result), nil
}

// Decrypt data with AES-256-GCM algorithm
// Decrypt data with AES-256-GCM algorithm, like `AES256GCM.Decrypt()` but fully functional
func DecryptAES256GCM(text, key []byte) ([]byte, error) {
text, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
Expand Down

0 comments on commit 79828bf

Please sign in to comment.