forked from TBD54566975/ssi-sdk
-
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.
Public and Private Key Bi-directional Serialization (TBD54566975#149)
* pub key conversion * priv key conversion
- Loading branch information
1 parent
c79bd66
commit 62accc8
Showing
2 changed files
with
316 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
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,209 @@ | ||
package crypto | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeyToBytes(t *testing.T) { | ||
t.Run("ed25519", func(tt *testing.T) { | ||
pub, priv, err := GenerateKeyByKeyType(Ed25519) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pub) | ||
assert.NotEmpty(tt, priv) | ||
|
||
pubKeyBytes, err := PubKeyToBytes(pub) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pubKeyBytes) | ||
|
||
reconstructedPub, err := BytesToPubKey(pubKeyBytes, Ed25519) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPub) | ||
assert.EqualValues(tt, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := PrivKeyToBytes(priv) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, privKeyBytes) | ||
|
||
reconstructedPriv, err := BytesToPrivKey(privKeyBytes, Ed25519) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPriv) | ||
assert.EqualValues(tt, priv, reconstructedPriv) | ||
}) | ||
|
||
t.Run("X25519", func(tt *testing.T) { | ||
pub, priv, err := GenerateKeyByKeyType(X25519) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pub) | ||
assert.NotEmpty(tt, priv) | ||
|
||
pubKeyBytes, err := PubKeyToBytes(pub) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pubKeyBytes) | ||
|
||
reconstructedPub, err := BytesToPubKey(pubKeyBytes, X25519) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPub) | ||
assert.EqualValues(tt, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := PrivKeyToBytes(priv) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, privKeyBytes) | ||
|
||
reconstructedPriv, err := BytesToPrivKey(privKeyBytes, X25519) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPriv) | ||
assert.EqualValues(tt, priv, reconstructedPriv) | ||
}) | ||
|
||
t.Run("Secp256k1", func(tt *testing.T) { | ||
pub, priv, err := GenerateKeyByKeyType(Secp256k1) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pub) | ||
assert.NotEmpty(tt, priv) | ||
|
||
pubKeyBytes, err := PubKeyToBytes(pub) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pubKeyBytes) | ||
|
||
reconstructedPub, err := BytesToPubKey(pubKeyBytes, Secp256k1) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPub) | ||
assert.EqualValues(tt, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := PrivKeyToBytes(priv) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, privKeyBytes) | ||
|
||
reconstructedPriv, err := BytesToPrivKey(privKeyBytes, Secp256k1) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPriv) | ||
assert.EqualValues(tt, priv, reconstructedPriv) | ||
}) | ||
|
||
t.Run("P224", func(tt *testing.T) { | ||
pub, priv, err := GenerateKeyByKeyType(P224) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pub) | ||
assert.NotEmpty(tt, priv) | ||
|
||
pubKeyBytes, err := PubKeyToBytes(pub) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pubKeyBytes) | ||
|
||
reconstructedPub, err := BytesToPubKey(pubKeyBytes, P224) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPub) | ||
assert.EqualValues(tt, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := PrivKeyToBytes(priv) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, privKeyBytes) | ||
|
||
reconstructedPriv, err := BytesToPrivKey(privKeyBytes, P224) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPriv) | ||
assert.EqualValues(tt, priv, reconstructedPriv) | ||
}) | ||
|
||
t.Run("P256", func(tt *testing.T) { | ||
pub, priv, err := GenerateKeyByKeyType(P256) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pub) | ||
assert.NotEmpty(tt, priv) | ||
|
||
pubKeyBytes, err := PubKeyToBytes(pub) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pubKeyBytes) | ||
|
||
reconstructedPub, err := BytesToPubKey(pubKeyBytes, P256) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPub) | ||
assert.EqualValues(tt, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := PrivKeyToBytes(priv) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, privKeyBytes) | ||
|
||
reconstructedPriv, err := BytesToPrivKey(privKeyBytes, P256) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPriv) | ||
assert.EqualValues(tt, priv, reconstructedPriv) | ||
}) | ||
|
||
t.Run("P384", func(tt *testing.T) { | ||
pub, priv, err := GenerateKeyByKeyType(P384) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pub) | ||
assert.NotEmpty(tt, priv) | ||
|
||
pubKeyBytes, err := PubKeyToBytes(pub) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pubKeyBytes) | ||
|
||
reconstructedPub, err := BytesToPubKey(pubKeyBytes, P384) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPub) | ||
assert.EqualValues(tt, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := PrivKeyToBytes(priv) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, privKeyBytes) | ||
|
||
reconstructedPriv, err := BytesToPrivKey(privKeyBytes, P384) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPriv) | ||
assert.EqualValues(tt, priv, reconstructedPriv) | ||
}) | ||
|
||
t.Run("P521", func(tt *testing.T) { | ||
pub, priv, err := GenerateKeyByKeyType(P521) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pub) | ||
assert.NotEmpty(tt, priv) | ||
|
||
pubKeyBytes, err := PubKeyToBytes(pub) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pubKeyBytes) | ||
|
||
reconstructedPub, err := BytesToPubKey(pubKeyBytes, P521) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPub) | ||
assert.EqualValues(tt, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := PrivKeyToBytes(priv) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, privKeyBytes) | ||
|
||
reconstructedPriv, err := BytesToPrivKey(privKeyBytes, P521) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPriv) | ||
assert.EqualValues(tt, priv, reconstructedPriv) | ||
}) | ||
|
||
t.Run("RSA", func(tt *testing.T) { | ||
pub, priv, err := GenerateKeyByKeyType(RSA) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pub) | ||
assert.NotEmpty(tt, priv) | ||
|
||
pubKeyBytes, err := PubKeyToBytes(pub) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, pubKeyBytes) | ||
|
||
reconstructedPub, err := BytesToPubKey(pubKeyBytes, RSA) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPub) | ||
assert.EqualValues(tt, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := PrivKeyToBytes(priv) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, privKeyBytes) | ||
|
||
reconstructedPriv, err := BytesToPrivKey(privKeyBytes, RSA) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, reconstructedPriv) | ||
assert.EqualValues(tt, priv, reconstructedPriv) | ||
}) | ||
} |