crypto-lite 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/crypto-lite.rb CHANGED
@@ -1,148 +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
- 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 '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