keccak256 2.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.travis.yml +18 -0
- data/ChangeLog.md +3 -0
- data/Gemfile +2 -0
- data/README.md +32 -0
- data/Rakefile +7 -0
- data/keccack256.gemspec +20 -0
- data/lib/keccak256.rb +103 -0
- data/test/test_keccak256.rb +24 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ee15cbda09e860d595c0efd6974b8ede74ef568620b20946386d879f6bbd3416
|
4
|
+
data.tar.gz: b6a761015bf6712727d73311dd8f2060f886e77584a1a2ed615529fec74af874
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: baa31c4d0b438bd4c198d1ac6d65ea883ae4a71e12d0e090d4b379f4c5aa19e3f049eb9f6ab3f1f6c108804ff1cfd5af2add3661b305d2372191fd865a3414ff
|
7
|
+
data.tar.gz: a8d3dd4f3039126ab8a31f259d53aecf0077bf1d7b4259f029f256563cbed3bce8941bca772fdea449df9bf853e7998311b9fd7005fa0376043ee38e06e6eb99
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/ChangeLog.md
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Keccak256 Gem
|
2
|
+
|
3
|
+
**Note:** This is a fork of [havenwood/sha3-pure-ruby](https://github.com/havenwood/sha3-pure-ruby) to add support for the (older) Keccak version this hashing algorithm which Ethereum uses.
|
4
|
+
|
5
|
+
> A pure Ruby implementation of Keccak256.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install it from the command line:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
gem install keccak256
|
13
|
+
```
|
14
|
+
|
15
|
+
Or execute `bundle` after adding this line to your app's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'keccak256'
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage Example
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
Digest::Keccak256.new.hexdigest 'cat'
|
25
|
+
#=> "52763589e772702fa7977a28b3cfb6ca534f0208a2b2d55f7558af664eac478a"
|
26
|
+
```
|
27
|
+
|
28
|
+
## License
|
29
|
+
|
30
|
+
Creative Commons
|
31
|
+
|
32
|
+
See original license: [havenwood/sha3-pure-ruby](https://github.com/havenwood/sha3-pure-ruby)
|
data/Rakefile
ADDED
data/keccack256.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path '../lib', __FILE__
|
3
|
+
$:.unshift lib unless $:.include? lib
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'keccak256'
|
7
|
+
spec.version = '2.0.0'
|
8
|
+
spec.authors = ['havenwood', 'evtaylor']
|
9
|
+
spec.email = ['admin@evantaylor.ca']
|
10
|
+
spec.description = 'An implementation of Keccak256 (the hash function used by Ethereum) in pure Ruby.'
|
11
|
+
spec.summary = 'Pure Ruby Keccack256.'
|
12
|
+
spec.homepage = 'https://github.com/evtaylor/sha3-pure-ruby'
|
13
|
+
spec.license = 'https://creativecommons.org/publicdomain/zero/1.0/'
|
14
|
+
spec.files = `git ls-files`.split $/
|
15
|
+
# spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename f }
|
16
|
+
spec.test_files = spec.files.grep %r{^(test)/}
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
spec.add_development_dependency 'rake'
|
19
|
+
spec.add_development_dependency 'minitest'
|
20
|
+
end
|
data/lib/keccak256.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class Keccak256 < Digest::Class
|
5
|
+
PILN = [10, 7, 11, 17, 18, 3, 5, 16,
|
6
|
+
8, 21, 24, 4, 15, 23, 19, 13,
|
7
|
+
12, 2, 20, 14, 22, 9, 6, 1]
|
8
|
+
|
9
|
+
ROTC = [ 1, 3, 6, 10, 15, 21, 28, 36,
|
10
|
+
45, 55, 2, 14, 27, 41, 56, 8,
|
11
|
+
25, 43, 62, 18, 39, 61, 20, 44]
|
12
|
+
|
13
|
+
RNDC = [0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
|
14
|
+
0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
|
15
|
+
0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
|
16
|
+
0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
|
17
|
+
0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
|
18
|
+
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
|
19
|
+
0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
|
20
|
+
0x8000000000008080, 0x0000000080000001, 0x8000000080008008]
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@size = 256 / 8
|
24
|
+
@buffer = ''
|
25
|
+
end
|
26
|
+
|
27
|
+
def << s
|
28
|
+
@buffer << s
|
29
|
+
self
|
30
|
+
end
|
31
|
+
alias update <<
|
32
|
+
|
33
|
+
def reset
|
34
|
+
@buffer.clear
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def finish
|
39
|
+
s = Array.new 25, 0
|
40
|
+
width = 200 - @size * 2
|
41
|
+
padding = "\x01"
|
42
|
+
|
43
|
+
buffer = @buffer
|
44
|
+
buffer << padding << "\0" * (width - buffer.size % width)
|
45
|
+
buffer[-1] = (buffer[-1].ord | 0x80).chr
|
46
|
+
|
47
|
+
0.step buffer.size - 1, width do |j|
|
48
|
+
quads = buffer[j, width].unpack 'Q*'
|
49
|
+
(width / 8).times do |i|
|
50
|
+
s[i] ^= quads[i]
|
51
|
+
end
|
52
|
+
|
53
|
+
keccak s
|
54
|
+
end
|
55
|
+
|
56
|
+
s.pack('Q*')[0, @size]
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
def keccak s
|
61
|
+
24.times.each_with_object [] do |round, a|
|
62
|
+
# Theta
|
63
|
+
5.times do |i|
|
64
|
+
a[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20]
|
65
|
+
end
|
66
|
+
|
67
|
+
5.times do |i|
|
68
|
+
t = a[(i + 4) % 5] ^ rotate(a[(i + 1) % 5], 1)
|
69
|
+
0.step 24, 5 do |j|
|
70
|
+
s[j + i] ^= t
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Rho Pi
|
75
|
+
t = s[1]
|
76
|
+
24.times do |i|
|
77
|
+
j = PILN[i]
|
78
|
+
a[0] = s[j]
|
79
|
+
s[j] = rotate t, ROTC[i]
|
80
|
+
t = a[0]
|
81
|
+
end
|
82
|
+
|
83
|
+
# Chi
|
84
|
+
0.step 24, 5 do |j|
|
85
|
+
5.times do |i|
|
86
|
+
a[i] = s[j + i]
|
87
|
+
end
|
88
|
+
|
89
|
+
5.times do |i|
|
90
|
+
s[j + i] ^= ~a[(i + 1) % 5] & a[(i + 2) % 5]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Iota
|
95
|
+
s[0] ^= RNDC[round]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def rotate x, y
|
100
|
+
(x << y | x >> 64 - y) & (1 << 64) - 1
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path '../../lib/keccak256', __FILE__
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'minitest/pride'
|
4
|
+
|
5
|
+
# http://emn178.github.io/online-tools/sha3_224.html
|
6
|
+
# http://emn178.github.io/online-tools/sha3_256.html
|
7
|
+
# http://emn178.github.io/online-tools/sha3_512.html
|
8
|
+
# http://emn178.github.io/online-tools/sha3_384.html
|
9
|
+
|
10
|
+
describe Digest::Keccak256 do
|
11
|
+
describe 'Default 256 Keccak padding' do
|
12
|
+
it 'should work when empty' do
|
13
|
+
empty_string_sha3 = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
|
14
|
+
assert_equal Digest::Keccak256.new.hexdigest(''), empty_string_sha3
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should work with content' do
|
18
|
+
cat_sha3 = '52763589e772702fa7977a28b3cfb6ca534f0208a2b2d55f7558af664eac478a'
|
19
|
+
assert_equal Digest::Keccak256.new.hexdigest('cat'), cat_sha3
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: keccak256
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- havenwood
|
8
|
+
- evtaylor
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2021-06-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: minitest
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
description: An implementation of Keccak256 (the hash function used by Ethereum) in
|
43
|
+
pure Ruby.
|
44
|
+
email:
|
45
|
+
- admin@evantaylor.ca
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".travis.yml"
|
52
|
+
- ChangeLog.md
|
53
|
+
- Gemfile
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- keccack256.gemspec
|
57
|
+
- lib/keccak256.rb
|
58
|
+
- test/test_keccak256.rb
|
59
|
+
homepage: https://github.com/evtaylor/sha3-pure-ruby
|
60
|
+
licenses:
|
61
|
+
- https://creativecommons.org/publicdomain/zero/1.0/
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubygems_version: 3.2.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Pure Ruby Keccack256.
|
82
|
+
test_files:
|
83
|
+
- test/test_keccak256.rb
|