Skip to content

Commit

Permalink
add generatePublicKey from private key
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent 6e5508f commit afbaabc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/scala/com/fluency03/blockchain/Crypto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ object Crypto {
gen.generateKeyPair()
}

def generatePublicKey(privateKey: PrivateKey): PublicKey =
KeyFactory.getInstance(KEY_ALGORITHM)
.generatePublic(new ECPublicKeySpec(ecSpec.getG.multiply(privateKey.asInstanceOf[ECPrivateKey].getD), ecSpec))

def recoverPublicKey(hex: String): PublicKey =
KeyFactory.getInstance(KEY_ALGORITHM)
.generatePublic(new ECPublicKeySpec(ecSpec.getCurve.decodePoint(hex.hex2Bytes), ecSpec))
Expand Down
1 change: 1 addition & 0 deletions src/test/scala/com/fluency03/blockchain/CryptoTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CryptoTest extends FlatSpec with Matchers {
val signature = sign(data, pair.getPrivate.getEncoded)
verify(data, pair.getPublic.getEncoded, signature) shouldEqual true

generatePublicKey(pair.getPrivate) shouldEqual pair.getPublic
recoverPublicKey(publicKeyToHex(pair.getPublic)) shouldEqual pair.getPublic
recoverPrivateKey(privateKeyToHex(pair.getPrivate)) shouldEqual pair.getPrivate
}
Expand Down

0 comments on commit afbaabc

Please sign in to comment.