crypto-lite 0.2.0 → 0.2.1
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 -10
- data/lib/crypto-lite.rb +80 -34
- data/lib/crypto-lite/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d361cfd5f45ee36170d5efa9e63194a453dc8401334f8eb9db47d15344c74a9b
|
4
|
+
data.tar.gz: 40de7d4721e87e02b8157b0668814a91a75ef3ebd4d5d202e2b0d4085fd52e59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6543504e262d4ea380e529abd15e6789d1cb19988dec206028ad2f715b4ad556838e00c01dd865378a59dc3da2a602371a00a4e7d32db93d374233525be836a
|
7
|
+
data.tar.gz: f541ff370e6a134d5f84a65cc5381d75d8016f9e0ac16d927695507726809d71532acd4ae0b799ab57febf8ee288ac2fdcecf63b3ee7bb9ac17a0e350ac0c8c5
|
data/README.md
CHANGED
@@ -28,6 +28,10 @@ sha256hex( '616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb
|
|
28
28
|
sha256hex( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
29
29
|
sha256hex( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
30
30
|
|
31
|
+
# "auto-magic" hex string to binary string conversion heuristic
|
32
|
+
sha256( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
33
|
+
sha256( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
34
|
+
|
31
35
|
|
32
36
|
## try a
|
33
37
|
sha256( "a" ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
|
@@ -38,6 +42,9 @@ sha256( 0x61 ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807
|
|
38
42
|
sha256hex( '61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
|
39
43
|
sha256hex( '0x61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
|
40
44
|
|
45
|
+
# "auto-magic" hex string to binary string conversion heuristic
|
46
|
+
sha256( '0x61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
|
47
|
+
|
41
48
|
|
42
49
|
## try some more
|
43
50
|
sha256( "Hello, Cryptos!" ) #=> "33eedea60b0662c66c289ceba71863a864cf84b00e10002ca1069bf58f9362d5"
|
@@ -45,6 +52,76 @@ sha256( "Hello, Cryptos!" ) #=> "33eedea60b0662c66c289ceba71863a864cf84b00e1000
|
|
45
52
|
|
46
53
|
|
47
54
|
|
55
|
+
#### Aside - Hex String `'0x616263'` vs Binary String `'abc' == "\x61\x62\x63"`
|
56
|
+
|
57
|
+
Note: All hash functions operate on binary strings ("byte arrays")
|
58
|
+
and NOT hex strings.
|
59
|
+
|
60
|
+
Note: For hex strings the `0x` or `0X` prefix is optional.
|
61
|
+
Examples of hex strings:
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
# hex string binary string ("byte array")
|
65
|
+
'61' 'a' == "\x61"
|
66
|
+
'0x61' 'a' == "\x61"
|
67
|
+
|
68
|
+
'616263' 'abc' == "\x61\x62\x63"
|
69
|
+
'0x616263' 'abc' == "\x61\x62\x63"
|
70
|
+
'0X616263' 'abc' == "\x61\x62\x63"
|
71
|
+
|
72
|
+
# or 160-bit hex string (hash)
|
73
|
+
'93ce48570b55c42c2af816aeaba06cfee1224fae'
|
74
|
+
'0x93ce48570b55c42c2af816aeaba06cfee1224fae'
|
75
|
+
|
76
|
+
# or 256-bit hex string (hash)
|
77
|
+
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
|
78
|
+
'0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
|
79
|
+
```
|
80
|
+
|
81
|
+
You can use `[str].pack( 'H*' )`
|
82
|
+
to convert a hex string into a binary string.
|
83
|
+
Note: The standard `Array#pack` conversion
|
84
|
+
will NOT "auto-magically" cut-off the `0x` or `0X` prefix.
|
85
|
+
|
86
|
+
|
87
|
+
If you know you have a hex string use the hex-variant of the hash function that will handle the hex-to-bin conversion for you. Example:
|
88
|
+
|
89
|
+
``` ruby
|
90
|
+
sha256hex( '61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
|
91
|
+
sha256hex( '0x61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
|
92
|
+
|
93
|
+
sha256hex( '616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
94
|
+
sha256hex( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
95
|
+
sha256hex( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
96
|
+
```
|
97
|
+
|
98
|
+
What about the built-in "auto-magic" hex-to-bin conversion / heuristic?
|
99
|
+
|
100
|
+
Yes, if your passed in string starts with the
|
101
|
+
the `0x` or `0X` prefix the string gets "auto-magically" converted
|
102
|
+
to binary. Or if your passed in string is all hexadecimal characters,
|
103
|
+
that is, `0-9` and `a-f` and has a minimum length of ten characters.
|
104
|
+
Example:
|
105
|
+
|
106
|
+
|
107
|
+
``` ruby
|
108
|
+
# "auto-magic" hex string to binary string conversion heuristic
|
109
|
+
|
110
|
+
sha256( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
111
|
+
sha256( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
112
|
+
|
113
|
+
# or without 0x or 0X BUT with minimum heuristic length
|
114
|
+
hash160( '02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737' )
|
115
|
+
#=> "93ce48570b55c42c2af816aeaba06cfee1224fae"
|
116
|
+
|
117
|
+
hash256( '6fe6b145a3908a4d6616b13c1109717add8672c900' )
|
118
|
+
#=> "02335f08b8fe4ddad263a50b7a33c5d38ea1cbd8fd2056a1320a3ddece541711"
|
119
|
+
|
120
|
+
# and so on
|
121
|
+
```
|
122
|
+
|
123
|
+
|
124
|
+
|
48
125
|
**Keccak 256-Bit**
|
49
126
|
|
50
127
|
``` ruby
|
@@ -71,16 +148,13 @@ All-in-one "best-of-both-worlds" helper - first hash with sha256 and than hash w
|
|
71
148
|
|
72
149
|
|
73
150
|
``` ruby
|
74
|
-
|
151
|
+
hash160( '02b9d1cc0b793b03b9f64d022e9c67d5f32670b03f636abf0b3147b34123d13990' )
|
75
152
|
=> "e6b145a3908a4d6616b13c1109717add8672c900"
|
76
153
|
|
77
|
-
|
154
|
+
hash160( '02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737' )
|
78
155
|
=> "93ce48570b55c42c2af816aeaba06cfee1224fae"
|
79
156
|
```
|
80
157
|
|
81
|
-
Why hex? The input string is a hex string.
|
82
|
-
Note: It's optional to start a hex string with `0x` or `0X`.
|
83
|
-
|
84
158
|
|
85
159
|
|
86
160
|
**HASH256 - SHA256(SHA256())**
|
@@ -88,15 +162,11 @@ Note: It's optional to start a hex string with `0x` or `0X`.
|
|
88
162
|
All-in-one double sha256 hash helper, that is, first hash with sha256 and than hash with sha256 again. Why? Arguably higher security.
|
89
163
|
|
90
164
|
``` ruby
|
91
|
-
|
165
|
+
hash256( '6fe6b145a3908a4d6616b13c1109717add8672c900' )
|
92
166
|
=> "02335f08b8fe4ddad263a50b7a33c5d38ea1cbd8fd2056a1320a3ddece541711"
|
93
167
|
```
|
94
168
|
|
95
169
|
|
96
|
-
Why hex? The input string is a hex string.
|
97
|
-
Note: It's optional to start a hex string with `0x` or `0X`.
|
98
|
-
|
99
|
-
|
100
170
|
|
101
171
|
|
102
172
|
|
data/lib/crypto-lite.rb
CHANGED
@@ -10,9 +10,6 @@ require 'digest/sha3' # e.g. keccak (original submission/proposal NOT official
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
13
|
## our own code
|
17
14
|
require 'crypto-lite/version' # note: let version always go first
|
18
15
|
|
@@ -20,6 +17,11 @@ require 'crypto-lite/version' # note: let version always go first
|
|
20
17
|
|
21
18
|
module Crypto
|
22
19
|
|
20
|
+
## check if it is a hex (string)
|
21
|
+
## - allow optiona 0x or 0X and allow abcdef and ABCDEF
|
22
|
+
HEX_RE = /\A(?:0x)?[0-9a-f]+\z/i
|
23
|
+
|
24
|
+
|
23
25
|
|
24
26
|
def self.message( input ) ## convert input to (binary) string
|
25
27
|
input_type = if input.is_a?( String )
|
@@ -56,16 +58,19 @@ module Crypto
|
|
56
58
|
end
|
57
59
|
|
58
60
|
def self.keccak256( input )
|
61
|
+
input = hex_to_bin_automagic( input ) ## add automagic hex (string) to bin (string) check - why? why not?
|
59
62
|
keccak256bin( input ).unpack( 'H*' )[0]
|
60
63
|
end
|
61
64
|
|
62
65
|
|
66
|
+
|
63
67
|
def self.rmd160bin( input )
|
64
68
|
message = message( input ) ## "normalize" / convert to (binary) string
|
65
69
|
Digest::RMD160.digest( message )
|
66
70
|
end
|
67
71
|
|
68
72
|
def self.rmd160( input )
|
73
|
+
input = hex_to_bin_automagic( input ) ## add automagic hex (string) to bin (string) check - why? why not?
|
69
74
|
rmd160bin( input ).unpack( 'H*' )[0]
|
70
75
|
end
|
71
76
|
## todo/fix: add alias RIPEMD160 - why? why not?
|
@@ -86,20 +91,16 @@ module Crypto
|
|
86
91
|
end
|
87
92
|
|
88
93
|
def self.sha256( input, engine=nil )
|
94
|
+
input = hex_to_bin_automagic( input ) ## add automagic hex (string) to bin (string) check - why? why not?
|
89
95
|
sha256bin( input, engine ).unpack( 'H*' )[0]
|
90
96
|
end
|
91
97
|
|
92
98
|
|
93
|
-
def self.sha256hex( input, engine=nil )
|
94
|
-
|
95
|
-
|
96
|
-
## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
|
97
|
-
if input.start_with?( '0x') || input.start_with?( '0X' )
|
98
|
-
input = input[2..-1]
|
99
|
-
end
|
99
|
+
def self.sha256hex( input, engine=nil ) ## convenience helper - lets you pass in hex string
|
100
|
+
raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" unless input =~ HEX_RE
|
100
101
|
|
101
|
-
|
102
|
-
|
102
|
+
input = strip0x( input ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
|
103
|
+
sha256bin( [input].pack( 'H*' ), engine ).unpack( 'H*' )[0]
|
103
104
|
end
|
104
105
|
|
105
106
|
|
@@ -113,34 +114,75 @@ module Crypto
|
|
113
114
|
# ripemd160.unpack( "H*" )[0] # Convert back to hex
|
114
115
|
# end
|
115
116
|
|
116
|
-
def self.
|
117
|
-
|
117
|
+
def self.hash160bin( input )
|
118
|
+
message = message( input ) ## "normalize" / convert to (binary) string
|
119
|
+
|
120
|
+
rmd160bin(sha256bin( message ))
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.hash160( input )
|
124
|
+
input = hex_to_bin_automagic( input ) ## add automagic hex (string) to bin (string) check - why? why not?
|
125
|
+
hash160bin( input ).unpack( 'H*' )[0]
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.hash160hex( input ) ## convenience helper - lets you pass in hex string
|
129
|
+
raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" unless input =~ HEX_RE
|
130
|
+
|
131
|
+
input = strip0x( input ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
|
132
|
+
hash160bin( [input].pack( 'H*' ) ).unpack( 'H*' )[0]
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
def self.hash256bin( input )
|
138
|
+
message = message( input ) ## "normalize" / convert to (binary) string
|
139
|
+
|
140
|
+
sha256bin(sha256bin( message ))
|
141
|
+
end
|
118
142
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
143
|
+
def self.hash256( input )
|
144
|
+
input = hex_to_bin_automagic( input ) ## add automagic hex (string) to bin (string) check - why? why not?
|
145
|
+
hash256bin( input ).unpack( 'H*' )[0]
|
146
|
+
end
|
147
|
+
|
148
|
+
def self.hash256hex( input ) ## convenience helper - lets you pass in hex string
|
149
|
+
raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" unless input =~ HEX_RE
|
123
150
|
|
124
|
-
|
125
|
-
|
126
|
-
rmd160 = rmd160bin( sha256bin ).unpack( "H*" )[0]
|
127
|
-
rmd160
|
151
|
+
input = strip0x( input ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
|
152
|
+
hash256bin( [input].pack( 'H*' ) ).unpack( "H*" )[0]
|
128
153
|
end
|
129
154
|
|
130
|
-
def self.hash256hex( input )
|
131
|
-
## convenience helper - lets you pass in hex string
|
132
155
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
156
|
+
########
|
157
|
+
# more helpers
|
158
|
+
def self.hex_to_bin_automagic( input )
|
159
|
+
## todo/check/fix: add configure setting to turn off automagic - why? why not?
|
160
|
+
if input.is_a?( String ) && input =~ HEX_RE
|
161
|
+
if input[0,2] == '0x' || input[0,2] == '0X'
|
162
|
+
## starting with 0x or 0X always assume hex string for now - why? why not?
|
163
|
+
input = input[2..-1]
|
164
|
+
[input].pack( 'H*' )
|
165
|
+
elsif input.size >= 10
|
166
|
+
## note: hex heuristic!!
|
167
|
+
## for now assumes string MUST have more than 10 digits to qualify!!!
|
168
|
+
[input].pack( 'H*' )
|
169
|
+
else
|
170
|
+
input ## pass through as is!!! (e.g. a, abc, etc.)
|
171
|
+
end
|
172
|
+
else
|
173
|
+
input ## pass through as is
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
137
177
|
|
138
|
-
|
139
|
-
|
140
|
-
sha256 = sha256bin( sha256bin ).unpack( "H*" )[0]
|
141
|
-
sha256
|
178
|
+
def self.strip0x( str ) ## todo/check: add alias e.g. strip_hex_prefix or such - why? why not?
|
179
|
+
(str[0,2] == '0x' || str[0,2] == '0X') ? str[2..-1] : str
|
142
180
|
end
|
143
181
|
|
182
|
+
def self.hex_to_bin( str )
|
183
|
+
str = strip0x( str ) ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
|
184
|
+
[str].pack( 'H*' )
|
185
|
+
end
|
144
186
|
|
145
187
|
def self.pluralize( count, noun )
|
146
188
|
count == 1 ? "#{count} #{noun}" : "#{count} #{noun}s"
|
@@ -148,6 +190,7 @@ module Crypto
|
|
148
190
|
|
149
191
|
|
150
192
|
|
193
|
+
|
151
194
|
module RSA
|
152
195
|
def self.generate_keys ## todo/check: add a generate alias - why? why not?
|
153
196
|
key_pair = OpenSSL::PKey::RSA.new( 2048 )
|
@@ -186,8 +229,11 @@ def keccak256( input ) Crypto.keccak256( input ); end
|
|
186
229
|
|
187
230
|
def rmd160( input ) Crypto.rmd160( input ); end
|
188
231
|
|
189
|
-
def
|
190
|
-
def
|
232
|
+
def hash160( input ) Crypto.hash160( input ); end
|
233
|
+
def hash160hex( input ) Crypto.hash160hex( input ); end
|
234
|
+
|
235
|
+
def hash256( input ) Crypto.hash256( input ); end
|
236
|
+
def hash256hex( input ) Crypto.hash256hex( input ); end
|
191
237
|
|
192
238
|
|
193
239
|
RSA = Crypto::RSA
|
data/lib/crypto-lite/version.rb
CHANGED