crypto-lite 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7c97262d69928c8f9b51816fcf3fff1f5bc61ba5bf49bc81d011ec9567be877
4
- data.tar.gz: bfb41f345684cc2f27256e64d95f4a4535b908784f6353643bfef251c83262b7
3
+ metadata.gz: 8380b7856889a9c184423b31a39b84dff8197bcb34a58687cd8d496bf7ded819
4
+ data.tar.gz: b91ff4cb4baaa9a7de69224e99585ad5efa6491bb2d8e54adf36841675dfd00c
5
5
  SHA512:
6
- metadata.gz: 450adb10806e9944fafc4fdf04bb3b09a59fa6fc20ac62a8c9b48b780711ef23555174765b7f545ded98607185bad5488f116a691b34d253de7b7cd30d84fd62
7
- data.tar.gz: 524b2c9ae838156c20eb313a045057974e51339e912c48f6945864429ecfd958a7cc8236a83fbbd0121ebc119c2b4a0e76b4b55383b9e64e9ccee35363e912ea
6
+ metadata.gz: 37c7deea2e4badbeb25abd89e4df6ad98f9503c5b8c7fc22f14bd141d2036d97aa33b2fd9c00f538490c9eb6e5eb4fa554199574948b893691c5bf1dfab34fd0
7
+ data.tar.gz: 8727fbeea1fa393124ff0d302d1e0f11cc88517424cefba0e809c1f7d1685a758cf0e60eff2d978355da158fd4cb55bea4190f0368eae327d43124ef7d6fe7b6
@@ -7,5 +7,6 @@ lib/crypto-lite/version.rb
7
7
  lib/crypto.rb
8
8
  lib/crypto/lite.rb
9
9
  test/helper.rb
10
- test/test_hash.rb
10
+ test/test_hash_keccak.rb
11
+ test/test_hash_sha.rb
11
12
  test/test_version.rb
data/README.md CHANGED
@@ -12,21 +12,21 @@
12
12
 
13
13
  ### Secure Hashing / Hash Functions
14
14
 
15
- SHA256 - Secure Hash Algorithm (SHA) 256-bits (32 bytes)
15
+ **SHA256 - Secure Hash Algorithm (SHA) 256-Bits (32 Bytes)**
16
16
 
17
17
 
18
18
  ``` ruby
19
19
  require 'crypto' ## or use require 'crypto-lite'
20
20
 
21
21
  ## try abc
22
- sha256( "abc" ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
23
- sha256( "abc".b ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
24
- sha256( "\x61\x62\x63" ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
25
- sha256( 0x616263 ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
22
+ sha256( "abc" ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
23
+ sha256( "abc".b ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
24
+ sha256( "\x61\x62\x63" ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
25
+ sha256( 0x616263 ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
26
26
 
27
- pp sha256hex( '616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
28
- pp sha256hex( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
29
- pp sha256hex( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
27
+ sha256hex( '616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
28
+ sha256hex( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
29
+ sha256hex( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
30
30
 
31
31
 
32
32
  ## try a
@@ -45,10 +45,64 @@ sha256( "Hello, Cryptos!" ) #=> "33eedea60b0662c66c289ceba71863a864cf84b00e1000
45
45
 
46
46
 
47
47
 
48
+ **Keccak 256-Bit**
49
+
50
+ ``` ruby
51
+ keccak256( "Hello, Cryptos!" ) #=> "2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5"
52
+ ```
53
+
54
+
55
+ **RMD - RACE¹ Integrity Primitives Evaluation Message Digest 160-Bit**
56
+
57
+ ¹: Research and development in Advanced Communications technologies in Europe
58
+
59
+
60
+ ``` ruby
61
+ rmd160( "Hello, Cryptos!" ) #=>"4d65f7b740bbade4097e1348e15d2a7d52ac5f53"
62
+ ```
63
+
64
+
65
+
66
+ #### Helpers
67
+
68
+ **HASH160 - RMD160(SHA256())**
69
+
70
+ All-in-one "best-of-both-worlds" helper - first hash with sha256 and than hash with rmd160. Why? Get the higher security of sha256 and the smaller size of rmd160.
71
+
72
+
73
+ ``` ruby
74
+ hash160hex( '02b9d1cc0b793b03b9f64d022e9c67d5f32670b03f636abf0b3147b34123d13990' )
75
+ => "e6b145a3908a4d6616b13c1109717add8672c900"
76
+
77
+ hash160hex( '02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737' )
78
+ => "93ce48570b55c42c2af816aeaba06cfee1224fae"
79
+ ```
80
+
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
+
85
+
86
+ **HASH256 - SHA256(SHA256())**
87
+
88
+ All-in-one double sha256 hash helper, that is, first hash with sha256 and than hash with sha256 again. Why? Arguably higher security.
89
+
90
+ ``` ruby
91
+ hash256hex( '6fe6b145a3908a4d6616b13c1109717add8672c900' )
92
+ => "02335f08b8fe4ddad263a50b7a33c5d38ea1cbd8fd2056a1320a3ddece541711"
93
+ ```
94
+
95
+
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
+
101
+
48
102
 
49
103
  ### Public Key Signature Algorithms
50
104
 
51
- RSA - Rivest-Shamir-Adleman
105
+ **RSA - Rivest, Shamir and Adleman**
52
106
 
53
107
 
54
108
 
data/Rakefile CHANGED
@@ -19,6 +19,7 @@ Hoe.spec 'crypto-lite' do
19
19
  self.history_file = 'CHANGELOG.md'
20
20
 
21
21
  self.extra_deps = [
22
+ ['digest-sha3-patched']
22
23
  ]
23
24
 
24
25
  self.licenses = ['Public Domain']
@@ -3,6 +3,15 @@ require 'digest'
3
3
  require 'base64'
4
4
  require 'openssl'
5
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
+
12
+
13
+
14
+
6
15
 
7
16
  ## our own code
8
17
  require 'crypto-lite/version' # note: let version always go first
@@ -11,7 +20,8 @@ require 'crypto-lite/version' # note: let version always go first
11
20
 
12
21
  module Crypto
13
22
 
14
- def self.sha256bin( input, engine=nil ) ## todo/check: add alias sha256b or such to - why? why not?
23
+
24
+ def self.message( input ) ## convert input to (binary) string
15
25
  input_type = if input.is_a?( String )
16
26
  "#{input.class.name}/#{input.encoding}"
17
27
  else
@@ -28,13 +38,42 @@ module Crypto
28
38
  input
29
39
  end
30
40
 
41
+ bytes = message.bytes
42
+ bin = bytes.map {|byte| byte.to_s(2).rjust(8, "0")}.join( ' ' )
43
+ hex = bytes.map {|byte| byte.to_s(16).rjust(2, "0")}.join( ' ' )
44
+ puts " #{pluralize( bytes.size, 'byte')}: #{bytes.inspect}"
45
+ puts " binary: #{bin}"
46
+ puts " hex: #{hex}"
47
+
48
+ message
49
+ end
50
+
31
51
 
32
- bytes = message.bytes
33
- bin = bytes.map {|byte| byte.to_s(2).rjust(8, "0")}.join( ' ' )
34
- hex = bytes.map {|byte| byte.to_s(16).rjust(2, "0")}.join( ' ' )
35
- puts " #{pluralize( bytes.size, 'byte')}: #{bytes.inspect}"
36
- puts " binary: #{bin}"
37
- puts " hex: #{hex}"
52
+
53
+ def self.keccak256bin( input )
54
+ message = message( input ) ## "normalize" / convert to (binary) string
55
+ Digest::SHA3.digest( message, 256 )
56
+ end
57
+
58
+ def self.keccak256( input )
59
+ keccak256bin( input ).unpack( 'H*' )[0]
60
+ end
61
+
62
+
63
+ def self.rmd160bin( input )
64
+ message = message( input ) ## "normalize" / convert to (binary) string
65
+ Digest::RMD160.digest( message )
66
+ end
67
+
68
+ def self.rmd160( input )
69
+ rmd160bin( input ).unpack( 'H*' )[0]
70
+ end
71
+ ## todo/fix: add alias RIPEMD160 - why? why not?
72
+
73
+
74
+
75
+ def self.sha256bin( input, engine=nil ) ## todo/check: add alias sha256b or such to - why? why not?
76
+ message = message( input ) ## "normalize" / convert to (binary) string
38
77
 
39
78
  if engine && ['openssl'].include?( engine.to_s.downcase )
40
79
  puts " engine: #{engine}"
@@ -65,6 +104,44 @@ module Crypto
65
104
 
66
105
 
67
106
 
107
+ ####
108
+ ## helper
109
+ # def hash160( pubkey )
110
+ # binary = [pubkey].pack( "H*" ) # Convert to binary first before hashing
111
+ # sha256 = Digest::SHA256.digest( binary )
112
+ # ripemd160 = Digest::RMD160.digest( sha256 )
113
+ # ripemd160.unpack( "H*" )[0] # Convert back to hex
114
+ # end
115
+
116
+ def self.hash160hex( input )
117
+ ## convenience helper - lets you pass in hex string
118
+
119
+ ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
120
+ if input.start_with?( '0x') || input.start_with?( '0X' )
121
+ input = input[2..-1]
122
+ end
123
+
124
+ raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" if input.downcase =~ /[^0-9a-f]/
125
+ sha256bin = sha256bin( [input].pack( 'H*' ) )
126
+ rmd160 = rmd160bin( sha256bin ).unpack( "H*" )[0]
127
+ rmd160
128
+ end
129
+
130
+ def self.hash256hex( input )
131
+ ## convenience helper - lets you pass in hex string
132
+
133
+ ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
134
+ if input.start_with?( '0x') || input.start_with?( '0X' )
135
+ input = input[2..-1]
136
+ end
137
+
138
+ raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" if input.downcase =~ /[^0-9a-f]/
139
+ sha256bin = sha256bin( [input].pack( 'H*' ) )
140
+ sha256 = sha256bin( sha256bin ).unpack( "H*" )[0]
141
+ sha256
142
+ end
143
+
144
+
68
145
  def self.pluralize( count, noun )
69
146
  count == 1 ? "#{count} #{noun}" : "#{count} #{noun}s"
70
147
  end
@@ -105,6 +182,14 @@ end # module Crypto
105
182
  def sha256( input, engine=nil ) Crypto.sha256( input, engine ); end
106
183
  def sha256hex( input, engine=nil ) Crypto.sha256hex( input, engine ); end
107
184
 
185
+ def keccak256( input ) Crypto.keccak256( input ); end
186
+
187
+ def rmd160( input ) Crypto.rmd160( input ); end
188
+
189
+ def hash160hex( input ) Crypto.hash160hex( input ); end
190
+ def hash256hex( input ) Crypto.hash256hex( input ); end
191
+
192
+
108
193
  RSA = Crypto::RSA
109
194
 
110
195
 
@@ -2,7 +2,7 @@
2
2
  module CryptoLite
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 1
5
+ MINOR = 2
6
6
  PATCH = 0
7
7
  VERSION = [MAJOR,MINOR,PATCH].join('.')
8
8
 
@@ -0,0 +1,25 @@
1
+ ###
2
+ # to run use
3
+ # ruby -I ./lib -I ./test test/test_hash_keccak.rb
4
+
5
+
6
+ require 'helper'
7
+
8
+
9
+ class TestHashKeccak < MiniTest::Test
10
+
11
+ KECCAK256_EMPTY = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
12
+ KECCAK256_TESTING = '5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02'
13
+ KECCAK256_HELLO_CRYPTOS = '2cf14baa817e931f5cc2dcb63c889619d6b7ae0794fc2223ebadf8e672c776f5'
14
+
15
+
16
+ def test_empty
17
+ assert_equal KECCAK256_EMPTY, keccak256( '' )
18
+ end
19
+
20
+ def test_misc
21
+ assert_equal KECCAK256_TESTING, keccak256( 'testing' )
22
+ assert_equal KECCAK256_HELLO_CRYPTOS, keccak256( 'Hello, Cryptos!' )
23
+ end
24
+
25
+ end # class TestHashKeccak
@@ -1,6 +1,6 @@
1
1
  ###
2
2
  # to run use
3
- # ruby -I ./lib -I ./test test/test_hash.rb
3
+ # ruby -I ./lib -I ./test test/test_hash_sha.rb
4
4
 
5
5
 
6
6
  require 'helper'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crypto-lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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-14 00:00:00.000000000 Z
11
+ date: 2021-01-15 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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rdoc
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -63,7 +77,8 @@ files:
63
77
  - lib/crypto.rb
64
78
  - lib/crypto/lite.rb
65
79
  - test/helper.rb
66
- - test/test_hash.rb
80
+ - test/test_hash_keccak.rb
81
+ - test/test_hash_sha.rb
67
82
  - test/test_version.rb
68
83
  homepage: https://github.com/rubycoco/blockchain
69
84
  licenses: