-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added encryption/decryption mechanism
- Loading branch information
Showing
5 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package credentials | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
|
||
"github.com/charmbracelet/log" | ||
|
||
"github.com/google/tink/go/aead" | ||
"github.com/google/tink/go/insecurecleartextkeyset" | ||
"github.com/google/tink/go/keyset" | ||
) | ||
|
||
// Generate a AES256GCM keyset handle. | ||
func GenerateKey() string { | ||
kh, err := keyset.NewHandle(aead.AES256GCMKeyTemplate()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
|
||
// Create a writer to store the keyset | ||
buf := new(bytes.Buffer) | ||
writer := keyset.NewBinaryWriter(buf) | ||
|
||
err = insecurecleartextkeyset.Write(kh, writer) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Encode the serialized keyset from the buffer to a base64 string | ||
base64EncodedKey := hex.EncodeToString(buf.Bytes()) | ||
|
||
log.Info("Key succesfully generated") | ||
return base64EncodedKey | ||
} | ||
|
||
func parseEncodedKeyset(encodedKeyset string) *keyset.Handle{ | ||
encodedKeysetBytes, err := hex.DecodeString(encodedKeyset) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
reader := keyset.NewBinaryReader(bytes.NewReader(encodedKeysetBytes)) | ||
kh, err := insecurecleartextkeyset.Read(reader) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return kh | ||
} | ||
|
||
// Encrypt data | ||
func Encrypt(encodedKeyset string, data []byte) ([]byte, error) { | ||
kh := parseEncodedKeyset(encodedKeyset) | ||
a, err := aead.New(kh) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return a.Encrypt(data, nil) | ||
} | ||
|
||
func Decrypt(encodedKeyset string, ciphertext []byte) ([]byte, error) { | ||
kh := parseEncodedKeyset(encodedKeyset) | ||
a, err := aead.New(kh) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return a.Decrypt(ciphertext, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package credentials | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestKeyGeneration_assertKeyReturned(t *testing.T) { | ||
keyset := GenerateKey() | ||
|
||
assert.True(t, len(keyset) > 10) | ||
} | ||
|
||
func TestEncryption(t *testing.T) { | ||
keyset := GenerateKey() | ||
cleartext := make([]byte, 128) | ||
|
||
// Encrypt data | ||
ciphertext, err := Encrypt(keyset, cleartext) | ||
if err != nil { | ||
assert.FailNow(t, "unable to encrypt data: "+err.Error()) | ||
} | ||
|
||
assert.NotNil(t, ciphertext) | ||
|
||
// Decrypt data | ||
decryptedData, err := Decrypt(keyset, ciphertext) | ||
if err != nil { | ||
assert.FailNow(t, "unable to decrypt data: "+err.Error()) | ||
} | ||
|
||
assert.Equal(t, cleartext, decryptedData) | ||
} |