This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a basic encrypt and decrypt method
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/bitcoinschema/go-bitcoin/v2" | ||
) | ||
|
||
// Encrypt will encrypt the value using the encryption key | ||
func Encrypt(encryptionKey, encryptValue string) (string, error) { | ||
|
||
// Get the keys seeded with the encryption key | ||
privateKey, _, err := bitcoin.PrivateAndPublicKeys(encryptionKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Encrypt the private key | ||
var encryptedValue string | ||
if encryptedValue, err = bitcoin.EncryptWithPrivateKey( | ||
privateKey, encryptValue, | ||
); err != nil { | ||
return "", err | ||
} | ||
|
||
return encryptedValue, nil | ||
} | ||
|
||
// Decrypt will take the data and decrypt using a char(64) key | ||
func Decrypt(encryptionKey, data string) (string, error) { | ||
return bitcoin.DecryptWithPrivateKeyString(encryptionKey, data) | ||
} |
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,78 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
testEncryptValue = "##!(TEST)!##" | ||
) | ||
|
||
// TestEncrypt will test the Encrypt and Decrypt methods | ||
func TestEncrypt(t *testing.T) { | ||
t.Run("empty key", func(t *testing.T) { | ||
encrypted, err := Encrypt("", "") | ||
require.Error(t, err) | ||
assert.Equal(t, "", encrypted) | ||
}) | ||
|
||
t.Run("invalid key", func(t *testing.T) { | ||
encrypted, err := Encrypt("123", "") | ||
require.Error(t, err) | ||
assert.Equal(t, "", encrypted) | ||
}) | ||
|
||
t.Run("valid small key, no value", func(t *testing.T) { | ||
encryptionKey, err := RandomHex(64) | ||
require.NoError(t, err) | ||
|
||
var encrypted string | ||
encrypted, err = Encrypt(encryptionKey, "") | ||
require.NoError(t, err) | ||
assert.NotEqual(t, 0, len(encrypted)) | ||
|
||
var decrypted string | ||
decrypted, err = Decrypt(encryptionKey, encrypted) | ||
require.NoError(t, err) | ||
assert.Equal(t, "", decrypted) | ||
}) | ||
|
||
t.Run("hardcoded small key with value", func(t *testing.T) { | ||
encryptionKey := "a7f024b811012a88" | ||
|
||
encrypted, err := Encrypt(encryptionKey, testEncryptValue) | ||
require.NoError(t, err) | ||
|
||
var decrypted string | ||
decrypted, err = Decrypt(encryptionKey, encrypted) | ||
require.NoError(t, err) | ||
assert.Equal(t, testEncryptValue, decrypted) | ||
}) | ||
|
||
t.Run("hardcoded 32 key with value", func(t *testing.T) { | ||
encryptionKey := "be5d67424e5e3d7bb0ca69da68e423774062aebf76cb265490ac2d57d2fa2933" | ||
|
||
encrypted, err := Encrypt(encryptionKey, testEncryptValue) | ||
require.NoError(t, err) | ||
|
||
var decrypted string | ||
decrypted, err = Decrypt(encryptionKey, encrypted) | ||
require.NoError(t, err) | ||
assert.Equal(t, testEncryptValue, decrypted) | ||
}) | ||
|
||
t.Run("hardcoded 64 key with value", func(t *testing.T) { | ||
encryptionKey := "35dbe09a941a90a5f59e57020face68860d7b284b7b2973a58de8b4242ec5a925a40ac2933b7e45e78a0b3a13123520e46f9566815589ba2d345577dadee0d5e" | ||
|
||
encrypted, err := Encrypt(encryptionKey, testEncryptValue) | ||
require.NoError(t, err) | ||
|
||
var decrypted string | ||
decrypted, err = Decrypt(encryptionKey, encrypted) | ||
require.NoError(t, err) | ||
assert.Equal(t, testEncryptValue, decrypted) | ||
}) | ||
} |