crypto-lite 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +7 -0
- data/README.md +443 -119
- data/Rakefile +4 -0
- data/lib/crypto-lite.rb +98 -65
- data/lib/crypto-lite/config.rb +32 -0
- data/lib/crypto-lite/helper.rb +25 -0
- data/lib/crypto-lite/metal.rb +128 -0
- data/lib/crypto-lite/sign_rsa.rb +29 -0
- data/lib/crypto-lite/version.rb +1 -1
- data/test/test_base58.rb +36 -0
- data/test/test_bitcoin_addr.rb +58 -0
- data/test/test_hash.rb +24 -55
- data/test/test_hash_sha.rb +87 -0
- metadata +65 -2
data/test/test_base58.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
###
|
2
|
+
# to run use
|
3
|
+
# ruby -I ./lib -I ./test test/test_base58.rb
|
4
|
+
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
|
9
|
+
class TestBase58 < MiniTest::Test
|
10
|
+
|
11
|
+
HEX_TESTS = [
|
12
|
+
["00000000000000000000", "1111111111"],
|
13
|
+
["00000000000000000000123456789abcdef0", "111111111143c9JGph3DZ"],
|
14
|
+
]
|
15
|
+
|
16
|
+
def test_hex
|
17
|
+
HEX_TESTS.each do |item|
|
18
|
+
assert_equal item[1], base58( hex: item[0] )
|
19
|
+
assert_equal item[1], base58( item[0] )
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def test_bitcoin_addr
|
25
|
+
addr_exp = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs'
|
26
|
+
|
27
|
+
pkh = 'f54a5851e9372b87810a8e60cdd2e7cfd80b6e31'
|
28
|
+
|
29
|
+
## all-in-one
|
30
|
+
assert_equal addr_exp, base58check( hex: '00' + pkh )
|
31
|
+
assert_equal addr_exp, base58check( '00' + pkh )
|
32
|
+
|
33
|
+
assert_equal addr_exp, Crypto::Metal.base58bin_check( "\x00" + [pkh].pack('H*') )
|
34
|
+
end
|
35
|
+
|
36
|
+
end # class TestBase58
|
@@ -0,0 +1,58 @@
|
|
1
|
+
###
|
2
|
+
# to run use
|
3
|
+
# ruby -I ./lib -I ./test test/test_bitcoin_addr.rb
|
4
|
+
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
class TestBitcoinAddr < MiniTest::Test
|
9
|
+
|
10
|
+
|
11
|
+
def test_bitcoin_addr_v1
|
12
|
+
pk = "0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
|
13
|
+
|
14
|
+
step1 = sha256( pk )
|
15
|
+
assert_equal "0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98",
|
16
|
+
step1
|
17
|
+
|
18
|
+
step2 = ripemd160( step1 )
|
19
|
+
assert_equal "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31",
|
20
|
+
step2
|
21
|
+
|
22
|
+
step3 = "00" + step2
|
23
|
+
assert_equal "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31",
|
24
|
+
step3
|
25
|
+
|
26
|
+
step4 = sha256( step3 )
|
27
|
+
assert_equal "ad3c854da227c7e99c4abfad4ea41d71311160df2e415e713318c70d67c6b41c",
|
28
|
+
step4
|
29
|
+
|
30
|
+
step5 = sha256( step4 )
|
31
|
+
assert_equal "c7f18fe8fcbed6396741e58ad259b5cb16b7fd7f041904147ba1dcffabf747fd",
|
32
|
+
step5
|
33
|
+
|
34
|
+
step6 = step5[0..7] # note: 4 bytes in hex string are 8 digits/chars
|
35
|
+
assert_equal "c7f18fe8", step6
|
36
|
+
|
37
|
+
step7 = step3 + step6
|
38
|
+
assert_equal "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31c7f18fe8", step7
|
39
|
+
|
40
|
+
addr = base58( step7 )
|
41
|
+
assert_equal "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs", addr
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def test_bitcoin_addr_v2
|
46
|
+
pk = "0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
|
47
|
+
|
48
|
+
step1 = hash160( pk )
|
49
|
+
assert_equal "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31", step1
|
50
|
+
|
51
|
+
step2 = "00" + step1
|
52
|
+
assert_equal "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31", step2
|
53
|
+
|
54
|
+
addr = base58check( step2 )
|
55
|
+
assert_equal "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs", addr
|
56
|
+
end
|
57
|
+
|
58
|
+
end # class TestBitcoinAddr
|
data/test/test_hash.rb
CHANGED
@@ -8,71 +8,40 @@ require 'helper'
|
|
8
8
|
|
9
9
|
class TestHash < MiniTest::Test
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
SHA256_HELLO_CRYPTOS = '33eedea60b0662c66c289ceba71863a864cf84b00e10002ca1069bf58f9362d5'
|
11
|
+
KECCAK256_TESTS = [
|
12
|
+
['', 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'],
|
13
|
+
['testing', '5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02'],
|
14
|
+
['Hello, Cryptos!', '2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5'],
|
15
|
+
]
|
17
16
|
|
17
|
+
SHA3_256_TESTS = [
|
18
|
+
['', 'a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a'],
|
19
|
+
['Hello, Cryptos!', '7dddf4bc9b86352b67e8823e5010ddbd2a90a854469e2517992ca7ca89e5bd58'],
|
20
|
+
]
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_abc
|
25
|
-
['abc',
|
26
|
-
'abc'.b,
|
27
|
-
"\x61\x62\x63",
|
28
|
-
0x616263
|
29
|
-
].each do |input|
|
30
|
-
assert_equal SHA256_ABC, sha256( input )
|
31
|
-
assert_equal SHA256_ABC, sha256( input, :openssl )
|
32
|
-
end
|
22
|
+
RIPEMD160_TESTS = [
|
23
|
+
['Hello, Cryptos!', '4d65f7b740bbade4097e1348e15d2a7d52ac5f53'],
|
24
|
+
]
|
33
25
|
|
34
|
-
['616263',
|
35
|
-
'0x616263',
|
36
|
-
'0X616263'
|
37
|
-
].each do |input|
|
38
|
-
assert_equal SHA256_ABC, sha256hex( input )
|
39
|
-
end
|
40
|
-
## pp sha256hex( 'hello' ) -- fails - uses non-hex chars
|
41
|
-
|
42
|
-
# a = dec (97), hex (61), bin (01100001)
|
43
26
|
|
44
|
-
[ 'a',
|
45
|
-
"\x61",
|
46
|
-
0b01100001,
|
47
|
-
0x61
|
48
|
-
].each do |input|
|
49
|
-
assert_equal SHA256_A, sha256( input )
|
50
|
-
assert_equal SHA256_A, sha256( input, :openssl )
|
51
|
-
end
|
52
27
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
end
|
28
|
+
def test_keccak256
|
29
|
+
KECCAK256_TESTS.each do |item|
|
30
|
+
assert_equal item[1], keccak256( item[0].b )
|
31
|
+
end
|
32
|
+
end
|
59
33
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
assert_equal SHA256_B, sha256( input )
|
34
|
+
def test_sha3_256
|
35
|
+
SHA3_256_TESTS.each do |item|
|
36
|
+
assert_equal item[1], sha3_256( item[0].b )
|
64
37
|
end
|
38
|
+
end
|
65
39
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
assert_equal SHA256_C, sha256( input )
|
40
|
+
def test_ripemd160
|
41
|
+
RIPEMD160_TESTS.each do |item|
|
42
|
+
assert_equal item[1], ripemd160( item[0].b )
|
70
43
|
end
|
71
44
|
end
|
72
45
|
|
73
46
|
|
74
|
-
def test_misc
|
75
|
-
assert_equal SHA256_HELLO_CRYPTOS, sha256( 'Hello, Cryptos!' )
|
76
|
-
end
|
77
|
-
|
78
47
|
end # class TestHash
|
@@ -0,0 +1,87 @@
|
|
1
|
+
###
|
2
|
+
# to run use
|
3
|
+
# ruby -I ./lib -I ./test test/test_hash_sha.rb
|
4
|
+
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
|
9
|
+
class TestHashSha < MiniTest::Test
|
10
|
+
|
11
|
+
SHA256_ABC = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
|
12
|
+
SHA256_A = 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb'
|
13
|
+
SHA256_B = '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d'
|
14
|
+
SHA256_C = '2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6'
|
15
|
+
|
16
|
+
|
17
|
+
BIN_TESTS = [
|
18
|
+
['', 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'],
|
19
|
+
['Hello, Cryptos!', '33eedea60b0662c66c289ceba71863a864cf84b00e10002ca1069bf58f9362d5'],
|
20
|
+
|
21
|
+
['Blockchain 1', '223aae3830e87aff41db07714983671429a74fb7064646e27ab62adbdc892cd1'],
|
22
|
+
['Blockchain 2', '8f4b5a896e78f680de2c141d25533e8c9207c9ee0d9b09c22a5a01d7992e7d1b'],
|
23
|
+
['Blockchain 3', 'f9f5e7d64f3a93f071900a73d34b2b722867adcb09345a21a25b5cd7f8b41254'],
|
24
|
+
['Blockchain 4', '69580cfb9a134395106ac27de53595cc41fb9940dcc298082c87144932a983a9'],
|
25
|
+
['Blockchain 12', 'f94455d34e7b32dc7dac780fb94a5e958f0d7e2da4cc6500505d5daed719c227'],
|
26
|
+
['Blockchain 13', '0cedb5e229c9582a35ec291ccb172b3f7c76a20c7f645e08d24ece8d5692e153'],
|
27
|
+
]
|
28
|
+
|
29
|
+
|
30
|
+
def test_bin
|
31
|
+
BIN_TESTS.each do |item|
|
32
|
+
assert_equal item[1], sha256( item[0].b )
|
33
|
+
assert_equal item[1], sha256( item[0].b, engine: 'openssl' )
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
def test_abc
|
39
|
+
['abc',
|
40
|
+
'abc'.b,
|
41
|
+
"\x61\x62\x63",
|
42
|
+
0x616263
|
43
|
+
].each do |input|
|
44
|
+
assert_equal SHA256_ABC, sha256( input )
|
45
|
+
assert_equal SHA256_ABC, sha256( input, engine: 'openssl' )
|
46
|
+
end
|
47
|
+
|
48
|
+
['616263',
|
49
|
+
'0x616263',
|
50
|
+
'0X616263'
|
51
|
+
].each do |input|
|
52
|
+
assert_equal SHA256_ABC, sha256( hex: input )
|
53
|
+
end
|
54
|
+
## pp sha256hex( 'hello' ) -- fails - uses non-hex chars
|
55
|
+
|
56
|
+
# a = dec (97), hex (61), bin (01100001)
|
57
|
+
|
58
|
+
[ 'a',
|
59
|
+
"\x61",
|
60
|
+
0b01100001,
|
61
|
+
0x61
|
62
|
+
].each do |input|
|
63
|
+
assert_equal SHA256_A, sha256( input )
|
64
|
+
assert_equal SHA256_A, sha256( input, engine: 'openssl' )
|
65
|
+
end
|
66
|
+
|
67
|
+
['61',
|
68
|
+
'0x61',
|
69
|
+
'0X61'
|
70
|
+
].each do |input|
|
71
|
+
assert_equal SHA256_A, sha256( hex: input )
|
72
|
+
end
|
73
|
+
|
74
|
+
[ 'b',
|
75
|
+
0b01100010
|
76
|
+
].each do |input|
|
77
|
+
assert_equal SHA256_B, sha256( input )
|
78
|
+
end
|
79
|
+
|
80
|
+
[ 'c',
|
81
|
+
0b01100011
|
82
|
+
].each do |input|
|
83
|
+
assert_equal SHA256_C, sha256( input )
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end # class TestHashSha
|
metadata
CHANGED
@@ -1,15 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crypto-lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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: 2021-01-
|
11
|
+
date: 2021-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: digest-sha3-patched
|
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'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: base32-alphabets
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: base58-alphabets
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: elliptic
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
13
69
|
- !ruby/object:Gem::Dependency
|
14
70
|
name: rdoc
|
15
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,11 +115,18 @@ files:
|
|
59
115
|
- README.md
|
60
116
|
- Rakefile
|
61
117
|
- lib/crypto-lite.rb
|
118
|
+
- lib/crypto-lite/config.rb
|
119
|
+
- lib/crypto-lite/helper.rb
|
120
|
+
- lib/crypto-lite/metal.rb
|
121
|
+
- lib/crypto-lite/sign_rsa.rb
|
62
122
|
- lib/crypto-lite/version.rb
|
63
123
|
- lib/crypto.rb
|
64
124
|
- lib/crypto/lite.rb
|
65
125
|
- test/helper.rb
|
126
|
+
- test/test_base58.rb
|
127
|
+
- test/test_bitcoin_addr.rb
|
66
128
|
- test/test_hash.rb
|
129
|
+
- test/test_hash_sha.rb
|
67
130
|
- test/test_version.rb
|
68
131
|
homepage: https://github.com/rubycoco/blockchain
|
69
132
|
licenses:
|