crypto-lite 0.2.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/crypto-lite.rb CHANGED
@@ -1,147 +1,145 @@
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
-
15
-
16
-
17
- ## our own code
18
- require 'crypto-lite/version' # note: let version always go first
19
- require 'crypto-lite/config'
20
- require 'crypto-lite/metal'
21
-
22
-
23
-
24
-
25
- module Crypto
26
-
27
- def self.base58( *args, **kwargs )
28
- input = args_to_input( args, kwargs )
29
- Metal.base58bin( input )
30
- end
31
-
32
- def self.base58check( *args, **kwargs )
33
- input = args_to_input( args, kwargs )
34
- Metal.base58bin_check( input )
35
- end
36
-
37
-
38
- ########################
39
- # (secure) hash functions
40
-
41
- def self.keccak256( *args, **kwargs )
42
- input = args_to_input( args, kwargs )
43
- Metal.keccak256bin( input ).unpack( 'H*' )[0]
44
- end
45
-
46
-
47
- def self.rmd160( *args, **kwargs )
48
- input = args_to_input( args, kwargs )
49
- Metal.rmd160bin( input ).unpack( 'H*' )[0]
50
- end
51
-
52
- ## add alias RIPEMD160 - why? why not?
53
- class << self
54
- alias_method :ripemd160, :rmd160
55
- end
56
-
57
-
58
- def self.sha256( *args, **kwargs )
59
- input = args_to_input( args, kwargs )
60
- engine = kwargs[:engine]
61
- Metal.sha256bin( input, engine ).unpack( 'H*' )[0]
62
- end
63
-
64
- def self.sha3_256( *args, **kwargs )
65
- input = args_to_input( args, kwargs )
66
- Metal.sha3_256bin( input ).unpack( 'H*' )[0]
67
- end
68
-
69
-
70
-
71
- def self.hash160( *args, **kwargs )
72
- input = args_to_input( args, kwargs )
73
- Metal.hash160bin( input ).unpack( 'H*' )[0]
74
- end
75
-
76
- def self.hash256( *args, **kwargs )
77
- input = args_to_input( args, kwargs )
78
- Metal.hash256bin( input ).unpack( 'H*' )[0]
79
- end
80
-
81
-
82
-
83
- ########
84
- # more helpers
85
- ## check if it is a hex (string)
86
- ## - allow optiona 0x or 0X and allow abcdef and ABCDEF
87
- HEX_RE = /\A(?:0x)?[0-9a-f]+\z/i
88
-
89
- def self.args_to_input( args, kwargs )
90
- if kwargs[:hex]
91
- hex = kwargs[:hex]
92
- raise ArgumentError, "expected hex string (0-9a-f) - got >#{hex}< - can't pack string; sorry" unless hex =~ HEX_RE
93
-
94
- hex = strip0x( hex ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
95
- [hex].pack( 'H*' )
96
- else ## assume single input arg for now
97
- input = args[0]
98
- input = hex_to_bin_automagic( input ) ## add automagic hex (string) to bin (string) check - why? why not?
99
- input
100
- end
101
- end
102
-
103
- def self.hex_to_bin_automagic( input )
104
- ## todo/check/fix: add configure setting to turn off automagic - why? why not?
105
- if input.is_a?( String ) && input =~ HEX_RE
106
- if input[0,2] == '0x' || input[0,2] == '0X'
107
- ## starting with 0x or 0X always assume hex string for now - why? why not?
108
- input = input[2..-1]
109
- [input].pack( 'H*' )
110
- elsif input.size >= 10
111
- ## note: hex heuristic!!
112
- ## for now assumes string MUST have more than 10 digits to qualify!!!
113
- [input].pack( 'H*' )
114
- else
115
- input ## pass through as is!!! (e.g. a, abc, etc.)
116
- end
117
- else
118
- input ## pass through as is
119
- end
120
- end
121
-
122
-
123
- def self.strip0x( str ) ## todo/check: add alias e.g. strip_hex_prefix or such - why? why not?
124
- (str[0,2] == '0x' || str[0,2] == '0X') ? str[2..-1] : str
125
- end
126
-
127
- # def self.hex_to_bin( str )
128
- # str = strip0x( str ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
129
- # [str].pack( 'H*' )
130
- # end
131
-
132
- end # module Crypto
133
-
134
-
135
-
136
-
137
- require 'crypto-lite/helper'
138
- include CryptoHelper # add convenience "top-level" / global helpers
139
-
140
-
141
- require 'crypto-lite/sign_rsa'
142
- RSA = Crypto::RSA
143
-
144
-
145
-
146
-
147
- 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 'digest-lite' # e.g. keccak (original submission/proposal NOT official sha3)
9
+ require 'base32-alphabets'
10
+ require 'base58-alphabets'
11
+ require 'elliptic'
12
+
13
+
14
+
15
+ ## our own code
16
+ require_relative 'crypto-lite/version' # note: let version always go first
17
+ require_relative 'crypto-lite/config'
18
+ 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
+ require_relative 'crypto-lite/helper'
136
+ include CryptoHelper # add convenience "top-level" / global helpers
137
+
138
+
139
+ require_relative 'crypto-lite/sign_rsa'
140
+ RSA = Crypto::RSA
141
+
142
+
143
+
144
+
145
+ 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,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
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,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 = 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
@@ -1,47 +1,47 @@
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
+
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