crypto-lite 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +80 -52
- data/Rakefile +1 -0
- data/lib/crypto-lite/helper.rb +53 -14
- data/lib/crypto-lite/metal.rb +36 -85
- data/lib/crypto-lite/version.rb +2 -2
- data/lib/crypto-lite.rb +12 -118
- data/test/test_base58.rb +9 -6
- data/test/test_bitcoin_addr.rb +7 -7
- data/test/test_hash.rb +9 -3
- data/test/test_hash_sha.rb +22 -15
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa60a6933e949f9b2cdf2ca665775533edd9ebbf80682aa99095bf39016f44ad
|
4
|
+
data.tar.gz: a02c578732edb686e84bfcb62bdcfc3e5d18583af22566c33819faff7ff071cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25f749066f58f0de25d62d4b73ba9a6169ba66d2f5e3541ccbd56954f74261e42e754ea270bc91c6ad6d6a9194e20b2beeeb461a2439a981ef576443c02d9af8
|
7
|
+
data.tar.gz: badbeaeb2d2e5e54fb7af1127933615c1e5a8a9f7595bc9f2451c603bcc70cd63856f18bc9a0a7013a8f9b82ed441ecc168383b3187338a1c815eac166a8b523
|
data/README.md
CHANGED
@@ -15,23 +15,36 @@
|
|
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
|
-
|
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
|
-
|
28
|
-
sha256(
|
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" )
|
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,11 +610,11 @@ 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)
|
@@ -594,11 +622,11 @@ 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
|
|
data/Rakefile
CHANGED
data/lib/crypto-lite/helper.rb
CHANGED
@@ -1,25 +1,64 @@
|
|
1
|
-
|
2
1
|
module CryptoHelper
|
3
|
-
### note: use include CryptoHelper
|
4
|
-
## to get "top-level" / global helpers
|
5
2
|
|
6
|
-
## add convenience "top-level" helpers
|
7
|
-
def sha256( *args, **kwargs ) Crypto.sha256( *args, **kwargs ); end
|
8
|
-
def sha3_256( *args, **kwargs ) Crypto.sha3_256( *args, **kwargs ); end
|
9
3
|
|
10
|
-
|
4
|
+
###
|
5
|
+
# todo: change ''.b to BYTE_ZERO constant or such - why? why not?
|
6
|
+
|
7
|
+
|
8
|
+
######
|
9
|
+
# add convenience helpers
|
10
|
+
def base58( bin=''.b, hex: nil )
|
11
|
+
bin = hex.hex_to_bin if hex # uses Bytes.hex_to_bin
|
12
|
+
Crypto::Metal.base58( bin )
|
13
|
+
end
|
11
14
|
|
12
|
-
def
|
13
|
-
|
15
|
+
def base58check( bin=''.b, hex: nil )
|
16
|
+
bin = hex.hex_to_bin if hex # uses Bytes.hex_to_bin
|
17
|
+
Crypto::Metal.base58check( bin )
|
18
|
+
end
|
19
|
+
|
20
|
+
def keccak256( bin=''.b, hex: nil )
|
21
|
+
bin = hex.hex_to_bin if hex # uses Bytes.hex_to_bin
|
22
|
+
Crypto::Metal.keccak256( bin )
|
23
|
+
end
|
24
|
+
|
25
|
+
def rmd160( bin=''.b, hex: nil )
|
26
|
+
bin = hex.hex_to_bin if hex # uses Bytes.hex_to_bin
|
27
|
+
Crypto::Metal.rmd160( bin )
|
28
|
+
end
|
29
|
+
## add alias RIPEMD160 - why? why not?
|
14
30
|
alias_method :ripemd160, :rmd160
|
15
31
|
|
16
|
-
def
|
32
|
+
def sha256( bin=''.b, hex: nil,
|
33
|
+
engine: nil )
|
34
|
+
bin = hex.hex_to_bin if hex # uses Bytes.hex_to_bin
|
35
|
+
Crypto::Metal.sha256( bin, engine )
|
36
|
+
end
|
37
|
+
|
38
|
+
def sha3_256( bin=''.b, hex: nil,
|
39
|
+
engine: nil )
|
40
|
+
bin = hex.hex_to_bin if hex # uses Bytes.hex_to_bin
|
41
|
+
Crypto::Metal.sha3_256( bin, engine )
|
42
|
+
end
|
17
43
|
|
18
|
-
def
|
44
|
+
def hash160( bin=''.b, hex: nil )
|
45
|
+
bin = hex.hex_to_bin if hex # uses Bytes.hex_to_bin
|
46
|
+
Crypto::Metal.hash160( bin )
|
47
|
+
end
|
19
48
|
|
49
|
+
def hash256( bin=''.b, hex: nil )
|
50
|
+
bin = hex.hex_to_bin if hex # uses Bytes.hex_to_bin
|
51
|
+
Crypto::Metal.hash256( bin )
|
52
|
+
end
|
53
|
+
end # module CryptoHelper
|
20
54
|
|
21
|
-
def base58( *args, **kwargs ) Crypto.base58( *args, **kwargs ); end
|
22
|
-
def base58check( *args, **kwargs ) Crypto.base58check( *args, **kwargs ); end
|
23
|
-
end
|
24
55
|
|
25
56
|
|
57
|
+
module Crypto
|
58
|
+
extend CryptoHelper
|
59
|
+
##
|
60
|
+
## lets you use
|
61
|
+
## Crypto.sha256( bin, hex: ) -> bin
|
62
|
+
## Crytpo.base58( bin, hex: ) -> bin
|
63
|
+
## etc.
|
64
|
+
end # module Crypto
|
data/lib/crypto-lite/metal.rb
CHANGED
@@ -1,72 +1,56 @@
|
|
1
|
-
module Crypto
|
2
|
-
module Metal
|
3
|
-
|
4
|
-
def self.debug?() Crypto.debug?; end
|
5
1
|
|
6
|
-
|
7
|
-
|
8
|
-
## work with binary strings (aka byte arrays) / data
|
9
|
-
|
10
|
-
##
|
11
|
-
## todo/check: use/keep bin-suffix in name - why? why not?
|
2
|
+
module Crypto
|
3
|
+
module MetalHelper
|
12
4
|
|
5
|
+
####
|
6
|
+
# note: all (digest) hash functions
|
7
|
+
# take in a binary string and return a binary string!!!!
|
13
8
|
|
14
|
-
def
|
15
|
-
|
16
|
-
Base58::Bitcoin.encode_bin( input )
|
9
|
+
def base58( bin )
|
10
|
+
Base58::Bitcoin.encode_bin( bin )
|
17
11
|
end
|
18
12
|
|
19
|
-
def
|
20
|
-
|
21
|
-
|
22
|
-
base58bin( input + hash256[0,4] )
|
13
|
+
def base58check( bin )
|
14
|
+
hash256 = hash256( bin )
|
15
|
+
base58( bin + hash256[0,4] )
|
23
16
|
end
|
24
17
|
|
25
18
|
|
26
19
|
########################
|
27
20
|
# (secure) hash functions
|
28
21
|
|
29
|
-
def
|
30
|
-
|
31
|
-
Digest::KeccakLite.digest( message, 256 )
|
22
|
+
def keccak256( bin )
|
23
|
+
Digest::KeccakLite.digest( bin, 256 )
|
32
24
|
end
|
33
25
|
|
34
|
-
def
|
35
|
-
|
36
|
-
Digest::RMD160.digest( message )
|
26
|
+
def rmd160( bin )
|
27
|
+
Digest::RMD160.digest( bin )
|
37
28
|
end
|
38
|
-
|
39
29
|
## add alias RIPEMD160 - why? why not?
|
40
|
-
|
41
|
-
alias_method :ripemd160bin, :rmd160bin
|
42
|
-
end
|
30
|
+
alias_method :ripemd160, :rmd160
|
43
31
|
|
44
32
|
|
45
|
-
def
|
46
|
-
message = message( input ) ## "normalize" / convert to (binary) string
|
47
|
-
|
33
|
+
def sha256( bin, engine=nil ) ## todo/check: add alias sha256b or such to - why? why not?
|
48
34
|
if engine && ['openssl'].include?( engine.to_s.downcase )
|
49
|
-
puts " engine: #{engine}" if debug?
|
35
|
+
## puts " engine: #{engine}" if debug?
|
50
36
|
digest = OpenSSL::Digest::SHA256.new
|
51
37
|
## or use OpenSSL::Digest.new( 'SHA256' )
|
52
|
-
digest.update(
|
38
|
+
digest.update( bin )
|
53
39
|
digest.digest
|
54
40
|
else ## use "built-in" hash function from digest module
|
55
|
-
Digest::SHA256.digest(
|
41
|
+
Digest::SHA256.digest( bin )
|
56
42
|
end
|
57
43
|
end
|
58
44
|
|
59
45
|
|
60
|
-
def
|
61
|
-
message = message( input ) ## "normalize" / convert to (binary) string
|
62
|
-
|
46
|
+
def sha3_256( bin, engine=nil )
|
63
47
|
if engine && ['openssl'].include?( engine.to_s.downcase )
|
64
|
-
puts " engine: #{engine}" if debug?
|
48
|
+
## puts " engine: #{engine}" if debug?
|
65
49
|
digest = OpenSSL::Digest.new( 'SHA3-256' )
|
66
|
-
digest.update(
|
50
|
+
digest.update( bin )
|
67
51
|
digest.digest
|
68
52
|
else ## use "built-in" hash function from digest module
|
69
|
-
Digest::SHA3Lite.digest(
|
53
|
+
Digest::SHA3Lite.digest( bin, 256 )
|
70
54
|
end
|
71
55
|
end
|
72
56
|
|
@@ -80,56 +64,23 @@ module Metal
|
|
80
64
|
# ripemd160.unpack( "H*" )[0] # Convert back to hex
|
81
65
|
# end
|
82
66
|
|
83
|
-
def
|
84
|
-
|
85
|
-
|
86
|
-
rmd160bin(sha256bin( message ))
|
67
|
+
def hash160( bin )
|
68
|
+
rmd160(sha256( bin ))
|
87
69
|
end
|
88
70
|
|
89
|
-
|
90
|
-
|
91
|
-
message = message( input ) ## "normalize" / convert to (binary) string
|
92
|
-
|
93
|
-
sha256bin(sha256bin( message ))
|
71
|
+
def hash256( bin )
|
72
|
+
sha256(sha256( bin ))
|
94
73
|
end
|
74
|
+
end # module MetalHelper
|
95
75
|
|
96
76
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
106
|
-
puts " input: #{input} (#{input_type})"
|
107
|
-
end
|
108
|
-
|
109
|
-
message = if input.is_a?( Integer ) ## assume byte if single (unsigned) integer
|
110
|
-
raise ArgumentError, "expected unsigned byte (0-255) - got #{input} (0x#{input.to_s(16)}) - can't pack negative number; sorry" if input < 0
|
111
|
-
## note: pack - H (String) => hex string (high nibble first)
|
112
|
-
## todo/check: is there a better way to convert integer number to (binary) string!!!
|
113
|
-
[input.to_s(16)].pack('H*')
|
114
|
-
else ## assume (binary) string
|
115
|
-
input
|
116
|
-
end
|
117
|
-
|
118
|
-
if debug?
|
119
|
-
bytes = message.bytes
|
120
|
-
bin = bytes.map {|byte| byte.to_s(2).rjust(8, "0")}.join( ' ' )
|
121
|
-
hex = bytes.map {|byte| byte.to_s(16).rjust(2, "0")}.join( ' ' )
|
122
|
-
puts " #{pluralize( bytes.size, 'byte')}: #{bytes.inspect}"
|
123
|
-
puts " binary: #{bin}"
|
124
|
-
puts " hex: #{hex}"
|
125
|
-
end
|
126
|
-
|
127
|
-
message
|
128
|
-
end
|
77
|
+
module Metal
|
78
|
+
extend MetalHelper
|
79
|
+
## lets you use
|
80
|
+
## Crypto::Metal.sha256( bin ) -> bin
|
81
|
+
## Crytpo::Metal.base58( bin ) -> bin
|
82
|
+
## etc.
|
83
|
+
end # module Metal
|
84
|
+
end # module Crypto
|
129
85
|
|
130
|
-
def self.pluralize( count, noun )
|
131
|
-
count == 1 ? "#{count} #{noun}" : "#{count} #{noun}s"
|
132
|
-
end
|
133
86
|
|
134
|
-
end # module Metal
|
135
|
-
end # module Crypto
|
data/lib/crypto-lite/version.rb
CHANGED
data/lib/crypto-lite.rb
CHANGED
@@ -5,141 +5,35 @@ require 'openssl'
|
|
5
5
|
|
6
6
|
|
7
7
|
## our own 3rd party (2nd party?)
|
8
|
+
require 'bytes'
|
9
|
+
|
10
|
+
## extend String
|
11
|
+
class String
|
12
|
+
alias_method :hexdigest, :bin_to_hex ## note: bin_to_hex added via Bytes!!!
|
13
|
+
end
|
14
|
+
|
15
|
+
|
8
16
|
require 'digest-lite' # e.g. keccak (original submission/proposal NOT official sha3)
|
9
17
|
require 'base32-alphabets'
|
10
18
|
require 'base58-alphabets'
|
11
19
|
require 'elliptic'
|
12
20
|
|
13
21
|
|
14
|
-
|
15
22
|
## our own code
|
16
23
|
require_relative 'crypto-lite/version' # note: let version always go first
|
17
24
|
require_relative 'crypto-lite/config'
|
18
25
|
require_relative 'crypto-lite/metal'
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
module Crypto
|
24
|
-
|
25
|
-
def self.base58( *args, **kwargs )
|
26
|
-
input = args_to_input( args, kwargs )
|
27
|
-
Metal.base58bin( input )
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.base58check( *args, **kwargs )
|
31
|
-
input = args_to_input( args, kwargs )
|
32
|
-
Metal.base58bin_check( input )
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
########################
|
37
|
-
# (secure) hash functions
|
38
|
-
|
39
|
-
def self.keccak256( *args, **kwargs )
|
40
|
-
input = args_to_input( args, kwargs )
|
41
|
-
Metal.keccak256bin( input ).unpack( 'H*' )[0]
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
def self.rmd160( *args, **kwargs )
|
46
|
-
input = args_to_input( args, kwargs )
|
47
|
-
Metal.rmd160bin( input ).unpack( 'H*' )[0]
|
48
|
-
end
|
49
|
-
|
50
|
-
## add alias RIPEMD160 - why? why not?
|
51
|
-
class << self
|
52
|
-
alias_method :ripemd160, :rmd160
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
def self.sha256( *args, **kwargs )
|
57
|
-
input = args_to_input( args, kwargs )
|
58
|
-
engine = kwargs[:engine]
|
59
|
-
Metal.sha256bin( input, engine ).unpack( 'H*' )[0]
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.sha3_256( *args, **kwargs )
|
63
|
-
input = args_to_input( args, kwargs )
|
64
|
-
Metal.sha3_256bin( input ).unpack( 'H*' )[0]
|
65
|
-
end
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
def self.hash160( *args, **kwargs )
|
70
|
-
input = args_to_input( args, kwargs )
|
71
|
-
Metal.hash160bin( input ).unpack( 'H*' )[0]
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.hash256( *args, **kwargs )
|
75
|
-
input = args_to_input( args, kwargs )
|
76
|
-
Metal.hash256bin( input ).unpack( 'H*' )[0]
|
77
|
-
end
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
########
|
82
|
-
# more helpers
|
83
|
-
## check if it is a hex (string)
|
84
|
-
## - allow optiona 0x or 0X and allow abcdef and ABCDEF
|
85
|
-
HEX_RE = /\A(?:0x)?[0-9a-f]+\z/i
|
86
|
-
|
87
|
-
def self.args_to_input( args, kwargs )
|
88
|
-
if kwargs[:hex]
|
89
|
-
hex = kwargs[:hex]
|
90
|
-
raise ArgumentError, "expected hex string (0-9a-f) - got >#{hex}< - can't pack string; sorry" unless hex =~ HEX_RE
|
91
|
-
|
92
|
-
hex = strip0x( hex ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
|
93
|
-
[hex].pack( 'H*' )
|
94
|
-
else ## assume single input arg for now
|
95
|
-
input = args[0]
|
96
|
-
input = hex_to_bin_automagic( input ) ## add automagic hex (string) to bin (string) check - why? why not?
|
97
|
-
input
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def self.hex_to_bin_automagic( input )
|
102
|
-
## todo/check/fix: add configure setting to turn off automagic - why? why not?
|
103
|
-
if input.is_a?( String ) && input =~ HEX_RE
|
104
|
-
if input[0,2] == '0x' || input[0,2] == '0X'
|
105
|
-
## starting with 0x or 0X always assume hex string for now - why? why not?
|
106
|
-
input = input[2..-1]
|
107
|
-
[input].pack( 'H*' )
|
108
|
-
elsif input.size >= 10
|
109
|
-
## note: hex heuristic!!
|
110
|
-
## for now assumes string MUST have more than 10 digits to qualify!!!
|
111
|
-
[input].pack( 'H*' )
|
112
|
-
else
|
113
|
-
input ## pass through as is!!! (e.g. a, abc, etc.)
|
114
|
-
end
|
115
|
-
else
|
116
|
-
input ## pass through as is
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
|
121
|
-
def self.strip0x( str ) ## todo/check: add alias e.g. strip_hex_prefix or such - why? why not?
|
122
|
-
(str[0,2] == '0x' || str[0,2] == '0X') ? str[2..-1] : str
|
123
|
-
end
|
124
|
-
|
125
|
-
# def self.hex_to_bin( str )
|
126
|
-
# str = strip0x( str ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
|
127
|
-
# [str].pack( 'H*' )
|
128
|
-
# end
|
129
|
-
|
130
|
-
end # module Crypto
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
26
|
require_relative 'crypto-lite/helper'
|
136
|
-
include CryptoHelper # add convenience "top-level" / global helpers
|
137
27
|
|
138
28
|
|
139
29
|
require_relative 'crypto-lite/sign_rsa'
|
140
30
|
RSA = Crypto::RSA
|
141
31
|
|
142
32
|
|
33
|
+
## auto-add top-level helpers - why? why not?
|
34
|
+
include CryptoHelper
|
35
|
+
|
36
|
+
|
143
37
|
|
144
38
|
|
145
39
|
puts CryptoLite.banner ## say hello
|
data/test/test_base58.rb
CHANGED
@@ -9,14 +9,16 @@ require 'helper'
|
|
9
9
|
class TestBase58 < MiniTest::Test
|
10
10
|
|
11
11
|
HEX_TESTS = [
|
12
|
-
["00000000000000000000",
|
12
|
+
["00000000000000000000", "1111111111"],
|
13
13
|
["00000000000000000000123456789abcdef0", "111111111143c9JGph3DZ"],
|
14
14
|
]
|
15
15
|
|
16
|
+
|
16
17
|
def test_hex
|
17
18
|
HEX_TESTS.each do |item|
|
18
|
-
assert_equal item[1], base58( hex: item[0] )
|
19
|
-
|
19
|
+
assert_equal item[1], Crypto.base58( hex: item[0] )
|
20
|
+
bin = Bytes.hex_to_bin( item[0] )
|
21
|
+
assert_equal item[1], Crypto.base58( bin )
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
@@ -27,10 +29,11 @@ def test_bitcoin_addr
|
|
27
29
|
pkh = 'f54a5851e9372b87810a8e60cdd2e7cfd80b6e31'
|
28
30
|
|
29
31
|
## all-in-one
|
30
|
-
assert_equal addr_exp, base58check( hex: '00' + pkh )
|
31
|
-
|
32
|
+
assert_equal addr_exp, Crypto.base58check( hex: '00' + pkh )
|
33
|
+
bin = Bytes.hex_to_bin('00' + pkh)
|
34
|
+
assert_equal addr_exp, Crypto.base58check( bin )
|
32
35
|
|
33
|
-
assert_equal addr_exp, Crypto::Metal.
|
36
|
+
assert_equal addr_exp, Crypto::Metal.base58check( "\x00" + [pkh].pack('H*') )
|
34
37
|
end
|
35
38
|
|
36
39
|
end # class TestBase58
|
data/test/test_bitcoin_addr.rb
CHANGED
@@ -11,11 +11,11 @@ class TestBitcoinAddr < MiniTest::Test
|
|
11
11
|
def test_bitcoin_addr_v1
|
12
12
|
pk = "0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
|
13
13
|
|
14
|
-
step1 = sha256( pk )
|
14
|
+
step1 = Crypto.sha256( hex: pk ).hexdigest
|
15
15
|
assert_equal "0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98",
|
16
16
|
step1
|
17
17
|
|
18
|
-
step2 = ripemd160( step1 )
|
18
|
+
step2 = Crypto.ripemd160( hex: step1 ).hexdigest
|
19
19
|
assert_equal "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31",
|
20
20
|
step2
|
21
21
|
|
@@ -23,11 +23,11 @@ def test_bitcoin_addr_v1
|
|
23
23
|
assert_equal "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31",
|
24
24
|
step3
|
25
25
|
|
26
|
-
step4 = sha256( step3 )
|
26
|
+
step4 = Crypto.sha256( hex: step3 ).hexdigest
|
27
27
|
assert_equal "ad3c854da227c7e99c4abfad4ea41d71311160df2e415e713318c70d67c6b41c",
|
28
28
|
step4
|
29
29
|
|
30
|
-
step5 = sha256( step4 )
|
30
|
+
step5 = Crypto.sha256( hex: step4 ).hexdigest
|
31
31
|
assert_equal "c7f18fe8fcbed6396741e58ad259b5cb16b7fd7f041904147ba1dcffabf747fd",
|
32
32
|
step5
|
33
33
|
|
@@ -37,7 +37,7 @@ def test_bitcoin_addr_v1
|
|
37
37
|
step7 = step3 + step6
|
38
38
|
assert_equal "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31c7f18fe8", step7
|
39
39
|
|
40
|
-
addr = base58( step7 )
|
40
|
+
addr = Crypto.base58( hex: step7 )
|
41
41
|
assert_equal "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs", addr
|
42
42
|
end
|
43
43
|
|
@@ -45,13 +45,13 @@ end
|
|
45
45
|
def test_bitcoin_addr_v2
|
46
46
|
pk = "0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
|
47
47
|
|
48
|
-
step1 = hash160( pk )
|
48
|
+
step1 = Crypto.hash160( hex: pk ).hexdigest
|
49
49
|
assert_equal "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31", step1
|
50
50
|
|
51
51
|
step2 = "00" + step1
|
52
52
|
assert_equal "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31", step2
|
53
53
|
|
54
|
-
addr = base58check( step2 )
|
54
|
+
addr = Crypto.base58check( hex: step2 )
|
55
55
|
assert_equal "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs", addr
|
56
56
|
end
|
57
57
|
|
data/test/test_hash.rb
CHANGED
@@ -24,22 +24,28 @@ class TestHash < MiniTest::Test
|
|
24
24
|
]
|
25
25
|
|
26
26
|
|
27
|
+
def assert_hexdigest( exp, bin )
|
28
|
+
assert_equal exp, bin.hexdigest
|
29
|
+
end
|
30
|
+
|
31
|
+
|
27
32
|
|
28
33
|
def test_keccak256
|
29
34
|
KECCAK256_TESTS.each do |item|
|
30
|
-
|
35
|
+
assert_hexdigest item[1], Crypto.keccak256( item[0].b )
|
31
36
|
end
|
32
37
|
end
|
33
38
|
|
34
39
|
def test_sha3_256
|
35
40
|
SHA3_256_TESTS.each do |item|
|
36
|
-
|
41
|
+
assert_hexdigest item[1], Crypto.sha3_256( item[0].b )
|
37
42
|
end
|
38
43
|
end
|
39
44
|
|
40
45
|
def test_ripemd160
|
41
46
|
RIPEMD160_TESTS.each do |item|
|
42
|
-
|
47
|
+
assert_hexdigest item[1], Crypto.ripemd160( item[0].b )
|
48
|
+
assert_hexdigest item[1], Crypto.rmd160( item[0].b )
|
43
49
|
end
|
44
50
|
end
|
45
51
|
|
data/test/test_hash_sha.rb
CHANGED
@@ -30,10 +30,17 @@ class TestHashSha < MiniTest::Test
|
|
30
30
|
]
|
31
31
|
|
32
32
|
|
33
|
+
|
34
|
+
def assert_hexdigest( exp, bin )
|
35
|
+
assert_equal exp, bin.hexdigest
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
33
40
|
def test_bin
|
34
41
|
BIN_TESTS.each do |item|
|
35
|
-
|
36
|
-
|
42
|
+
assert_hexdigest item[1], Crypto.sha256( item[0].b )
|
43
|
+
assert_hexdigest item[1], Crypto.sha256( item[0].b, engine: 'openssl' )
|
37
44
|
end
|
38
45
|
end
|
39
46
|
|
@@ -42,17 +49,17 @@ def test_abc
|
|
42
49
|
['abc',
|
43
50
|
'abc'.b,
|
44
51
|
"\x61\x62\x63",
|
45
|
-
0x616263
|
52
|
+
# 0x616263
|
46
53
|
].each do |input|
|
47
|
-
|
48
|
-
|
54
|
+
assert_hexdigest SHA256_ABC, Crypto.sha256( input )
|
55
|
+
assert_hexdigest SHA256_ABC, Crypto.sha256( input, engine: 'openssl' )
|
49
56
|
end
|
50
57
|
|
51
58
|
['616263',
|
52
59
|
'0x616263',
|
53
60
|
'0X616263'
|
54
61
|
].each do |input|
|
55
|
-
|
62
|
+
assert_hexdigest SHA256_ABC, Crypto.sha256( hex: input )
|
56
63
|
end
|
57
64
|
## pp sha256hex( 'hello' ) -- fails - uses non-hex chars
|
58
65
|
|
@@ -60,30 +67,30 @@ def test_abc
|
|
60
67
|
|
61
68
|
[ 'a',
|
62
69
|
"\x61",
|
63
|
-
0b01100001,
|
64
|
-
0x61
|
70
|
+
# 0b01100001,
|
71
|
+
# 0x61
|
65
72
|
].each do |input|
|
66
|
-
|
67
|
-
|
73
|
+
assert_hexdigest SHA256_A, Crypto.sha256( input )
|
74
|
+
assert_hexdigest SHA256_A, Crypto.sha256( input, engine: 'openssl' )
|
68
75
|
end
|
69
76
|
|
70
77
|
['61',
|
71
78
|
'0x61',
|
72
79
|
'0X61'
|
73
80
|
].each do |input|
|
74
|
-
|
81
|
+
assert_hexdigest SHA256_A, Crypto.sha256( hex: input )
|
75
82
|
end
|
76
83
|
|
77
84
|
[ 'b',
|
78
|
-
0b01100010
|
85
|
+
# 0b01100010
|
79
86
|
].each do |input|
|
80
|
-
|
87
|
+
assert_hexdigest SHA256_B, Crypto.sha256( input )
|
81
88
|
end
|
82
89
|
|
83
90
|
[ 'c',
|
84
|
-
0b01100011
|
91
|
+
# 0b01100011
|
85
92
|
].each do |input|
|
86
|
-
|
93
|
+
assert_hexdigest SHA256_C, Crypto.sha256( input )
|
87
94
|
end
|
88
95
|
end
|
89
96
|
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crypto-lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-12-
|
11
|
+
date: 2022-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bytes
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: digest-lite
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|