From e1bb6395498fa65cc5e9af216a4a8bd256d853fa Mon Sep 17 00:00:00 2001 From: fluency03 Date: Sun, 13 May 2018 21:23:14 +0200 Subject: [PATCH] add tests for hashHexs --- .../fluency03/blockchain/crypto/Base58.scala | 2 +- .../blockchain/crypto/Secp256k1.scala | 20 +++++++++---------- .../blockchain/crypto/RIPEMD160Test.scala | 14 +++++++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/scala/com/github/fluency03/blockchain/crypto/Base58.scala b/src/main/scala/com/github/fluency03/blockchain/crypto/Base58.scala index fe6c7a5..1912b9c 100644 --- a/src/main/scala/com/github/fluency03/blockchain/crypto/Base58.scala +++ b/src/main/scala/com/github/fluency03/blockchain/crypto/Base58.scala @@ -8,7 +8,7 @@ import scala.annotation.tailrec object Base58 { lazy val ALPHABET: Array[Char] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray - private val ENCODED_ZERO = ALPHABET(0) + lazy val ENCODED_ZERO = ALPHABET(0) def encodeString(text: String): Base58 = encode(text.getBytes) diff --git a/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala b/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala index 555c480..23936c9 100644 --- a/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala +++ b/src/main/scala/com/github/fluency03/blockchain/crypto/Secp256k1.scala @@ -20,11 +20,11 @@ object Secp256k1 { val ecSpec: ECParameterSpec = ECNamedCurveTable.getParameterSpec(SPECP256K1) - def sign(data: Bytes, privateKey: Bytes): Bytes = { - val keySpec: PKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey) + def sign(data: Bytes, privateKeyBytes: Bytes): Bytes = { + val keySpec: PKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes) val keyFactory: KeyFactory = KeyFactory.getInstance(KEY_ALGORITHM) - val key: PrivateKey = keyFactory.generatePrivate(keySpec) - sign(data, key) + val privateKey: PrivateKey = keyFactory.generatePrivate(keySpec) + sign(data, privateKey) } def sign(data: Bytes, privateKey: PrivateKey): Bytes = { @@ -34,11 +34,11 @@ object Secp256k1 { sig.sign() } - def verify(data: Bytes, publicKey: Bytes, signature: Bytes): Boolean = { - val keySpec: X509EncodedKeySpec = new X509EncodedKeySpec(publicKey) + def verify(data: Bytes, publicKeyBytes: Bytes, signature: Bytes): Boolean = { + val keySpec: X509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes) val keyFactory: KeyFactory = KeyFactory.getInstance(KEY_ALGORITHM) - val key: PublicKey = keyFactory.generatePublic(keySpec) - verify(data, key, signature) + val publicKey: PublicKey = keyFactory.generatePublic(keySpec) + verify(data, publicKey, signature) } def verify(data: Bytes, publicKey: PublicKey, signature: Bytes): Boolean = { @@ -77,8 +77,8 @@ object Secp256k1 { def privateKeyToBytes(privateKey: PrivateKey): Bytes = privateKey.asInstanceOf[ECPrivateKey].getD.toByteArray - def publicKeyHexToAddress(publicKey: HexString, networkBytes: String = "00"): String = - Base58.checkEncode(networkBytes.hex2Bytes ++ publicKey.hex2Bytes.hash160Digest) + def publicKeyHexToAddress(publicKeyHex: HexString, networkBytes: String = "00"): String = + Base58.checkEncode(networkBytes.hex2Bytes ++ publicKeyHex.hex2Bytes.hash160Digest) /** * Convert a Public Key to its Base58 Address. diff --git a/src/test/scala/com/github/fluency03/blockchain/crypto/RIPEMD160Test.scala b/src/test/scala/com/github/fluency03/blockchain/crypto/RIPEMD160Test.scala index 8a72c94..f989564 100644 --- a/src/test/scala/com/github/fluency03/blockchain/crypto/RIPEMD160Test.scala +++ b/src/test/scala/com/github/fluency03/blockchain/crypto/RIPEMD160Test.scala @@ -25,6 +25,20 @@ class RIPEMD160Test extends FlatSpec with Matchers { RIPEMD160.hashString("bitcoin") shouldEqual "5891bf40b0b0e8e19f524bdc2e842d012264624b" RIPEMD160.hashString("blockchain") shouldEqual "5c403af45cae136a79eea3c7e9f79c3dd049776b" + RIPEMD160.hashStrings( + "61956bf4e271df1cd88a9a7828a59c88", + "eb7ea13c176c4d03355ac27129760673") shouldEqual + "352b0b6bd7284755d5c685fb7793c9f4d672c5ff" + + RIPEMD160.hashHexs("3D7C570DCDF97F6C1823C0CE4F5ACB0878A1505992445F33BC66C7302F56C6BA") shouldEqual + "45590A0195EB451711B9D076E4C0CD04675AAD55".toLowerCase + + RIPEMD160.hashHexs("3D7C570DCDF97F6C1823C0CE4F", "5ACB0878A1505992445F33BC66C7302F56C6BA") shouldEqual + "45590A0195EB451711B9D076E4C0CD04675AAD55".toLowerCase + + RIPEMD160.hashHexs("3D7C570DCDF9" + "7F6C1823C0CE4F", "5ACB0878A1505992445" + "F33BC66C7302F56C6BA") shouldEqual + "45590A0195EB451711B9D076E4C0CD04675AAD55".toLowerCase + RIPEMD160.hash("0261107E41495C0DDC3850CD205D32896133EDD2ADDEBE7D8E20E01EFD078710".hex2Bytes) shouldEqual "A6020CF86FECEDEEF8859C172BAA1A15A575BC6F".toLowerCase