sha3-pure-ruby 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
+ SHA1:
3
+ metadata.gz: 454ede6b3b6a58124b12318611d00bc4590a52a4
4
+ data.tar.gz: 3f00505cc75d45c90759f258a1d1bdb73cc6b4f5
5
+ SHA512:
6
+ metadata.gz: d68106a9854cff672c1131d6e07e623ae794b19ffd146fc05b497e5f3b47d3cbacca1ac8fe5add04af8b229a9816502fb81cba1992aaecc5c4c606435ce7ed1e
7
+ data.tar.gz: 2888bad8a3ca3f787580747b299877b5293ae0a1b99b7479e645ea52767b6cc3ceeae8708e2d1ab133720cbe2e87d6496d4e0a290f624c0eb08d71dbfa39f5b7
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sha3-pure-ruby (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ sha3-pure-ruby!
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Pure Ruby SHA3 Gem
2
+
3
+ A pure Ruby implementation of SHA3.
4
+
5
+ ## Installation
6
+
7
+ Install it yourself:
8
+
9
+ $ gem install sha3-pure-ruby
10
+
11
+ Or add this line to your app's Gemfile:
12
+
13
+ gem 'sha3-pure-ruby'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ Digest::SHA3.new.hexdigest ''
23
+ #=> "0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e"
24
+ ```
25
+
26
+ ## License
27
+
28
+ To the extent possible under law, Christian Neukirchen has waived all copyright and related or neighboring rights to the source code in this file.
29
+ Minor changes by Clemens Gruber.
30
+ Refactored, tests added, and cut into a gem by Shannon Skipper.
31
+
32
+ [https://creativecommons.org/publicdomain/zero/1.0/](https://creativecommons.org/publicdomain/zero/1.0/)
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ task default: [:test]
2
+
3
+ task :test do
4
+ ruby 'test/test_sha3-pure-ruby.rb'
5
+ end
@@ -0,0 +1,103 @@
1
+ require 'digest'
2
+ require_relative 'sha3-pure-ruby/version'
3
+
4
+ module Digest
5
+ class SHA3 < Digest::Class
6
+ PILN = [10, 7, 11, 17, 18, 3, 5, 16,
7
+ 8, 21, 24, 4, 15, 23, 19, 13,
8
+ 12, 2, 20, 14, 22, 9, 6, 1]
9
+
10
+ ROTC = [ 1, 3, 6, 10, 15, 21, 28, 36,
11
+ 45, 55, 2, 14, 27, 41, 56, 8,
12
+ 25, 43, 62, 18, 39, 61, 20, 44]
13
+
14
+ RNDC = [0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
15
+ 0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
16
+ 0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
17
+ 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
18
+ 0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
19
+ 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
20
+ 0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
21
+ 0x8000000000008080, 0x0000000080000001, 0x8000000080008008]
22
+
23
+ def initialize hash_size = 512
24
+ @size = hash_size / 8
25
+ @buffer = ''
26
+ end
27
+
28
+ def << s
29
+ @buffer << s
30
+ self
31
+ end
32
+ alias update <<
33
+
34
+ def reset
35
+ @buffer.clear
36
+ self
37
+ end
38
+
39
+ def finish
40
+ s = Array.new 25, 0
41
+ width = 200 - @size * 2
42
+
43
+ buffer = @buffer
44
+ buffer << "\x01" << "\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,3 @@
1
+ class SHA3
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path '../lib', __FILE__
3
+ $:.unshift lib unless $:.include? lib
4
+ require 'sha3-pure-ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sha3-pure-ruby'
8
+ spec.version = SHA3::VERSION
9
+ spec.authors = ['havenwood']
10
+ spec.email = ['shannonskipper@gmail.com']
11
+ spec.description = 'An implementation of SHA3 in pure Ruby.'
12
+ spec.summary = 'Pure Ruby SHA3.'
13
+ spec.homepage = 'https://github.com/havenwood/sha3-pure-ruby'
14
+ spec.license = 'https://creativecommons.org/publicdomain/zero/1.0/'
15
+ spec.files = `git ls-files`.split $/
16
+ # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename f }
17
+ spec.test_files = spec.files.grep %r{^(test)/}
18
+ spec.require_paths = ['lib']
19
+ spec.add_development_dependency 'rake'
20
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path '../../lib/sha3-pure-ruby', __FILE__
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+
5
+ describe Digest::SHA3 do
6
+ it 'should work' do
7
+ empty_string_sha3 = '0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'
8
+ assert_equal Digest::SHA3.new.hexdigest(''), empty_string_sha3
9
+ end
10
+ end
11
+
data/travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.9.2
6
+ - jruby-19mode
7
+ - rbx-19mode
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sha3-pure-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - havenwood
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: An implementation of SHA3 in pure Ruby.
28
+ email:
29
+ - shannonskipper@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - README.md
38
+ - Rakefile
39
+ - lib/sha3-pure-ruby.rb
40
+ - lib/sha3-pure-ruby/version.rb
41
+ - sha3-pure-ruby.gemspec
42
+ - test/test_sha3-pure-ruby.rb
43
+ - travis.yml
44
+ homepage: https://github.com/havenwood/sha3-pure-ruby
45
+ licenses:
46
+ - https://creativecommons.org/publicdomain/zero/1.0/
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.0.3
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Pure Ruby SHA3.
68
+ test_files:
69
+ - test/test_sha3-pure-ruby.rb
70
+ has_rdoc: