Skip to content

Commit

Permalink
Add convenience methods, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
salyh committed Jul 12, 2024
1 parent c4e780d commit a9e460e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
36 changes: 36 additions & 0 deletions gcm/gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/cipher"
"crypto/rand"
"crypto/sha512"
"encoding/base64"
"errors"
"os"

Expand Down Expand Up @@ -36,6 +37,28 @@ func Decrypt(cipherText, secretKey, aad []byte) ([]byte, error) {
return plainText, nil
}

func DecryptFromString(cipherText string, secretKey, aad []byte) ([]byte, error) {
decoded, err := base64.StdEncoding.DecodeString(cipherText)
if err != nil {
return nil, err
}

plainText, err := Decrypt(decoded, secretKey, aad)
if err != nil {
return nil, err
}
return plainText, nil

}

func DecryptFromStringToString(cipherText string, secretKey, aad []byte) (string, error) {
plainText, err := DecryptFromString(cipherText, secretKey, aad)
if err != nil {
return "", err
}
return string(plainText), nil
}

func Encrypt(plainText, secretKey, aad []byte) ([]byte, error) {
aes, err := aes.NewCipher(secretKey)
if err != nil {
Expand All @@ -62,6 +85,19 @@ func Encrypt(plainText, secretKey, aad []byte) ([]byte, error) {
return cipherText, nil
}

func EncryptStringToString(plainText string, secretKey, aad []byte) (string, error) {
return EncryptToString([]byte(plainText), secretKey, aad)
}

func EncryptToString(plainText, secretKey, aad []byte) (string, error) {
cipherText, err := Encrypt(plainText, secretKey, aad)
if err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(cipherText), nil
}

func EncryptFile(plainTextFile, cipherTextFile string, secretKey []byte) error {
plainText, err := os.ReadFile(plainTextFile)
if err != nil {
Expand Down
87 changes: 87 additions & 0 deletions gcm/gcm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package gcm

import (
"bytes"
"testing"
)

var key1 = []byte("12345678123456781234567812345678")
var key2 = []byte("11111111111111111111111111111111")
var plainTextBytes = []byte("this is plaintext")
var plainTextString = "this is plaintext"
var aad = []byte("some aad")

func TestAesGcm(t *testing.T) {

a, err := Encrypt(plainTextBytes, key1, aad)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

b, err := Decrypt(a, key1, aad)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

if !bytes.Equal(b, plainTextBytes) {
t.Fatalf("Expected %v == %v", b, plainTextBytes)
}
}

func TestAesGcmWrongKey(t *testing.T) {

a, err := Encrypt(plainTextBytes, key1, aad)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

_, err = Decrypt(a, key2, aad)
if err == nil {
t.Fatalf("Unexpected error %v", err)
}
}

func TestAesGcmWrongAad(t *testing.T) {

a, err := Encrypt(plainTextBytes, key1, aad)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

_, err = Decrypt(a, key1, nil)
if err == nil {
t.Fatalf("Unexpected error %v", err)
}

}

func TestAesGcmString(t *testing.T) {

a, err := EncryptStringToString(plainTextString, key1, aad)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

b, err := DecryptFromStringToString(a, key1, aad)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

if b != plainTextString {
t.Fatalf("Expected %v == %v", b, plainTextBytes)
}
}

func TestAesGcmStringWrongAad(t *testing.T) {

a, err := EncryptStringToString(plainTextString, key1, aad)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

_, err = DecryptFromStringToString(a, key1, key2)
if err == nil {
t.Fatalf("Unexpected error %v", err)
}

}

0 comments on commit a9e460e

Please sign in to comment.