digest-lite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b97f9380b612a38fadac785f5b716ca86c86aa45fe2c04c61f4894a42bdddd45
4
+ data.tar.gz: 945c6911c0ccba36229fe864b476006f4586bf4cb65d0202dc6801a479cf8a54
5
+ SHA512:
6
+ metadata.gz: cbe48b8857dd2477511736154a6097f8c353b15e0adb9da482ad6e89226d182fe4d5fb926939fc281490f7d1f7adb541c9c6676646148749b08fda69aebd5cbb
7
+ data.tar.gz: efd0c3394b2249dde99b040dbbb1a2c0a0b856c8be11231a05fb234ed22eab0287a627a1ec311443e7d4f8e91c3cf081dfc3c70bce0eb5aec4ce6f928f44ec20
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.0.1 / 2022-12-08
2
+
3
+ * Everything is new. First release
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ CHANGELOG.md
2
+ Manifest.txt
3
+ README.md
4
+ Rakefile
5
+ lib/digest-lite.rb
6
+ lib/digest-lite/keccak.rb
7
+ lib/digest-lite/sha3.rb
8
+ lib/digest-lite/version.rb
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Digest Lite
2
+
3
+
4
+ digest-lite gem - crypto(graphic) hash functions / classes - Digest::KeccakLite (512bit, 256bit, etc), Digest::SHA3Lite (512bit, 256bit, etc) in "100% pure" ruby "lite" scripts, that is, without any c-extensions and with zero-dependency
5
+
6
+
7
+
8
+ * home :: [github.com/rubycoco/blockchain](https://github.com/rubycoco/blockchain)
9
+ * bugs :: [github.com/rubycoco/blockchain/issues](https://github.com/rubycoco/blockchain/issues)
10
+ * gem :: [rubygems.org/gems/digest-lite](https://rubygems.org/gems/digest-lite)
11
+ * rdoc :: [rubydoc.info/gems/digest-lite](http://rubydoc.info/gems/digest-lite)
12
+
13
+
14
+
15
+
16
+ ## Background - Digest - The Standard Built-In Module / Library For Crypto(graphic) Hash Function Algorithms
17
+
18
+ In [Ruby's standard Digest module](https://github.com/ruby/digest) the following crypto(graphic) hash function algorithms / classes
19
+ are available:
20
+
21
+ - MD5 (128 bits / 16 bytes) -- Message-Digest Algorithm by RSA Data Security, Inc., described in RFC1321.
22
+ - RIPEMD-160 / RMD160 (160 bits / 20 bytes) -- RIPEMD-160 cryptographic hash function, designed by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel.
23
+ - SHA1 (160 bits / 20 bytes) -- Secure Hash Algorithm by NIST (the US' National Institute of Standards and Technology), described in FIPS PUB 180-1.
24
+ - SHA2 Family -- described in FIPS 180-2.
25
+ - SHA512 (512 bits / 64 bytes)
26
+ - SHA384 (384 bits / 48 bytes)
27
+ - SHA256 (256 bits / 32 bytes)
28
+
29
+ Example:
30
+
31
+ ``` ruby
32
+ require 'digest'
33
+
34
+ Digest::MD5.hexdigest( 'abc' )
35
+ #=> "900150983cd24fb0d6963f7d28e17f72"
36
+
37
+ Digest::RMD160.hexdigest( 'abc' )
38
+ #=> "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"
39
+
40
+ Digest::SHA1.hexdigest( 'abc' )
41
+ #=> "a9993e364706816aba3e25717850c26c9cd0d89d"
42
+
43
+ Digest::SHA2.new(256).hexdigest( 'abc' ) # or
44
+ Digest::SHA256.hexdigest( 'abc' )
45
+ #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
46
+
47
+ Digest::SHA2.new(384).hexdigest 'abc' # or
48
+ Digest::SHA384.hexdigest 'abc'
49
+ #=> "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"
50
+
51
+ Digest::SHA2.new(512).hexdigest 'abc' # or
52
+ Digest::SHA512.hexdigest 'abc'
53
+ #=> "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
54
+ ```
55
+
56
+
57
+ What about the Keccak or SHA3 (family of hash function algorithms)? Read on.
58
+
59
+
60
+ ## Usage
61
+
62
+
63
+ ``` ruby
64
+ require 'digest-lite'
65
+
66
+ Digest::KeccakLite.new( 256 ).hexdigest( 'abc' ) # or
67
+ Digest::KeccakLite.hexdigest( 'abc', 256 )
68
+ #=> "4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45"
69
+
70
+ Digest::SHA3Lite.new( 224 ).hexdigest( 'abc' ) # or
71
+ Digest::SHA3Lite.hexdigest( 'abc', 224 )
72
+ #=> "e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf"
73
+
74
+ Digest::SHA3Lite.new( 256 ).hexdigest( 'abc' ) # or
75
+ Digest::SHA3Lite.hexdigest( 'abc', 256 )
76
+ #=> "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"
77
+
78
+ Digest::SHA3Lite.new( 384 ).hexdigest( 'abc' ) # or
79
+ Digest::SHA3Lite.hexdigest( 'abc', 384 )
80
+ #=> "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25"
81
+
82
+ Digest::SHA3Lite.new( 512 ).hexdigest( 'abc' ) # or
83
+ Digest::SHA3Lite.hexdigest( 'abc', 512 )
84
+ #=> "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0"
85
+ ```
86
+
87
+
88
+
89
+ Note: To avoid naming conflicts / errors with
90
+ `Digest::Keccak` or `Digest::SHA3` as used in alternate
91
+ digest gems (with or without c-extensions) this gem
92
+ uses `Digest::KeccakLite` or `Digest::SHA3Lite`
93
+ to let you load different (faster) variants
94
+ at the same time.
95
+
96
+
97
+
98
+
99
+
100
+ ## License
101
+
102
+ The scripts are dedicated to the public domain.
103
+ Use it as you please with no restrictions whatsoever.
104
+
105
+
106
+ ## Questions? Comments?
107
+
108
+
109
+ Post them on the [D.I.Y. Punk (Pixel) Art reddit](https://old.reddit.com/r/DIYPunkArt). Thanks.
110
+
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'hoe'
2
+ require './lib/digest-lite/version.rb'
3
+
4
+ Hoe.spec 'digest-lite' do
5
+
6
+ self.version = Digestlite::VERSION
7
+
8
+ self.summary = 'digest-lite - crypto(graphic) hash functions / classes - Digest::KeccakLite (512bit, 256bit, etc), Digest::SHA3Lite (512bit, 256bit, etc) in "100% pure" ruby "lite" scripts, that is, without any c-extensions and with zero-dependency'
9
+ self.description = summary
10
+
11
+ self.urls = { home: 'https://github.com/rubycoco/blockchain' }
12
+
13
+ self.author = 'Gerald Bauer'
14
+ self.email = 'wwwmake@googlegroups.com'
15
+
16
+ # switch extension to .markdown for gihub formatting
17
+ self.readme_file = 'README.md'
18
+ self.history_file = 'CHANGELOG.md'
19
+
20
+ self.extra_deps = [
21
+ ]
22
+
23
+ self.licenses = ['Public Domain']
24
+
25
+ self.spec_extras = {
26
+ required_ruby_version: '>= 2.3'
27
+ }
28
+
29
+ end
@@ -0,0 +1,125 @@
1
+ ###
2
+ # use a "vendor" bundled keccak 256 in all ruby (no c-extensions required)
3
+ #
4
+ # see https://github.com/evtaylor/keccak256
5
+
6
+
7
+
8
+ ## require 'digest'
9
+
10
+
11
+
12
+ ##
13
+ # note: use KeccakLite (or KeccakZero? KeccakSHA3?) instead of Keccak
14
+ # to allow usage of native w/ c-extension Keccak and "lite" version
15
+
16
+
17
+
18
+ module Digest
19
+ class KeccakLite < Digest::Class
20
+ PILN = [10, 7, 11, 17, 18, 3, 5, 16,
21
+ 8, 21, 24, 4, 15, 23, 19, 13,
22
+ 12, 2, 20, 14, 22, 9, 6, 1]
23
+
24
+ ROTC = [ 1, 3, 6, 10, 15, 21, 28, 36,
25
+ 45, 55, 2, 14, 27, 41, 56, 8,
26
+ 25, 43, 62, 18, 39, 61, 20, 44]
27
+
28
+ RNDC = [0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
29
+ 0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
30
+ 0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
31
+ 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
32
+ 0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
33
+ 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
34
+ 0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
35
+ 0x8000000000008080, 0x0000000080000001, 0x8000000080008008]
36
+
37
+ ## note: changed initialize to use param hash_size = 512
38
+ ## do NOT hard-code only 256 bit
39
+ def initialize( hash_size = 512 )
40
+ @size = hash_size / 8
41
+ @buffer = ''
42
+ end
43
+
44
+ def <<( s )
45
+ @buffer << s
46
+ self
47
+ end
48
+ alias_method :update, :<<
49
+
50
+ def reset
51
+ @buffer.clear
52
+ self
53
+ end
54
+
55
+ def finish
56
+ s = Array.new( 25, 0 )
57
+ width = 200 - @size * 2
58
+
59
+ ### note: padding changed in final FIPS PUB 202 standard
60
+ ## from "\x01" to "\x06"
61
+ padding = "\x01"
62
+
63
+ buffer = @buffer
64
+ buffer << padding << "\0" * (width - buffer.size % width)
65
+ buffer[-1] = (buffer[-1].ord | 0x80).chr
66
+
67
+ 0.step( buffer.size - 1, width ) do |j|
68
+ quads = buffer[j, width].unpack( 'Q*' )
69
+ (width / 8).times do |i|
70
+ s[i] ^= quads[i]
71
+ end
72
+
73
+ _keccak( s )
74
+ end
75
+
76
+ s.pack('Q*')[0, @size]
77
+ end
78
+
79
+ private
80
+ def _keccak( s )
81
+ 24.times.each_with_object( [] ) do |round, a|
82
+ # Theta
83
+ 5.times do |i|
84
+ a[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20]
85
+ end
86
+
87
+ 5.times do |i|
88
+ t = a[(i + 4) % 5] ^ _rotate(a[(i + 1) % 5], 1)
89
+ 0.step( 24, 5 ) do |j|
90
+ s[j + i] ^= t
91
+ end
92
+ end
93
+
94
+ # Rho Pi
95
+ t = s[1]
96
+ 24.times do |i|
97
+ j = PILN[i]
98
+ a[0] = s[j]
99
+ s[j] = _rotate( t, ROTC[i] )
100
+ t = a[0]
101
+ end
102
+
103
+ # Chi
104
+ 0.step( 24, 5 ) do |j|
105
+ 5.times do |i|
106
+ a[i] = s[j + i]
107
+ end
108
+
109
+ 5.times do |i|
110
+ s[j + i] ^= ~a[(i + 1) % 5] & a[(i + 2) % 5]
111
+ end
112
+ end
113
+
114
+ # Iota
115
+ s[0] ^= RNDC[round]
116
+ end
117
+ end
118
+
119
+ def _rotate( x, y )
120
+ (x << y | x >> 64 - y) & (1 << 64) - 1
121
+ end
122
+
123
+
124
+ end # class KeccakLite
125
+ end # module Digest
@@ -0,0 +1,118 @@
1
+ ###
2
+ # use a "vendor" bundled sha3 in all ruby (no c-extensions required)
3
+ #
4
+ # see https://github.com/havenwood/sha3-pure-ruby/blob/master/lib/sha3-pure-ruby.rb
5
+
6
+
7
+ ## require 'digest'
8
+
9
+ ##
10
+ # note: use SHA3Lite (or SHA3Zero? ZeroSHA3?) instead of SHA3
11
+ # to allow usage of native w/ c-extension SHA3 and "lite" version
12
+
13
+
14
+ module Digest
15
+ class SHA3Lite < Digest::Class
16
+ PILN = [10, 7, 11, 17, 18, 3, 5, 16,
17
+ 8, 21, 24, 4, 15, 23, 19, 13,
18
+ 12, 2, 20, 14, 22, 9, 6, 1]
19
+
20
+ ROTC = [ 1, 3, 6, 10, 15, 21, 28, 36,
21
+ 45, 55, 2, 14, 27, 41, 56, 8,
22
+ 25, 43, 62, 18, 39, 61, 20, 44]
23
+
24
+ RNDC = [0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
25
+ 0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
26
+ 0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
27
+ 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
28
+ 0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
29
+ 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
30
+ 0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
31
+ 0x8000000000008080, 0x0000000080000001, 0x8000000080008008]
32
+
33
+ def initialize( hash_size = 512 )
34
+ @size = hash_size / 8
35
+ @buffer = '' ## todo/check: make sure buffer is with encoding BINARY - why? why not?
36
+ end
37
+
38
+ def <<( s )
39
+ @buffer << s
40
+ self
41
+ end
42
+ alias_method :update, :<<
43
+
44
+ def reset
45
+ @buffer.clear
46
+ self
47
+ end
48
+
49
+ def finish
50
+ s = Array.new( 25, 0 )
51
+ width = 200 - @size * 2
52
+
53
+ ### note: padding changed in final FIPS PUB 202 standard
54
+ ## from "\x01" to "\x06"
55
+ padding = "\x06"
56
+
57
+ buffer = @buffer
58
+ buffer << padding << "\0" * (width - buffer.size % width)
59
+ buffer[-1] = (buffer[-1].ord | 0x80).chr
60
+
61
+ 0.step( buffer.size - 1, width ) do |j|
62
+ quads = buffer[j, width].unpack( 'Q*' )
63
+ (width / 8).times do |i|
64
+ s[i] ^= quads[i]
65
+ end
66
+
67
+ _keccak( s )
68
+ end
69
+
70
+ s.pack('Q*')[0, @size]
71
+ end
72
+
73
+ private
74
+ def _keccak( s )
75
+ 24.times.each_with_object( [] ) do |round, a|
76
+ # Theta
77
+ 5.times do |i|
78
+ a[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20]
79
+ end
80
+
81
+ 5.times do |i|
82
+ t = a[(i + 4) % 5] ^ _rotate(a[(i + 1) % 5], 1)
83
+ 0.step( 24, 5 ) do |j|
84
+ s[j + i] ^= t
85
+ end
86
+ end
87
+
88
+ # Rho Pi
89
+ t = s[1]
90
+ 24.times do |i|
91
+ j = PILN[i]
92
+ a[0] = s[j]
93
+ s[j] = _rotate( t, ROTC[i] )
94
+ t = a[0]
95
+ end
96
+
97
+ # Chi
98
+ 0.step( 24, 5 ) do |j|
99
+ 5.times do |i|
100
+ a[i] = s[j + i]
101
+ end
102
+
103
+ 5.times do |i|
104
+ s[j + i] ^= ~a[(i + 1) % 5] & a[(i + 2) % 5]
105
+ end
106
+ end
107
+
108
+ # Iota
109
+ s[0] ^= RNDC[round]
110
+ end
111
+ end
112
+
113
+ def _rotate( x, y )
114
+ (x << y | x >> 64 - y) & (1 << 64) - 1
115
+ end
116
+
117
+ end # class SHA3Lite
118
+ end # module Digest
@@ -0,0 +1,22 @@
1
+
2
+
3
+ module Digestlite
4
+ MAJOR = 0
5
+ MINOR = 0
6
+ PATCH = 1
7
+ VERSION = [MAJOR,MINOR,PATCH].join('.')
8
+
9
+ def self.version
10
+ VERSION
11
+ end
12
+
13
+ def self.banner
14
+ "digest-lite/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}] in (#{root})"
15
+ end
16
+
17
+ def self.root
18
+ File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )
19
+ end
20
+
21
+ end # module Digestlite
22
+
@@ -0,0 +1,14 @@
1
+ require 'digest'
2
+
3
+ ## our own code
4
+ require_relative 'digest-lite/version' # note: let version always go first
5
+ require_relative 'digest-lite/keccak'
6
+ require_relative 'digest-lite/sha3'
7
+
8
+
9
+ ##
10
+ ## add camcel case alias - why? why not?
11
+ DigestLite = Digestlite
12
+
13
+
14
+ puts Digestlite ## say hello
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: digest-lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gerald Bauer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rdoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7'
33
+ - !ruby/object:Gem::Dependency
34
+ name: hoe
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.23'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.23'
47
+ description: digest-lite - crypto(graphic) hash functions / classes - Digest::KeccakLite
48
+ (512bit, 256bit, etc), Digest::SHA3Lite (512bit, 256bit, etc) in "100% pure" ruby
49
+ "lite" scripts, that is, without any c-extensions and with zero-dependency
50
+ email: wwwmake@googlegroups.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files:
54
+ - CHANGELOG.md
55
+ - Manifest.txt
56
+ - README.md
57
+ files:
58
+ - CHANGELOG.md
59
+ - Manifest.txt
60
+ - README.md
61
+ - Rakefile
62
+ - lib/digest-lite.rb
63
+ - lib/digest-lite/keccak.rb
64
+ - lib/digest-lite/sha3.rb
65
+ - lib/digest-lite/version.rb
66
+ homepage: https://github.com/rubycoco/blockchain
67
+ licenses:
68
+ - Public Domain
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options:
72
+ - "--main"
73
+ - README.md
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '2.3'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.3.7
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: digest-lite - crypto(graphic) hash functions / classes - Digest::KeccakLite
91
+ (512bit, 256bit, etc), Digest::SHA3Lite (512bit, 256bit, etc) in "100% pure" ruby
92
+ "lite" scripts, that is, without any c-extensions and with zero-dependency
93
+ test_files: []