-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
236 additions
and
43 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
50 changes: 50 additions & 0 deletions
50
src/main/scala/com/github/fluency03/blockchain/Base58.scala
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,50 @@ | ||
package com.github.fluency03.blockchain | ||
|
||
import org.bouncycastle.util.encoders.Hex | ||
|
||
import scala.annotation.tailrec | ||
|
||
object Base58 { | ||
|
||
lazy val ALPHABET: Array[Char] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray | ||
private val ENCODED_ZERO = ALPHABET(0) | ||
|
||
def encodeString(str: String): String = encode(str.getBytes) | ||
|
||
def encodeHex(hex: String): String = encode(hex.hex2Bytes) | ||
|
||
def encode(bytes: Bytes): String = { | ||
@tailrec | ||
def buildBase58(res: String, bi: BigInt): String = | ||
if (bi <= 0) res | ||
else buildBase58(ALPHABET((bi % 58).toInt) + res, bi / 58) | ||
|
||
@tailrec | ||
def confirmZeroByte(res: String, bytes: Array[Byte], idx: Int): String = | ||
if (bytes(idx) != 0 || idx >= bytes.length) res | ||
else confirmZeroByte(ENCODED_ZERO + res, bytes, idx + 1) | ||
|
||
confirmZeroByte(buildBase58("", bytes.toBigInt), bytes, 0) | ||
} | ||
|
||
def decode(str: String): Bytes = { | ||
@tailrec | ||
def restoreBigInt(chars: Array[Char], bi: BigInt, idx: Int): BigInt = | ||
if (idx >= chars.length) bi | ||
else { | ||
val i: Int = ALPHABET.zipWithIndex.find(t => t._1 == chars(idx)).map(_._2).get | ||
restoreBigInt(chars, bi * 58 + i, idx + 1) | ||
} | ||
|
||
val bi = restoreBigInt(str.toCharArray, 0, 0) | ||
Hex.decode(bi.toString(16)) | ||
} | ||
|
||
def decodeToHex(str: String): String = new String(decode(str)) | ||
|
||
def checkEncodeHex(str: String): String = encodeString(str + str.hex2Bytes.toSha256Digest.toSha256.substring(0, 8)) | ||
|
||
def checkEncode(bytes: Bytes): String = encode(bytes ++ bytes.toSha256Digest.toSha256Digest.slice(0, 4)) | ||
|
||
|
||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/scala/com/github/fluency03/blockchain/RIPEMD160.scala
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,5 @@ | ||
package com.github.fluency03.blockchain | ||
|
||
object RIPEMD160 { | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/github/fluency03/blockchain/SHA256.scala
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,31 @@ | ||
package com.github.fluency03.blockchain | ||
|
||
import java.security.MessageDigest | ||
|
||
object SHA256 { | ||
|
||
/** | ||
* Generate SHA256 Hash from a input String. | ||
* https://gist.github.com/navicore/6234040bbfce3aa58f866db314c07c15 | ||
*/ | ||
def hash(text: String) : String = hash(text.getBytes) | ||
|
||
/** | ||
* Generate SHA256 Hash from a input Array of Byte. | ||
*/ | ||
def hash(bytes: Bytes) : String = String.format("%064x", | ||
new java.math.BigInteger(1, hashToDigest(bytes))) | ||
|
||
/** | ||
* Generate digest from a input Array of Byte. | ||
*/ | ||
def hashToDigest(bytes: Bytes): Bytes = | ||
MessageDigest.getInstance("SHA-256").digest(bytes) | ||
|
||
/** | ||
* Calculate the hash of concatenation a Seq of Strings. | ||
*/ | ||
def hashAll(strings: String*): String = hash(strings mkString "") | ||
|
||
|
||
} |
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
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
44 changes: 44 additions & 0 deletions
44
src/test/scala/com/github/fluency03/blockchain/Base58Test.scala
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,44 @@ | ||
package com.github.fluency03.blockchain | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class Base58Test extends FlatSpec with Matchers { | ||
|
||
"Base58" should "encode String to Base58 and decode it back to original." in { | ||
Base58.encodeString("abc") shouldEqual "ZiCa" | ||
Base58.encodeString("bitcoin") shouldEqual "4jJc4sAwPs" | ||
Base58.encodeString("blockchain") shouldEqual "6XidGdGeMggztM" | ||
Base58.encodeString("00F5F2D624CFB5C3F66D06123D0829D1C9CEBF770E2C13A798") shouldEqual | ||
"bSMTi3tDLFwyLC26U3SB8ctp7Y4iCcGpXxztHUWQSeo4tXV6p7WABrsxVa4tB7n8e8iT" | ||
|
||
Base58.decodeToHex("ZiCa") shouldEqual "abc" | ||
Base58.decodeToHex("4jJc4sAwPs") shouldEqual "bitcoin" | ||
Base58.decodeToHex("6XidGdGeMggztM") shouldEqual "blockchain" | ||
|
||
Base58.encodeString("04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a254112" + | ||
"9368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d") shouldEqual | ||
"2f6iufmY2PoZhwnZkWwvNYmN6A3G4dH8TSDH1Y5FKpC7yCxoJfqStHLBmkUYrwkekaYUttiAwYWCtioTWJn1s" + | ||
"mMSGtMwsyLqmSLpQbLrTpSrMNjKcNzL1viBDBkFuJEM3KMtPAx2g2hVLeBFDP79iaqHvwuVyu2zaViPVeLjWtRpWzu4sM" | ||
|
||
Base58.decodeToHex("2f6iufmY2PoZhwnZkWwvNYmN6A3G4dH8TSDH1Y5FKpC7yCxoJfqStHLBmkUYrwkekaYUttiAwYWCtioTWJn1s" + | ||
"mMSGtMwsyLqmSLpQbLrTpSrMNjKcNzL1viBDBkFuJEM3KMtPAx2g2hVLeBFDP79iaqHvwuVyu2zaViPVeLjWtRpWzu4sM") shouldEqual | ||
"04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a254112" + | ||
"9368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d" | ||
|
||
ripemd160Of("61956bf4e271df1cd88a9a7828a59c88eb7ea13c176c4d03355ac27129760673") shouldEqual | ||
"352b0b6bd7284755d5c685fb7793c9f4d672c5ff" | ||
ripemd160Of("abcd") shouldEqual "2e7e536fd487deaa943fda5522d917bdb9011b7a" | ||
ripemd160Of("205575f4f33a39ff47f569613a694c6321d6cdd7") shouldEqual "bd4e962413308b4a6689aa0e7cff5e419391c3db" | ||
ripemd160Of("bitcoin") shouldEqual "5891bf40b0b0e8e19f524bdc2e842d012264624b" | ||
ripemd160Of("blockchain") shouldEqual "5c403af45cae136a79eea3c7e9f79c3dd049776b" | ||
|
||
Crypto.publicKeyToAddress("04B4D653FCBB4B96000C99343F23B08A44FA306031E0587F9E657AB" + | ||
"4A2541129368D7D9BB05CD8AFBDF7705A6540D98028236965553F91BF1C5B4F70073F55B55D") shouldEqual | ||
"1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH" | ||
|
||
Base58.encodeHex("0088C2D2FA846282C870A76CADECBE45C4ACD72BB655DA1216") shouldEqual | ||
"1DU8Hi1sbHTpEP9vViBEkEw6noeUrgKkJH" | ||
|
||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/test/scala/com/github/fluency03/blockchain/RIPEMD160Test.scala
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,12 @@ | ||
package com.github.fluency03.blockchain | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class RIPEMD160Test extends FlatSpec with Matchers { | ||
|
||
"RIPEMD160" should "encode String to RIPEMD160 and decode it back to original." in { | ||
|
||
|
||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/test/scala/com/github/fluency03/blockchain/SHA256Test.scala
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,39 @@ | ||
package com.github.fluency03.blockchain | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class SHA256Test extends FlatSpec with Matchers { | ||
|
||
"SHA256" should "encode String to SHA256 and decode it back to original." in { | ||
SHA256.hash("open sesame") shouldEqual "41ef4bb0b23661e66301aac36066912dac037827b4ae63a7b1165a5aa93ed4eb" | ||
SHA256.hashAll("open", " ", "sesame") shouldEqual "41ef4bb0b23661e66301aac36066912dac037827b4ae63a7b1165a5aa93ed4eb" | ||
SHA256.hash("") shouldEqual "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | ||
SHA256.hash("0000000000000000000000000000000000000000000000000000000000000") shouldEqual "a738b0b5c122d30af5b9da1c63c5d590a31aeafa7de1723ee9b5e3a11c9def35" | ||
|
||
SHA256.hash(("04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a25411" + | ||
"29368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d").hex2Bytes) shouldEqual | ||
"173BDED8F2A2069C193E63EA30DC8FD20E815EC3642B9C24AD7002C03D1BFB9B".toLowerCase | ||
|
||
SHA256.hashToDigest(("04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a25411" + | ||
"29368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d").hex2Bytes) shouldEqual | ||
"173BDED8F2A2069C193E63EA30DC8FD20E815EC3642B9C24AD7002C03D1BFB9B".hex2Bytes | ||
|
||
SHA256.hash("0088C2D2FA846282C870A76CADECBE45C4ACD72BB6".hex2Bytes) shouldEqual | ||
"1F87490FC565C795595563D56412A0100CD1F29FFB60A3779789FE0C018C6164".toLowerCase | ||
|
||
|
||
SHA256.hash("1F87490FC565C795595563D56412A0100CD1F29FFB60A3779789FE0C018C6164".hex2Bytes) shouldEqual | ||
"55DA1216A5EF5BAE605B543A5A9CE2AC8A8FA1781AA037F35DE3F2222BAD8127".toLowerCase | ||
|
||
ripemd160Of("173BDED8F2A2069C193E63EA30DC8FD20E815EC3642B9C24AD7002C03D1BFB9B".hex2Bytes) shouldEqual | ||
"88C2D2FA846282C870A76CADECBE45C4ACD72BB6".toLowerCase | ||
|
||
hash160BytesOf(("04b4d653fcbb4b96000c99343f23b08a44fa306031e0587f9e657ab4a25411" + | ||
"29368d7d9bb05cd8afbdf7705a6540d98028236965553f91bf1c5b4f70073f55b55d").hex2Bytes) shouldEqual | ||
"88C2D2FA846282C870A76CADECBE45C4ACD72BB6".hex2Bytes | ||
|
||
SHA256.hashToDigest("00".hex2Bytes ++ "88C2D2FA846282C870A76CADECBE45C4ACD72BB6".hex2Bytes) shouldEqual | ||
"1F87490FC565C795595563D56412A0100CD1F29FFB60A3779789FE0C018C6164".hex2Bytes | ||
} | ||
|
||
} |
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