15
15
** SHA256 - Secure Hash Algorithm (SHA) 256-Bit (32 Bytes)**
16
16
17
17
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
+
18
27
``` ruby
19
28
require ' crypto' # # or use require 'crypto-lite'
20
29
21
30
# # 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
+ ```
26
39
27
- sha256( hex: ' 616263' ) # => "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
28
- sha256( hex: ' 0x616263' ) # => "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
29
- sha256( hex: ' 0X616263' ) # => "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
40
+ <!--
41
+ sha256( 0x616263 ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
30
42
31
43
# "auto-magic" hex string to binary string conversion heuristic
32
44
sha256( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
33
45
sha256( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
34
- ```
46
+
47
+ -->
35
48
36
49
37
50
Bonus Back Stage Tip: How does SHA256 work?
@@ -45,27 +58,32 @@ Onwards with more sha256 examples:
45
58
46
59
``` ruby
47
60
# # 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
+ <!--
50
73
sha256( 0b01100001 ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
51
74
sha256( 0x61 ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
52
75
53
- sha256( hex: ' 61' ) # => "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
54
- sha256( hex: ' 0x61' ) # => "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
55
-
56
76
# "auto-magic" hex string to binary string conversion heuristic
57
77
sha256( '0x61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
58
78
79
+ -->
59
80
60
- # # try some more
61
- sha256( " Hello, Cryptos!" ) # => "33eedea60b0662c66c289ceba71863a864cf84b00e10002ca1069bf58f9362d5"
62
- ```
63
81
64
82
65
83
** SHA3-256 - Secure Hashing Algorthim (SHA) 3, 256-Bit (32 Bytes)**
66
84
67
85
``` ruby
68
- sha3_256( " Hello, Cryptos!" ) # => "7dddf4bc9b86352b67e8823e5010ddbd2a90a854469e2517992ca7ca89e5bd58"
86
+ sha3_256( " Hello, Cryptos!" ).hexdigest # => "7dddf4bc9b86352b67e8823e5010ddbd2a90a854469e2517992ca7ca89e5bd58"
69
87
```
70
88
71
89
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
79
97
** Keccak 256-Bit**
80
98
81
99
``` ruby
82
- keccak256( " Hello, Cryptos!" ) # => "2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5"
100
+ keccak256( " Hello, Cryptos!" ).hexdigest # => "2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5"
83
101
```
84
102
85
103
@@ -101,13 +119,13 @@ original or official? - check your hash:
101
119
For keccak 256-bit:
102
120
103
121
``` ruby
104
- keccak256( ' ' ) # => "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
122
+ keccak256( ' ' ).hexdigest # => "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
105
123
```
106
124
107
125
For sha3 256-bit:
108
126
109
127
``` ruby
110
- sha3_256( ' ' ) # => "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"
128
+ sha3_256( ' ' ).hexdigest # => "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"
111
129
```
112
130
113
131
@@ -118,9 +136,9 @@ sha3_256( '' ) #=> "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80
118
136
119
137
120
138
``` ruby
121
- rmd160( " Hello, Cryptos!" ) # =>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
139
+ rmd160( " Hello, Cryptos!" ).hexdigest # =>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
122
140
# or use the alias / alternate name
123
- ripemd160( " Hello, Cryptos!" ) # =>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
141
+ ripemd160( " Hello, Cryptos!" ).hexdigest # =>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
124
142
```
125
143
126
144
@@ -162,14 +180,16 @@ to the hash function and that will "automagically"
162
180
handle the hex-to-bin conversion for you. Example:
163
181
164
182
``` ruby
165
- sha256( hex: ' 61' ) # => "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
166
- sha256( hex: ' 0x61' ) # => "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
183
+ sha256( hex: ' 61' ).hexdigest # => "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
184
+ sha256( hex: ' 0x61' ).hexdigest # => "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
167
185
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"
171
189
```
172
190
191
+
192
+ <!-- >
173
193
What about the built-in "auto-magic" hex-to-bin conversion / heuristic?
174
194
175
195
Yes, if your passed in string starts with the
@@ -194,6 +214,9 @@ hash256( '6fe6b145a3908a4d6616b13c1109717add8672c900' )
194
214
195
215
# and so on
196
216
```
217
+ -->
218
+
219
+
197
220
198
221
199
222
#### Hash Function Helpers
@@ -204,10 +227,10 @@ All-in-one "best-of-both-worlds" helper - first hash with sha256 and than hash w
204
227
205
228
206
229
``` ruby
207
- hash160( ' 02b9d1cc0b793b03b9f64d022e9c67d5f32670b03f636abf0b3147b34123d13990' )
230
+ hash160( ' 02b9d1cc0b793b03b9f64d022e9c67d5f32670b03f636abf0b3147b34123d13990' ).hexdigest
208
231
# => "e6b145a3908a4d6616b13c1109717add8672c900"
209
232
210
- hash160( ' 02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737' )
233
+ hash160( ' 02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737' ).hexdigest
211
234
# => "93ce48570b55c42c2af816aeaba06cfee1224fae"
212
235
```
213
236
@@ -222,7 +245,7 @@ All-in-one double sha256 hash helper, that is, first hash with sha256 and than h
222
245
223
246
224
247
``` ruby
225
- hash256( ' 6fe6b145a3908a4d6616b13c1109717add8672c900' )
248
+ hash256( ' 6fe6b145a3908a4d6616b13c1109717add8672c900' ).hexdigest
226
249
# => "02335f08b8fe4ddad263a50b7a33c5d38ea1cbd8fd2056a1320a3ddece541711"
227
250
```
228
251
@@ -233,29 +256,34 @@ hash256( '6fe6b145a3908a4d6616b13c1109717add8672c900' )
233
256
Base58 encoding / decoding with leading zero bytes (in hex or binary strings) getting encoded from ` 00 ` to ` 1 ` and back:
234
257
235
258
``` ruby
236
- base58( " 516b6fcd0f" ) # => "ABnLTmg"
237
- base58( " 00000000000000000000123456789abcdef0" ) # => "111111111143c9JGph3DZ"
259
+ base58( hex: " 516b6fcd0f" ) # => "ABnLTmg"
260
+ base58( hex: " 00000000000000000000123456789abcdef0" ) # => "111111111143c9JGph3DZ"
238
261
# 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"
241
264
242
- unbase58( " ABnLTmg" ) # => "516b6fcd0f"
265
+ unbase58( " ABnLTmg" ) # => "516b6fcd0f"
243
266
unbase58( " 111111111143c9JGph3DZ" ) # => "00000000000000000000123456789abcdef0"
244
267
```
245
268
269
+ <!-- todo/fix: check if unbase58 needs #bin_to_hex ???
270
+ -->
271
+
246
272
247
273
** BASE58CHECK - BASE58(X || SHA256(SHA256(X))[ :4] )**
248
274
249
275
Base58 encoding with an extra 4-byte secure hash checksum.
250
276
251
277
``` ruby
252
- base58check( " 516b6fcd0f" ) # => "237LSrY9NUUas"
253
- base58check( " 00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31" ) # => "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"
278
+ base58check( hex: " 516b6fcd0f" ) # => "237LSrY9NUUas"
279
+ base58check( hex: " 00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31" ) # => "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"
254
280
255
281
unbase58check( " 237LSrY9NUUas" ) # => "516b6fcd0f"
256
282
unbase58check( " 1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs" ) # => "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"
257
283
```
258
284
285
+ <!-- todo/fix: check if unbase58check needs #bin_to_hex ???
286
+ -->
259
287
260
288
261
289
### Public Key Signature Algorithms
@@ -313,7 +341,7 @@ Sign a transaction with an (elliptic curve) private key:
313
341
``` ruby
314
342
# Step 1 - Calculate the Transaction (tx) Hash
315
343
tx = ' from: Alice to: Bob cryptos: 43_000_000_000'
316
- txhash = sha256( tx )
344
+ txhash = sha256( tx ).hexdigest
317
345
318
346
# Step 2 - Get the Signer's Private key
319
347
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:
340
368
``` ruby
341
369
# Step 1 - Calculate the Transaction (tx) Hash
342
370
tx = ' from: Alice to: Bob cryptos: 43_000_000_000'
343
- txhash = sha256( tx )
371
+ txhash = sha256( tx ).hexdigest
344
372
345
373
# Step 2 - Get the Signer's Public Key
346
374
public_key = EC ::PublicKey .new (
@@ -582,23 +610,23 @@ Let's follow the steps from [How to create Bitcoin Address](https://en.bitcoin.i
582
610
pk = " 0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
583
611
584
612
# 1. Perform SHA-256 hashing on the public key
585
- step1 = sha256( pk )
613
+ step1 = sha256( hex: pk ).hexdigest
586
614
# => "0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98"
587
615
588
616
# 2. Perform RIPEMD-160 hashing on the result of SHA-256
589
- step2 = ripemd160( step1 )
617
+ step2 = ripemd160( hex: step1 ).hexdigest
590
618
# => "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"
591
619
592
620
# 3. Add version byte in front of RIPEMD-160 hash (0x00 for Bitcoin Main Network)
593
621
step3 = " 00" + step2
594
622
# => "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"
595
623
596
624
# 4. Perform SHA-256 hash on the extended RIPEMD-160 result
597
- step4 = sha256( step3 )
625
+ step4 = sha256( hex: step3 ).hexdigest
598
626
# => "ad3c854da227c7e99c4abfad4ea41d71311160df2e415e713318c70d67c6b41c"
599
627
600
628
# 5. Perform SHA-256 hash on the result of the previous SHA-256 hash
601
- step5 = sha256( step4 )
629
+ step5 = sha256( hex: step4 ).hexdigest
602
630
# => "c7f18fe8fcbed6396741e58ad259b5cb16b7fd7f041904147ba1dcffabf747fd"
603
631
604
632
# 6. Take the first 4 bytes of the second SHA-256 hash. This is the address checksum
@@ -613,7 +641,7 @@ step7 = step3 + step6
613
641
614
642
# 8. Convert the result from a byte string into a base58 string using Base58 encoding.
615
643
# This is the most commonly used Bitcoin Address format.
616
- addr = base58( step7 )
644
+ addr = base58( hex: step7 )
617
645
# => "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"
618
646
```
619
647
@@ -629,7 +657,7 @@ pk = "0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
629
657
# 1. Perform HASH-160 hashing on the public key
630
658
# a) Perform SHA-256 hashing on the public key
631
659
# b) Perform RIPEMD-160 hashing on the result of SHA-256
632
- step1 = hash160( pk )
660
+ step1 = hash160( hex: pk ).hexdigest
633
661
# => "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31"
634
662
635
663
# 2. Add version byte in front of RIPEMD-160 hash (0x00 for Bitoin Main Network)
@@ -646,7 +674,7 @@ step2 = "00" + step1
646
674
# e) Convert the result from a byte string into a base58 string
647
675
# using Base58 encoding.
648
676
# This is the most commonly used Bitcoin Address format.
649
- addr = base58check( step2 )
677
+ addr = base58check( hex: step2 )
650
678
# => "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs"
651
679
```
652
680
@@ -676,11 +704,11 @@ This is all then converted to Base58, which shortens the string and makes it eas
676
704
privatekey = " ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2"
677
705
extended = " 80" + privatekey + " 01"
678
706
# => "80ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db201"
679
- checksum = hash256( extended )[0 ..7 ]
707
+ checksum = hash256( hex: extended ).hexdigest [0 ..7 ]
680
708
# => "66557e53"
681
709
extendedchecksum = extended + checksum
682
710
# => "80ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db20166557e53"
683
- wif = base58( extendedchecksum )
711
+ wif = base58( hex: extendedchecksum )
684
712
# => "L5EZftvrYaSudiozVRzTqLcHLNDoVn7H5HSfM9BAN6tMJX8oTWz6"
685
713
```
686
714
@@ -690,7 +718,7 @@ Or let's try again with the base58check (`BASE58(X || SHA256(SHA256(X))[:4])`) s
690
718
privatekey = " ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db2"
691
719
extended = " 80" + privatekey + " 01"
692
720
# => "80ef235aacf90d9f4aadd8c92e4b2562e1d9eb97f0df9ba3b508258739cb013db201"
693
- wif = base58check( extended )
721
+ wif = base58check( hex: extended )
694
722
# => "L5EZftvrYaSudiozVRzTqLcHLNDoVn7H5HSfM9BAN6tMJX8oTWz6"
695
723
```
696
724
@@ -832,7 +860,7 @@ pk = "022744c02580b4905349bc481a60c308c2d98d823d44888835047f6bc5c38c4e8f"
832
860
# 1. Perform HASH-160 hashing on the public key
833
861
# a) Perform SHA-256 hashing on the public key
834
862
# b) Perform RIPEMD-160 hashing on the result of SHA-256
835
- step1 = hash160( pk )
863
+ step1 = hash160( hex: pk ).hexdigest
836
864
# => "a1f37969bcb547cd9c3a28fa07c2269ef813340a"
837
865
838
866
# 2. Add version byte in front of RIPEMD-160 hash (0x1e for Dodge Main Network)
@@ -849,7 +877,7 @@ step2 = "1e" + step1
849
877
# e) Convert the result from a byte string into a base58 string
850
878
# using Base58 encoding.
851
879
# This is the most commonly used Dodge Address format.
852
- addr = base58check( step2 )
880
+ addr = base58check( hex: step2 )
853
881
# => "DKuR12onkdp5GxC5c8DgXhGe4Z2AqCK3Xh"
854
882
```
855
883
@@ -980,7 +1008,7 @@ to calculate the hash of the public key
980
1008
981
1009
``` ruby
982
1010
pub = " 6e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b83b5c38e5e2b0c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0"
983
- hash = keccak256( pub )
1011
+ hash = keccak256( hex: pub ).hexdigest
984
1012
# => "2a5bc342ed616b5ba5732269001d3f1ef827552ae1114027bd3ecf1f086ba0f9"
985
1013
```
986
1014
0 commit comments