Skip to content

Commit

Permalink
added generate shared keypair
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Oct 30, 2020
1 parent 6fd66f5 commit fdd03e2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ View the generated [documentation](https://pkg.go.dev/github.com/bitcoinschema/g
- [Create PrivateKey](private_key.go)
- [PrivateKey (string) to Address (string)](address.go)
- [PrivateKey from string](private_key.go)
- [Generate Shared Keypair](private_key.go)
- [Get Private and Public keys](private_key.go)
- [WIF to PrivateKey](private_key.go)
- [PrivateKey to WIF](private_key.go)
Expand Down
7 changes: 7 additions & 0 deletions private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
"github.com/bitcoinsv/bsvutil"
)

// GenerateSharedKeyPair creates shared keys that can be used to encrypt/decrypt data that can be decrypted by yourself (privkey) and also the owner of the given public key
func GenerateSharedKeyPair(privKey *bsvec.PrivateKey, pubKey *bsvec.PublicKey) (sharedPrivKey *bsvec.PrivateKey, sharedPubKey *bsvec.PublicKey, err error) {
sharedSecret := bsvec.GenerateSharedSecret(privKey, pubKey)
sharedPrivKey, sharedPubKey = bsvec.PrivKeyFromBytes(bsvec.S256(), sharedSecret)
return
}

// PrivateKeyFromString turns a private key (hex encoded string) into an bsvec.PrivateKey
func PrivateKeyFromString(privateKey string) (*bsvec.PrivateKey, error) {
if len(privateKey) == 0 {
Expand Down
43 changes: 43 additions & 0 deletions private_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,51 @@ import (
"encoding/hex"
"fmt"
"testing"

"github.com/bitcoinsv/bsvd/bsvec"
)

// TestGenerateSharedKeyPair will test creating a shared key that can be used to encrypt data that can be decrypted by yourself (privkey) and also the owner of the given public key
func TestGenerateSharedKeyPair(t *testing.T) {

// The data that will be encrypted / shared
testString := "testing 1, 2, 3..."

// User 1
privKey1, _ := CreatePrivateKey()

// User 2
privKey2, _ := CreatePrivateKey()

_, user1SharedPubKey, err := GenerateSharedKeyPair(privKey1, privKey2.PubKey())
if err != nil {
t.Errorf("Failed to generate a shared key pair %s", err)
}

// encrypt something with the shared public key
eciesTest, err := bsvec.Encrypt(user1SharedPubKey, []byte(testString))
if err != nil {
t.Errorf("Failed to encrypt test data %s", err)
}

// user 2 decrypts it
user2SharedPrivKey, _, err := GenerateSharedKeyPair(privKey2, privKey1.PubKey())
if err != nil {
t.Errorf("Failed to generate a shared key pair %s", err)
}

decryptedTestData, err := bsvec.Decrypt(user2SharedPrivKey, eciesTest)
if err != nil {
t.Errorf("Failed to decrypt test data %s", err)
}

if string(decryptedTestData) != testString {
t.Errorf("Decrypted string doesnt match %s", decryptedTestData)
}

return
}

// TestCreatePrivateKey will test the method CreatePrivateKey()
func TestCreatePrivateKey(t *testing.T) {
rawKey, err := CreatePrivateKey()
Expand Down

0 comments on commit fdd03e2

Please sign in to comment.