Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Added a basic encrypt and decrypt method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 2, 2022
1 parent 396a8ff commit a719acb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
30 changes: 30 additions & 0 deletions utils/encrypt.go
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)
}
78 changes: 78 additions & 0 deletions utils/encrypt_test.go
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)
})
}

0 comments on commit a719acb

Please sign in to comment.