Skip to content

Commit f433407

Browse files
committed
up
1 parent f9c0425 commit f433407

17 files changed

+461
-343
lines changed

crypto-lite/README.md

+80-52
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,36 @@
1515
**SHA256 - Secure Hash Algorithm (SHA) 256-Bit (32 Bytes)**
1616

1717

18+
Note: By default all hash functions return binary strings.
19+
Use `String#hexdigest` (or `String#bin_to_hex`
20+
or `String#btoh`)
21+
to convert binary strings to hex(adecimal)
22+
strings (via `Bytes.bin_to_hex`).
23+
24+
25+
26+
1827
``` ruby
1928
require 'crypto' ## or use require 'crypto-lite'
2029

2130
## try abc
22-
sha256( "abc" ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
23-
sha256( "abc".b ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
24-
sha256( "\x61\x62\x63" ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
25-
sha256( 0x616263 ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
31+
sha256( "abc" ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
32+
sha256( "abc".b ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
33+
sha256( "\x61\x62\x63" ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
34+
35+
sha256( hex: '616263' ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
36+
sha256( hex: '0x616263' ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
37+
sha256( hex: '0X616263' ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
38+
```
2639

27-
sha256( hex: '616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
28-
sha256( hex: '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
29-
sha256( hex: '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
40+
<!--
41+
sha256( 0x616263 ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
3042
3143
# "auto-magic" hex string to binary string conversion heuristic
3244
sha256( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
3345
sha256( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
34-
```
46+
47+
-->
3548

3649

3750
Bonus Back Stage Tip: How does SHA256 work?
@@ -45,27 +58,32 @@ Onwards with more sha256 examples:
4558

4659
``` ruby
4760
## try a
48-
sha256( "a" ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
49-
sha256( "\x61" ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
61+
sha256( "a" ).hexdigest #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
62+
sha256( "\x61" ).hexdigest #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
63+
64+
sha256( hex: '61' ).hexdigest #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
65+
sha256( hex: '0x61' ).hexdigest #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
66+
67+
68+
## try some more
69+
sha256( "Hello, Cryptos!" ).hexdigest #=> "33eedea60b0662c66c289ceba71863a864cf84b00e10002ca1069bf58f9362d5"
70+
```
71+
72+
<!--
5073
sha256( 0b01100001 ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
5174
sha256( 0x61 ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
5275
53-
sha256( hex: '61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
54-
sha256( hex: '0x61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
55-
5676
# "auto-magic" hex string to binary string conversion heuristic
5777
sha256( '0x61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
5878
79+
-->
5980

60-
## try some more
61-
sha256( "Hello, Cryptos!" ) #=> "33eedea60b0662c66c289ceba71863a864cf84b00e10002ca1069bf58f9362d5"
62-
```
6381

6482

6583
**SHA3-256 - Secure Hashing Algorthim (SHA) 3, 256-Bit (32 Bytes)**
6684

6785
``` ruby
68-
sha3_256( "Hello, Cryptos!" ) #=> "7dddf4bc9b86352b67e8823e5010ddbd2a90a854469e2517992ca7ca89e5bd58"
86+
sha3_256( "Hello, Cryptos!" ).hexdigest #=> "7dddf4bc9b86352b67e8823e5010ddbd2a90a854469e2517992ca7ca89e5bd58"
6987
```
7088

7189
Note: Yes, SHA256 vs SHA3-256 / SHA-2 vs SHA-3 the hashing functions are
@@ -79,7 +97,7 @@ The sha3_256 is part of the (newer) Secure Hash Algorithm (SHA) 3 family / stand
7997
**Keccak 256-Bit**
8098

8199
``` ruby
82-
keccak256( "Hello, Cryptos!" ) #=> "2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5"
100+
keccak256( "Hello, Cryptos!" ).hexdigest #=> "2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5"
83101
```
84102

85103

@@ -101,13 +119,13 @@ original or official? - check your hash:
101119
For keccak 256-bit:
102120

103121
``` ruby
104-
keccak256( '' ) #=> "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
122+
keccak256( '' ).hexdigest #=> "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
105123
```
106124

107125
For sha3 256-bit:
108126

109127
``` ruby
110-
sha3_256( '' ) #=> "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"
128+
sha3_256( '' ).hexdigest #=> "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"
111129
```
112130

113131

@@ -118,9 +136,9 @@ sha3_256( '' ) #=> "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80
118136

119137

120138
``` ruby
121-
rmd160( "Hello, Cryptos!" ) #=>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
139+
rmd160( "Hello, Cryptos!" ).hexdigest #=>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
122140
# or use the alias / alternate name
123-
ripemd160( "Hello, Cryptos!" ) #=>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
141+
ripemd160( "Hello, Cryptos!" ).hexdigest #=>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
124142
```
125143

126144

@@ -162,14 +180,16 @@ to the hash function and that will "automagically"
162180
handle the hex-to-bin conversion for you. Example:
163181

164182
``` ruby
165-
sha256( hex: '61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
166-
sha256( hex: '0x61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
183+
sha256( hex: '61' ).hexdigest #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
184+
sha256( hex: '0x61' ).hexdigest #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
167185

168-
sha256( hex: '616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
169-
sha256( hex: '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
170-
sha256( hex: '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
186+
sha256( hex: '616263' ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
187+
sha256( hex: '0x616263' ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
188+
sha256( hex: '0X616263' ).hexdigest #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
171189
```
172190

191+
192+
<!-- >
173193
What about the built-in "auto-magic" hex-to-bin conversion / heuristic?
174194
175195
Yes, if your passed in string starts with the
@@ -194,6 +214,9 @@ hash256( '6fe6b145a3908a4d6616b13c1109717add8672c900' )
194214
195215
# and so on
196216
```
217+
-->
218+
219+
197220

198221

199222
#### Hash Function Helpers
@@ -204,10 +227,10 @@ All-in-one "best-of-both-worlds" helper - first hash with sha256 and than hash w
204227

205228

206229
``` ruby
207-
hash160( '02b9d1cc0b793b03b9f64d022e9c67d5f32670b03f636abf0b3147b34123d13990' )
230+
hash160( '02b9d1cc0b793b03b9f64d022e9c67d5f32670b03f636abf0b3147b34123d13990' ).hexdigest
208231
#=> "e6b145a3908a4d6616b13c1109717add8672c900"
209232

210-
hash160( '02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737' )
233+
hash160( '02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737' ).hexdigest
211234
#=> "93ce48570b55c42c2af816aeaba06cfee1224fae"
212235
```
213236

@@ -222,7 +245,7 @@ All-in-one double sha256 hash helper, that is, first hash with sha256 and than h
222245
223246

224247
``` ruby
225-
hash256( '6fe6b145a3908a4d6616b13c1109717add8672c900' )
248+
hash256( '6fe6b145a3908a4d6616b13c1109717add8672c900' ).hexdigest
226249
#=> "02335f08b8fe4ddad263a50b7a33c5d38ea1cbd8fd2056a1320a3ddece541711"
227250
```
228251

@@ -233,29 +256,34 @@ hash256( '6fe6b145a3908a4d6616b13c1109717add8672c900' )
233256
Base58 encoding / decoding with leading zero bytes (in hex or binary strings) getting encoded from `00` to `1` and back:
234257

235258
``` ruby
236-
base58( "516b6fcd0f" ) #=> "ABnLTmg"
237-
base58( "00000000000000000000123456789abcdef0" ) #=> "111111111143c9JGph3DZ"
259+
base58( hex: "516b6fcd0f" ) #=> "ABnLTmg"
260+
base58( hex: "00000000000000000000123456789abcdef0" ) #=> "111111111143c9JGph3DZ"
238261
# or with optional 0x or 0X prefix
239-
base58( "0x516b6fcd0f" ) #=> "ABnLTmg"
240-
base58( "0x00000000000000000000123456789abcdef0" ) #=> "111111111143c9JGph3DZ"
262+
base58( hex: "0x516b6fcd0f" ) #=> "ABnLTmg"
263+
base58( hex: "0x00000000000000000000123456789abcdef0" ) #=> "111111111143c9JGph3DZ"
241264

242-
unbase58( "ABnLTmg" ) #=> "516b6fcd0f"
265+
unbase58( "ABnLTmg" ) #=> "516b6fcd0f"
243266
unbase58( "111111111143c9JGph3DZ" ) #=> "00000000000000000000123456789abcdef0"
244267
```
245268

269+
<!-- todo/fix: check if unbase58 needs #bin_to_hex ???
270+
-->
271+
246272

247273
**BASE58CHECK - BASE58(X || SHA256(SHA256(X))[:4])**
248274

249275
Base58 encoding with an extra 4-byte secure hash checksum.
250276

251277
``` ruby
252-
base58check( "516b6fcd0f" ) #=> "237LSrY9NUUas"
253-
base58check( "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31" ) #=> "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"
278+
base58check( hex: "516b6fcd0f" ) #=> "237LSrY9NUUas"
279+
base58check( hex: "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31" ) #=> "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"
254280

255281
unbase58check( "237LSrY9NUUas" ) #=> "516b6fcd0f"
256282
unbase58check( "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs" ) #=> "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"
257283
```
258284

285+
<!-- todo/fix: check if unbase58check needs #bin_to_hex ???
286+
-->
259287

260288

261289
### Public Key Signature Algorithms
@@ -313,7 +341,7 @@ Sign a transaction with an (elliptic curve) private key:
313341
``` ruby
314342
# Step 1 - Calculate the Transaction (tx) Hash
315343
tx = 'from: Alice to: Bob cryptos: 43_000_000_000'
316-
txhash = sha256( tx )
344+
txhash = sha256( tx ).hexdigest
317345

318346
# Step 2 - Get the Signer's Private key
319347
private_key = EC::PrivateKey.new( 1234 ) # This private key is just an example. It should be much more secure!
@@ -340,7 +368,7 @@ Verify a signed transaction with an (elliptic curve) public key:
340368
``` ruby
341369
# Step 1 - Calculate the Transaction (tx) Hash
342370
tx = 'from: Alice to: Bob cryptos: 43_000_000_000'
343-
txhash = sha256( tx )
371+
txhash = sha256( tx ).hexdigest
344372

345373
# Step 2 - Get the Signer's Public Key
346374
public_key = EC::PublicKey.new(
@@ -582,23 +610,23 @@ Let's follow the steps from [How to create Bitcoin Address](https://en.bitcoin.i
582610
pk = "0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
583611

584612
# 1. Perform SHA-256 hashing on the public key
585-
step1 = sha256( pk )
613+
step1 = sha256( hex: pk ).hexdigest
586614
#=> "0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98"
587615

588616
# 2. Perform RIPEMD-160 hashing on the result of SHA-256
589-
step2 = ripemd160( step1 )
617+
step2 = ripemd160( hex: step1 ).hexdigest
590618
#=> "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"
591619

592620
# 3. Add version byte in front of RIPEMD-160 hash (0x00 for Bitcoin Main Network)
593621
step3 = "00" + step2
594622
#=> "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"
595623

596624
# 4. Perform SHA-256 hash on the extended RIPEMD-160 result
597-
step4 = sha256( step3 )
625+
step4 = sha256( hex: step3 ).hexdigest
598626
#=> "ad3c854da227c7e99c4abfad4ea41d71311160df2e415e713318c70d67c6b41c"
599627

600628
# 5. Perform SHA-256 hash on the result of the previous SHA-256 hash
601-
step5 = sha256( step4 )
629+
step5 = sha256( hex: step4 ).hexdigest
602630
#=> "c7f18fe8fcbed6396741e58ad259b5cb16b7fd7f041904147ba1dcffabf747fd"
603631

604632
# 6. Take the first 4 bytes of the second SHA-256 hash. This is the address checksum
@@ -613,7 +641,7 @@ step7 = step3 + step6
613641

614642
# 8. Convert the result from a byte string into a base58 string using Base58 encoding.
615643
# This is the most commonly used Bitcoin Address format.
616-
addr = base58( step7 )
644+
addr = base58( hex: step7 )
617645
#=> "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"
618646
```
619647

@@ -629,7 +657,7 @@ pk = "0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
629657
# 1. Perform HASH-160 hashing on the public key
630658
# a) Perform SHA-256 hashing on the public key
631659
# b) Perform RIPEMD-160 hashing on the result of SHA-256
632-
step1 = hash160( pk )
660+
step1 = hash160( hex: pk ).hexdigest
633661
#=> "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"
634662

635663
# 2. Add version byte in front of RIPEMD-160 hash (0x00 for Bitoin Main Network)
@@ -646,7 +674,7 @@ step2 = "00" + step1
646674
# e) Convert the result from a byte string into a base58 string
647675
# using Base58 encoding.
648676
# This is the most commonly used Bitcoin Address format.
649-
addr = base58check( step2 )
677+
addr = base58check( hex: step2 )
650678
#=> "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"
651679
```
652680

@@ -676,11 +704,11 @@ This is all then converted to Base58, which shortens the string and makes it eas
676704
privatekey = "ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2"
677705
extended = "80" + privatekey + "01"
678706
#=> "80ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db201"
679-
checksum = hash256( extended )[0..7]
707+
checksum = hash256( hex: extended ).hexdigest[0..7]
680708
#=> "66557e53"
681709
extendedchecksum = extended + checksum
682710
#=> "80ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db20166557e53"
683-
wif = base58( extendedchecksum )
711+
wif = base58( hex: extendedchecksum )
684712
#=> "L5EZftvrYaSudiozVRzTqLcHLNDoVn7H5HSfM9BAN6tMJX8oTWz6"
685713
```
686714

@@ -690,7 +718,7 @@ Or let's try again with the base58check (`BASE58(X || SHA256(SHA256(X))[:4])`) s
690718
privatekey = "ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2"
691719
extended = "80" + privatekey + "01"
692720
#=> "80ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db201"
693-
wif = base58check( extended )
721+
wif = base58check( hex: extended )
694722
#=> "L5EZftvrYaSudiozVRzTqLcHLNDoVn7H5HSfM9BAN6tMJX8oTWz6"
695723
```
696724

@@ -832,7 +860,7 @@ pk = "022744c02580b4905349bc481a60c308c2d98d823d44888835047f6bc5c38c4e8f"
832860
# 1. Perform HASH-160 hashing on the public key
833861
# a) Perform SHA-256 hashing on the public key
834862
# b) Perform RIPEMD-160 hashing on the result of SHA-256
835-
step1 = hash160( pk )
863+
step1 = hash160( hex: pk ).hexdigest
836864
#=> "a1f37969bcb547cd9c3a28fa07c2269ef813340a"
837865

838866
# 2. Add version byte in front of RIPEMD-160 hash (0x1e for Dodge Main Network)
@@ -849,7 +877,7 @@ step2 = "1e" + step1
849877
# e) Convert the result from a byte string into a base58 string
850878
# using Base58 encoding.
851879
# This is the most commonly used Dodge Address format.
852-
addr = base58check( step2 )
880+
addr = base58check( hex: step2 )
853881
#=> "DKuR12onkdp5GxC5c8DgXhGe4Z2AqCK3Xh"
854882
```
855883

@@ -980,7 +1008,7 @@ to calculate the hash of the public key
9801008

9811009
``` ruby
9821010
pub = "6e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0"
983-
hash = keccak256( pub )
1011+
hash = keccak256( hex: pub ).hexdigest
9841012
#=> "2a5bc342ed616b5ba5732269001d3f1ef827552ae1114027bd3ecf1f086ba0f9"
9851013
```
9861014

crypto-lite/Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Hoe.spec 'crypto-lite' do
1919
self.history_file = 'CHANGELOG.md'
2020

2121
self.extra_deps = [
22+
['bytes'],
2223
['digest-lite'],
2324
['base32-alphabets'],
2425
['base58-alphabets'],

0 commit comments

Comments
 (0)