Skip to content

Commit

Permalink
simplify and fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Oct 30, 2020
1 parent 3324fa6 commit 0add160
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 23 deletions.
6 changes: 1 addition & 5 deletions encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ func DecryptWithPrivateKeyString(privateKey, data string) (string, error) {
// EncryptShared will encrypt data and provide shared keys for decryption
func EncryptShared(user1PrivateKey *bsvec.PrivateKey, user2PrivateKey *bsvec.PublicKey, data []byte) (sharedPrivateKey *bsvec.PrivateKey, sharedPublicKey *bsvec.PublicKey, encrypredData []byte, err error) {
// Generate shared keys that can be decrypted by either user
sharedPrivateKey, sharedPubKey, err := GenerateSharedKeyPair(user1PrivateKey, user2PrivateKey)

if err != nil {
return nil, nil, nil, err
}
sharedPrivateKey, sharedPubKey := GenerateSharedKeyPair(user1PrivateKey, user2PrivateKey)
// Encrypt data with shared key
encrypredData, err = bsvec.Encrypt(sharedPubKey, data)
return
Expand Down
5 changes: 1 addition & 4 deletions encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,7 @@ func TestEncryptShared(t *testing.T) {
}

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

decryptedTestData, err := bsvec.Decrypt(user2SharedPrivKey, encryptedData)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ 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) {
// 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) (*bsvec.PrivateKey, *bsvec.PublicKey) {
sharedSecret := bsvec.GenerateSharedSecret(privKey, pubKey)
sharedPrivKey, sharedPubKey = bsvec.PrivKeyFromBytes(bsvec.S256(), sharedSecret)
return
return bsvec.PrivKeyFromBytes(bsvec.S256(), sharedSecret)
}

// PrivateKeyFromString turns a private key (hex encoded string) into an bsvec.PrivateKey
Expand Down
12 changes: 2 additions & 10 deletions private_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ func TestGenerateSharedKeyPair(t *testing.T) {
// User 2
privKey2, _ := CreatePrivateKey()

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

// encrypt something with the shared public key
eciesTest, err := bsvec.Encrypt(user1SharedPubKey, []byte(testString))
Expand All @@ -32,10 +29,7 @@ func TestGenerateSharedKeyPair(t *testing.T) {
}

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

decryptedTestData, err := bsvec.Decrypt(user2SharedPrivKey, eciesTest)
if err != nil {
Expand All @@ -45,8 +39,6 @@ func TestGenerateSharedKeyPair(t *testing.T) {
if string(decryptedTestData) != testString {
t.Errorf("Decrypted string doesnt match %s", decryptedTestData)
}

return
}

// TestCreatePrivateKey will test the method CreatePrivateKey()
Expand Down

0 comments on commit 0add160

Please sign in to comment.