Skip to content

Commit

Permalink
add tests for hashHexs
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 13, 2018
1 parent 1a8e9e4 commit e1bb639
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e1bb639

Please sign in to comment.