sha3 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sha3 might be problematic. Click here for more details.
- data/.gitignore +4 -8
- data/README.rdoc +40 -15
- data/ext/sha3/_sha3.c +7 -4
- data/lib/sha3.rb +33 -0
- data/lib/sha3/version.rb +1 -1
- data/spec/sha3_core_spec.rb +45 -0
- metadata +2 -2
data/.gitignore
CHANGED
@@ -6,18 +6,14 @@
|
|
6
6
|
coverage
|
7
7
|
InstalledFiles
|
8
8
|
lib/bundler/man
|
9
|
-
pkg
|
10
|
-
rdoc
|
9
|
+
pkg
|
10
|
+
rdoc
|
11
11
|
spec/reports
|
12
12
|
test/tmp
|
13
13
|
test/version_tmp
|
14
|
-
tmp
|
14
|
+
tmp
|
15
15
|
|
16
16
|
# YARD artifacts
|
17
17
|
.yardoc
|
18
18
|
_yardoc
|
19
|
-
doc/
|
20
|
-
|
21
|
-
*.lock
|
22
|
-
*.so
|
23
|
-
Makefile
|
19
|
+
doc/
|
data/README.rdoc
CHANGED
@@ -9,14 +9,15 @@ Documentation :: http://rubydoc.info/gems/sha3/frames
|
|
9
9
|
|
10
10
|
<em>SHA3 for Ruby</em> is a native (C) implementation of Keccak (SHA3) cryptographic hashing algorithm.
|
11
11
|
|
12
|
-
+SHA3::Digest
|
12
|
+
+SHA3::Digest+: A standard *Digest* _subclass_. The interface, and operation of this class are parallel to digest classes (e.g.: Digest::SHA2, and OpenSSL::Digest) bundled with MRI-based Rubies. See *Digest* documentation for additional details (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/digest/rdoc/Digest.html).
|
13
13
|
|
14
|
-
+SHA3::Digest.compute()
|
14
|
+
+SHA3::Digest.compute()+: A class-method with data bit-length hashing support.
|
15
15
|
|
16
16
|
== Releases
|
17
17
|
|
18
18
|
*0.1.x* :: Alpha code, and not suitable for production.
|
19
19
|
*0.2.0* :: Production worthy, but breaks API compatibility with 0.1.x. Backward-compatibility will be maintained henceforth.
|
20
|
+
*0.2.1* :: Added sub-class for each SHA3 supported bit-lengths (example: SHA3::Digest::SHA256). Minor bug fix.
|
20
21
|
|
21
22
|
== Installation
|
22
23
|
|
@@ -33,49 +34,73 @@ Valid hash bit-lengths are: *224*, *256*, *384*, *512*. You may also use corresp
|
|
33
34
|
:sha384
|
34
35
|
:sha512
|
35
36
|
|
37
|
+
# Example: SHA3::Digest.new(224) = SHA3::Digest.new(:sha224)
|
38
|
+
|
39
|
+
Alternatively, you can instantiate using one of four sub-classes:
|
40
|
+
|
41
|
+
SHA3::Digest::SHA224.new() # 224 bits
|
42
|
+
SHA3::Digest::SHA256.new() # 256 bits
|
43
|
+
SHA3::Digest::SHA384.new() # 384 bits
|
44
|
+
SHA3::Digest::SHA512.new() # 512 bits
|
45
|
+
|
36
46
|
=== Basics
|
37
47
|
|
38
48
|
# Instantiate a new SHA3::Digest class with 256 bit length
|
39
49
|
s = SHA3::Digest.new(:sha256)
|
40
|
-
|
41
|
-
|
50
|
+
|
51
|
+
# OR #
|
52
|
+
|
53
|
+
s = SHA3::Digest::SHA256.new()
|
54
|
+
|
42
55
|
# Update hash state, and compute new value
|
43
56
|
s.update "Compute Me"
|
44
|
-
# => #<SHA3::Digest: c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470>
|
45
57
|
|
46
58
|
# << is an .update() alias
|
47
59
|
s << "Me too"
|
48
|
-
# => #<SHA3::Digest: e26f539eee3a05c52eb1f9439652d23343adea9764f011da232d24cd6d19924a>
|
49
60
|
|
50
|
-
#
|
51
|
-
|
61
|
+
# Returns digest value in bytes
|
62
|
+
s.digest
|
63
|
+
# => "\xBE\xDF\r\xD9\xA1[dt \x8BbLN\x8A\x1Eg,\xE0\xF0d\x98\xE3\xAB\xD7\xCC7\xF0\xAEiu\x92Y"
|
64
|
+
|
65
|
+
# Returns digest value as hex string
|
66
|
+
s.hexdigest
|
67
|
+
# => "bedf0dd9a15b6474208b624c4e8a1e672ce0f06498e3abd7cc37f0ae69759259"
|
68
|
+
|
69
|
+
# Digest class methods:
|
52
70
|
|
53
|
-
|
54
|
-
|
71
|
+
SHA3::Digest.hexdigest("Hash me, please", :sha224)
|
72
|
+
# => "200e7bc18cd6132689eb8fa3f7c3a978d73215384a626c23e4508f33"
|
73
|
+
|
74
|
+
SHA3::Digest::SHA384.digest("Hash me, please")
|
75
|
+
# => "\xF5\xCEpC\xB0eV\xA3\x92P\xF8\x93\xB8\x145\x19\x1A)\xB1\x88\xBF\a\a7\x88\f\xFETI\e\x17\x80\xEC\xA6}\xABR\xAE\xAFK\xFF|d\x81\xF7\xB5\x9Ah"
|
55
76
|
|
56
77
|
=== Hashing a file
|
57
78
|
|
58
79
|
# Compute the hash value for given file, and return the result as hex
|
59
|
-
s = SHA3::Digest.
|
80
|
+
s = SHA3::Digest::SHA224.file("my_fantastical_file.bin").hexdigest
|
81
|
+
|
82
|
+
# Calling SHA3::Digest.file(...) defaults to SHA256
|
83
|
+
s = SHA3::Digest.file("tests.sh")
|
84
|
+
# => #<SHA3::Digest: a9801db49389339bd8a62817f229f0f9394ca73b34fd7dbc7ec5ed7a99bc49f1>
|
60
85
|
|
61
86
|
=== Bit-length hashing
|
62
87
|
|
63
88
|
# Compute hash of "011"
|
64
89
|
SHA3::Digest.compute(:sha224, "\xC0", 3).unpack("H*")
|
65
|
-
|
90
|
+
# => ["2b695a6fd92a2b3f3ce9cfca617d22c9bb52815dd59a9719b01bad25"]
|
66
91
|
|
67
92
|
== Development
|
68
93
|
|
69
94
|
* Native build tools (e.g., GCC, Minigw, etc.)
|
70
95
|
* Gems: rubygems-tasks, rake, rspec, yard
|
71
96
|
|
72
|
-
== RSpec
|
97
|
+
== Testing + RSpec
|
73
98
|
|
74
99
|
Call +rake+ to run the included RSpec tests.
|
75
100
|
|
76
|
-
|
101
|
+
Only a small subset of test vectors are included in the source repository; however, the complete test vectors suite is available for download. Simply run the +test.sh+ shell script (available in the root of source directory) to generate full bit-length RSpec test files.
|
77
102
|
|
78
|
-
sh
|
103
|
+
sh tests.sh
|
79
104
|
|
80
105
|
== Rubies
|
81
106
|
|
data/ext/sha3/_sha3.c
CHANGED
@@ -110,14 +110,17 @@ static VALUE c_digest_alloc(VALUE klass)
|
|
110
110
|
if (!mdx)
|
111
111
|
rb_raise(eDigestError, "failed to allocate object memory");
|
112
112
|
|
113
|
-
mdx->state = (hashState *) malloc(sizeof(*mdx->state));
|
114
|
-
|
115
|
-
|
116
|
-
if (!mdx->state)
|
113
|
+
mdx->state = (hashState *) malloc(sizeof(*mdx->state));
|
114
|
+
if (!mdx->state) {
|
115
|
+
free_allox(mdx);
|
117
116
|
rb_raise(eDigestError, "failed to allocate state memory");
|
117
|
+
}
|
118
118
|
|
119
119
|
obj = Data_Wrap_Struct(klass, 0, free_allox, mdx);
|
120
120
|
|
121
|
+
memset(mdx->state, 0, sizeof(*mdx->state));
|
122
|
+
mdx->hashbitlen = 0;
|
123
|
+
|
121
124
|
return obj;
|
122
125
|
}
|
123
126
|
|
data/lib/sha3.rb
CHANGED
@@ -1,2 +1,35 @@
|
|
1
1
|
require 'sha3_n'
|
2
2
|
require 'sha3/version'
|
3
|
+
|
4
|
+
module SHA3
|
5
|
+
class Digest
|
6
|
+
|
7
|
+
# Based on 'OpenSSL for Ruby 2' project
|
8
|
+
# Copyright (C) 2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
9
|
+
alg = { :sha224 => "SHA224", :sha256 => "SHA256", :sha384 => "SHA384", :sha512 => "SHA512"}
|
10
|
+
|
11
|
+
def self.digest(name, data)
|
12
|
+
super(data, name)
|
13
|
+
end
|
14
|
+
|
15
|
+
alg.each { |key, name|
|
16
|
+
klass = Class.new(Digest) {
|
17
|
+
define_method(:initialize) { |*data|
|
18
|
+
if data.length > 1
|
19
|
+
raise ArgumentError,
|
20
|
+
"wrong number of arguments (#{data.length} for 1)"
|
21
|
+
end
|
22
|
+
|
23
|
+
super(key, data.first)
|
24
|
+
}
|
25
|
+
}
|
26
|
+
singleton = (class << klass; self; end)
|
27
|
+
singleton.class_eval{
|
28
|
+
define_method(:digest){ |data| Digest.digest(key, data) }
|
29
|
+
define_method(:hexdigest){ |data| Digest.hexdigest(key, data) }
|
30
|
+
}
|
31
|
+
|
32
|
+
const_set(name, klass)
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
data/lib/sha3/version.rb
CHANGED
data/spec/sha3_core_spec.rb
CHANGED
@@ -70,6 +70,51 @@ describe SHA3::Digest do
|
|
70
70
|
sha.digest_length.should(eq(64))
|
71
71
|
sha.block_length.should(eq(72))
|
72
72
|
end
|
73
|
+
|
74
|
+
it "should pass Digest.SHA224() usage test" do
|
75
|
+
sha = SHA3::Digest::SHA224.new()
|
76
|
+
sha.hexdigest.should eq("f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd")
|
77
|
+
sha.update(["cc"].pack("H*")).hexdigest.should eq("a9cab59eb40a10b246290f2d6086e32e3689faf1d26b470c899f2802")
|
78
|
+
sha.reset.hexdigest.should eq("f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd")
|
79
|
+
sha << (["5fce8109a358570e40983e1184e541833bb9091e280f258cfb144387b05d190e431cb19baa67273ba0c58abe91308e1844dcd0b3678baa42f335f2fa05267a0240b3c718a5942b3b3e3bfa98a55c25a1466e8d7a603722cb2bbf03afa54cd769a99f310735ee5a05dae2c22d397bd95635f58c48a67f90e1b73aafcd3f82117f0166657838691005b18da6f341d6e90fc1cdb352b30fae45d348294e501b63252de14740f2b85ae5299ddec3172de8b6d0ba219a20a23bb5e10ff434d39db3f583305e9f5c039d98569e377b75a70ab837d1df269b8a4b566f40bb91b577455fd3c356c914fa06b9a7ce24c7317a172d"].pack("H*"))
|
80
|
+
sha.hexdigest.should(eq("db85af5cfce746240e6d44e73cef66a72ce5968284d35ffef7fbff6c"))
|
81
|
+
sha.digest_length.should(eq(28))
|
82
|
+
sha.block_length.should(eq(144))
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should pass Digest.SHA256() usage test" do
|
86
|
+
sha = SHA3::Digest::SHA256.new()
|
87
|
+
sha.hexdigest.should eq("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
|
88
|
+
sha.update(["cc"].pack("H*")).hexdigest.should eq("eead6dbfc7340a56caedc044696a168870549a6a7f6f56961e84a54bd9970b8a")
|
89
|
+
sha.reset.hexdigest.should eq("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
|
90
|
+
sha << (["6172f1971a6e1e4e6170afbad95d5fec99bf69b24b674bc17dd78011615e502de6f56b86b1a71d3f4348087218ac7b7d09302993be272e4a591968aef18a1262d665610d1070ee91cc8da36e1f841a69a7a682c580e836941d21d909a3afc1f0b963e1ca5ab193e124a1a53df1c587470e5881fb54dae1b0d840f0c8f9d1b04c645ba1041c7d8dbf22030a623aa15638b3d99a2c400ff76f3252079af88d2b37f35ee66c1ad7801a28d3d388ac450b97d5f0f79e4541755356b3b1a5696b023f39ab7ab5f28df4202936bc97393b93bc915cb159ea1bd7a0a414cb4b7a1ac3af68f50d79f0c9c7314e750f7d02faa58bfa"].pack("H*"))
|
91
|
+
sha.hexdigest.should(eq("4ea524e705020284b18284e34683725590e1ee565a6ff598ed4d42b1c987471e"))
|
92
|
+
sha.digest_length.should(eq(32))
|
93
|
+
sha.block_length.should(eq(136))
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should pass Digest.SHA384() usage test" do
|
97
|
+
sha = SHA3::Digest::SHA384.new()
|
98
|
+
sha.hexdigest.should eq("2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff")
|
99
|
+
sha.update(["cc"].pack("H*")).hexdigest.should eq("1b84e62a46e5a201861754af5dc95c4a1a69caf4a796ae405680161e29572641f5fa1e8641d7958336ee7b11c58f73e9")
|
100
|
+
sha.reset.hexdigest.should eq("2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff")
|
101
|
+
sha << (["3b8e97c5ffc2d6a40fa7de7fcefc90f3b12c940e7ab415321e29ee692dfac799b009c99dcddb708fce5a178c5c35ee2b8617143edc4c40b4d313661f49abdd93cea79d117518805496fe6acf292c4c2a1f76b403a97d7c399daf85b46ad84e16246c67d6836757bde336c290d5d401e6c1386ab32797af6bb251e9b2d8fe754c47482b72e0b394eab76916126fd68ea7d65eb93d59f5b4c5ac40f7c3b37e7f3694f29424c24af8c8f0ef59cd9dbf1d28e0e10f799a6f78cad1d45b9db3d7dee4a7059abe99182714983b9c9d44d7f5643596d4f3"].pack("H*"))
|
102
|
+
sha.hexdigest.should(eq("9172aad6c15b4dcd79bbd84fad0601119d8b4e3afed17b594ff38424157985ee27b65826b9905486e767e85aa031e07b"))
|
103
|
+
sha.digest_length.should(eq(48))
|
104
|
+
sha.block_length.should(eq(104))
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should pass Digest.SHA512() usage test" do
|
108
|
+
sha = SHA3::Digest::SHA512.new()
|
109
|
+
sha.hexdigest.should eq("0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e")
|
110
|
+
sha.update(["cc"].pack("H*")).hexdigest.should eq("8630c13cbd066ea74bbe7fe468fec1dee10edc1254fb4c1b7c5fd69b646e44160b8ce01d05a0908ca790dfb080f4b513bc3b6225ece7a810371441a5ac666eb9")
|
111
|
+
sha.reset.hexdigest.should eq("0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e")
|
112
|
+
sha << (["03d625488354df30e3f875a68edfcf340e8366a8e1ab67f9d5c5486a96829dfac0578289082b2a62117e1cf418b43b90e0adc881fc6ae8105c888e9ecd21aea1c9ae1a4038dfd17378fed71d02ae492087d7cdcd98f746855227967cb1ab4714261ee3bead3f4db118329d3ebef4bc48a875c19ba763966da0ebea800e01b2f50b00e9dd4caca6dcb314d00184ef71ea2391d760c950710db4a70f9212ffc54861f9dc752ce18867b8ad0c48df8466ef7231e7ac567f0eb55099e622ebb86cb237520190a61c66ad34f1f4e289cb3282ae3eaac6152ed24d2c92bae5a7658252a53c49b7b02dfe54fdb2e90074b6cf310ac661"].pack("H*"))
|
113
|
+
sha.hexdigest.should(eq("13a592b73ede487036c8816bd6fc6cdc04dc6133409a6ee990584160518f9ef573264cf04d38a3ba75d150f4f026f6df8936e13c8f4f3ecc9ecbc43fdfc488a4"))
|
114
|
+
sha.digest_length.should(eq(64))
|
115
|
+
sha.block_length.should(eq(72))
|
116
|
+
end
|
117
|
+
|
73
118
|
end
|
74
119
|
|
75
120
|
describe "SHA3::Digest.compute" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sha3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake-compiler
|