crypto-lite 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/crypto-lite.rb CHANGED
@@ -1,148 +1,39 @@
1
- require 'pp'
2
- require 'digest'
3
- require 'base64'
4
- require 'openssl'
5
-
6
- ## 3rd party gems
7
- require 'digest/sha3' # e.g. keccak (original submission/proposal NOT official sha3)
8
- ## see https://rubygems.org/gems/digest-sha3-patched
9
- ## https://github.com/teamhedge/digest-sha3-ruby
10
-
11
- ## our own 3rd party (2nd party?)
12
- require 'base32-alphabets'
13
- require 'base58-alphabets'
14
- require 'elliptic'
15
-
16
-
17
-
18
- ## our own code
19
- require 'crypto-lite/version' # note: let version always go first
20
- require 'crypto-lite/config'
21
- require 'crypto-lite/metal'
22
-
23
-
24
-
25
-
26
- module Crypto
27
-
28
- def self.base58( *args, **kwargs )
29
- input = args_to_input( args, kwargs )
30
- Metal.base58bin( input )
31
- end
32
-
33
- def self.base58check( *args, **kwargs )
34
- input = args_to_input( args, kwargs )
35
- Metal.base58bin_check( input )
36
- end
37
-
38
-
39
- ########################
40
- # (secure) hash functions
41
-
42
- def self.keccak256( *args, **kwargs )
43
- input = args_to_input( args, kwargs )
44
- Metal.keccak256bin( input ).unpack( 'H*' )[0]
45
- end
46
-
47
-
48
- def self.rmd160( *args, **kwargs )
49
- input = args_to_input( args, kwargs )
50
- Metal.rmd160bin( input ).unpack( 'H*' )[0]
51
- end
52
-
53
- ## add alias RIPEMD160 - why? why not?
54
- class << self
55
- alias_method :ripemd160, :rmd160
56
- end
57
-
58
-
59
- def self.sha256( *args, **kwargs )
60
- input = args_to_input( args, kwargs )
61
- engine = kwargs[:engine]
62
- Metal.sha256bin( input, engine ).unpack( 'H*' )[0]
63
- end
64
-
65
- def self.sha3_256( *args, **kwargs )
66
- input = args_to_input( args, kwargs )
67
- Metal.sha3_256bin( input ).unpack( 'H*' )[0]
68
- end
69
-
70
-
71
-
72
- def self.hash160( *args, **kwargs )
73
- input = args_to_input( args, kwargs )
74
- Metal.hash160bin( input ).unpack( 'H*' )[0]
75
- end
76
-
77
- def self.hash256( *args, **kwargs )
78
- input = args_to_input( args, kwargs )
79
- Metal.hash256bin( input ).unpack( 'H*' )[0]
80
- end
81
-
82
-
83
-
84
- ########
85
- # more helpers
86
- ## check if it is a hex (string)
87
- ## - allow optiona 0x or 0X and allow abcdef and ABCDEF
88
- HEX_RE = /\A(?:0x)?[0-9a-f]+\z/i
89
-
90
- def self.args_to_input( args, kwargs )
91
- if kwargs[:hex]
92
- hex = kwargs[:hex]
93
- raise ArgumentError, "expected hex string (0-9a-f) - got >#{hex}< - can't pack string; sorry" unless hex =~ HEX_RE
94
-
95
- hex = strip0x( hex ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
96
- [hex].pack( 'H*' )
97
- else ## assume single input arg for now
98
- input = args[0]
99
- input = hex_to_bin_automagic( input ) ## add automagic hex (string) to bin (string) check - why? why not?
100
- input
101
- end
102
- end
103
-
104
- def self.hex_to_bin_automagic( input )
105
- ## todo/check/fix: add configure setting to turn off automagic - why? why not?
106
- if input.is_a?( String ) && input =~ HEX_RE
107
- if input[0,2] == '0x' || input[0,2] == '0X'
108
- ## starting with 0x or 0X always assume hex string for now - why? why not?
109
- input = input[2..-1]
110
- [input].pack( 'H*' )
111
- elsif input.size >= 10
112
- ## note: hex heuristic!!
113
- ## for now assumes string MUST have more than 10 digits to qualify!!!
114
- [input].pack( 'H*' )
115
- else
116
- input ## pass through as is!!! (e.g. a, abc, etc.)
117
- end
118
- else
119
- input ## pass through as is
120
- end
121
- end
122
-
123
-
124
- def self.strip0x( str ) ## todo/check: add alias e.g. strip_hex_prefix or such - why? why not?
125
- (str[0,2] == '0x' || str[0,2] == '0X') ? str[2..-1] : str
126
- end
127
-
128
- # def self.hex_to_bin( str )
129
- # str = strip0x( str ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
130
- # [str].pack( 'H*' )
131
- # end
132
-
133
- end # module Crypto
134
-
135
-
136
-
137
-
138
- require 'crypto-lite/helper'
139
- include CryptoHelper # add convenience "top-level" / global helpers
140
-
141
-
142
- require 'crypto-lite/sign_rsa'
143
- RSA = Crypto::RSA
144
-
145
-
146
-
147
-
148
- puts CryptoLite.banner ## say hello
1
+ require 'pp'
2
+ require 'digest'
3
+ require 'base64'
4
+ require 'openssl'
5
+
6
+
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
+
16
+ require 'digest-lite' # e.g. keccak (original submission/proposal NOT official sha3)
17
+ require 'base32-alphabets'
18
+ require 'base58-alphabets'
19
+ require 'elliptic'
20
+
21
+
22
+ ## our own code
23
+ require_relative 'crypto-lite/version' # note: let version always go first
24
+ require_relative 'crypto-lite/config'
25
+ require_relative 'crypto-lite/metal'
26
+ require_relative 'crypto-lite/helper'
27
+
28
+
29
+ require_relative 'crypto-lite/sign_rsa'
30
+ RSA = Crypto::RSA
31
+
32
+
33
+ ## auto-add top-level helpers - why? why not?
34
+ include CryptoHelper
35
+
36
+
37
+
38
+
39
+ puts CryptoLite.banner ## say hello
data/lib/crypto.rb CHANGED
@@ -1,2 +1,2 @@
1
- require_relative 'crypto-lite' ## lets you use require 'crypto' too
2
-
1
+ require_relative 'crypto-lite' ## lets you use require 'crypto' too
2
+
data/test/helper.rb CHANGED
@@ -1,11 +1,11 @@
1
- ## $:.unshift(File.dirname(__FILE__))
2
-
3
- ## minitest setup
4
-
5
- require 'minitest/autorun'
6
-
7
-
8
- ## our own code
9
-
10
- require 'crypto-lite'
11
-
1
+ ## $:.unshift(File.dirname(__FILE__))
2
+
3
+ ## minitest setup
4
+
5
+ require 'minitest/autorun'
6
+
7
+
8
+ ## our own code
9
+
10
+ require 'crypto-lite'
11
+
data/test/test_base58.rb CHANGED
@@ -1,36 +1,39 @@
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
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
+
17
+ def test_hex
18
+ HEX_TESTS.each do |item|
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 )
22
+ end
23
+ end
24
+
25
+
26
+ def test_bitcoin_addr
27
+ addr_exp = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs'
28
+
29
+ pkh = 'f54a5851e9372b87810a8e60cdd2e7cfd80b6e31'
30
+
31
+ ## all-in-one
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 )
35
+
36
+ assert_equal addr_exp, Crypto::Metal.base58check( "\x00" + [pkh].pack('H*') )
37
+ end
38
+
39
+ end # class TestBase58
@@ -1,58 +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
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 = Crypto.sha256( hex: pk ).hexdigest
15
+ assert_equal "0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98",
16
+ step1
17
+
18
+ step2 = Crypto.ripemd160( hex: step1 ).hexdigest
19
+ assert_equal "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31",
20
+ step2
21
+
22
+ step3 = "00" + step2
23
+ assert_equal "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31",
24
+ step3
25
+
26
+ step4 = Crypto.sha256( hex: step3 ).hexdigest
27
+ assert_equal "ad3c854da227c7e99c4abfad4ea41d71311160df2e415e713318c70d67c6b41c",
28
+ step4
29
+
30
+ step5 = Crypto.sha256( hex: step4 ).hexdigest
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 = Crypto.base58( hex: step7 )
41
+ assert_equal "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs", addr
42
+ end
43
+
44
+
45
+ def test_bitcoin_addr_v2
46
+ pk = "0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352"
47
+
48
+ step1 = Crypto.hash160( hex: pk ).hexdigest
49
+ assert_equal "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31", step1
50
+
51
+ step2 = "00" + step1
52
+ assert_equal "00f54a5851e9372b87810a8e60cdd2e7cfd80b6e31", step2
53
+
54
+ addr = Crypto.base58check( hex: step2 )
55
+ assert_equal "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs", addr
56
+ end
57
+
58
+ end # class TestBitcoinAddr
data/test/test_hash.rb CHANGED
@@ -1,47 +1,53 @@
1
- ###
2
- # to run use
3
- # ruby -I ./lib -I ./test test/test_hash.rb
4
-
5
-
6
- require 'helper'
7
-
8
-
9
- class TestHash < MiniTest::Test
10
-
11
- KECCAK256_TESTS = [
12
- ['', 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'],
13
- ['testing', '5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02'],
14
- ['Hello, Cryptos!', '2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5'],
15
- ]
16
-
17
- SHA3_256_TESTS = [
18
- ['', 'a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a'],
19
- ['Hello, Cryptos!', '7dddf4bc9b86352b67e8823e5010ddbd2a90a854469e2517992ca7ca89e5bd58'],
20
- ]
21
-
22
- RIPEMD160_TESTS = [
23
- ['Hello, Cryptos!', '4d65f7b740bbade4097e1348e15d2a7d52ac5f53'],
24
- ]
25
-
26
-
27
-
28
- def test_keccak256
29
- KECCAK256_TESTS.each do |item|
30
- assert_equal item[1], keccak256( item[0].b )
31
- end
32
- end
33
-
34
- def test_sha3_256
35
- SHA3_256_TESTS.each do |item|
36
- assert_equal item[1], sha3_256( item[0].b )
37
- end
38
- end
39
-
40
- def test_ripemd160
41
- RIPEMD160_TESTS.each do |item|
42
- assert_equal item[1], ripemd160( item[0].b )
43
- end
44
- end
45
-
46
-
47
- end # class TestHash
1
+ ###
2
+ # to run use
3
+ # ruby -I ./lib -I ./test test/test_hash.rb
4
+
5
+
6
+ require 'helper'
7
+
8
+
9
+ class TestHash < MiniTest::Test
10
+
11
+ KECCAK256_TESTS = [
12
+ ['', 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'],
13
+ ['testing', '5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02'],
14
+ ['Hello, Cryptos!', '2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5'],
15
+ ]
16
+
17
+ SHA3_256_TESTS = [
18
+ ['', 'a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a'],
19
+ ['Hello, Cryptos!', '7dddf4bc9b86352b67e8823e5010ddbd2a90a854469e2517992ca7ca89e5bd58'],
20
+ ]
21
+
22
+ RIPEMD160_TESTS = [
23
+ ['Hello, Cryptos!', '4d65f7b740bbade4097e1348e15d2a7d52ac5f53'],
24
+ ]
25
+
26
+
27
+ def assert_hexdigest( exp, bin )
28
+ assert_equal exp, bin.hexdigest
29
+ end
30
+
31
+
32
+
33
+ def test_keccak256
34
+ KECCAK256_TESTS.each do |item|
35
+ assert_hexdigest item[1], Crypto.keccak256( item[0].b )
36
+ end
37
+ end
38
+
39
+ def test_sha3_256
40
+ SHA3_256_TESTS.each do |item|
41
+ assert_hexdigest item[1], Crypto.sha3_256( item[0].b )
42
+ end
43
+ end
44
+
45
+ def test_ripemd160
46
+ RIPEMD160_TESTS.each do |item|
47
+ assert_hexdigest item[1], Crypto.ripemd160( item[0].b )
48
+ assert_hexdigest item[1], Crypto.rmd160( item[0].b )
49
+ end
50
+ end
51
+
52
+
53
+ end # class TestHash