sha3 1.0.1 → 1.0.4

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.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4eb74d4c4626be8751f643664a1820c0227a88dd
4
- data.tar.gz: bb6c33da2459890eeb083874e209cbf75b3ca83e
2
+ SHA256:
3
+ metadata.gz: ae66a849bd5438b3b6d3fa4f5d33505a75e6f64556bfacc21d0efcf03a7d41d8
4
+ data.tar.gz: dea6beac49eeaeb12829af4e87b36dfc19ac1b66bb7872a7c6884441a70cf814
5
5
  SHA512:
6
- metadata.gz: ed137cc7d02d2c376b256c96f55bd430305fead1d00942cc251f7027c9651d08877b1a31372c9822cb8daf5fcebae689683b59c216d5bf3ffe413e179a5258cb
7
- data.tar.gz: 1446633b9181a36ed9b0fa04a3d2a7593add059fbe473a496ad35b9c0837da9dd690b167d8bb2d2ea036b6dca60f83b644e8e0719e728d8883aa72064ed8a32d
6
+ metadata.gz: d374e5f716056d6d888d862f97bc07d433c74f4d721d2342cc09f7865571e47eec50f7b1ecd9b284aedb1c908393b0da249e0c474f5792e215097263928d9195
7
+ data.tar.gz: 6ea6442adf3c90ca8849576f8539835630d968a032b385447a9c95e2f4be97861910f510ba528dbe398990c2eb71fac6b3966816807571855b855afa8c6af15b
checksums.yaml.gz.sig ADDED
Binary file
data/.rspec CHANGED
@@ -1 +1,3 @@
1
- --colour --format documentation
1
+ --format documentation
2
+ --color
3
+ --require ./spec/spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.6
4
+
5
+ Layout/LineLength:
6
+ Max: 120
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
- source "http://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
 
3
5
  gemspec
data/README.md CHANGED
@@ -1,17 +1,17 @@
1
1
  # sha3
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/sha3.svg)](https://badge.fury.io/rb/sha3) [![CI](https://secure.travis-ci.org/johanns/sha3.png)](https://secure.travis-ci.org/johanns/sha3) [![Dependencies](https://gemnasium.com/johanns/sha3.png)](https://gemnasium.com/johanns/sha3) [![CodeClimate](https://codeclimate.com/github/johanns/sha3.png)](https://codeclimate.com/github/johanns/sha3)
3
+ [![Gem Version](https://badge.fury.io/rb/sha3.svg)](https://badge.fury.io/rb/sha3) [![Ruby](https://github.com/johanns/sha3/actions/workflows/main.yml/badge.svg)](https://github.com/johanns/sha3/actions/workflows/main.yml)
4
4
 
5
5
  **SHA3 for Ruby** is a native (C) binding to SHA3 (Keccak FIPS 202) cryptographic hashing algorithm.
6
6
 
7
- - Home :: [https://github.com/johanns/sha3#readme]()
8
- - Issues :: [https://github.com/johanns/sha3/issues]()
9
- - Documentation :: [http://rubydoc.info/gems/sha3/frames]()
7
+ - [Home](https://github.com/johanns/sha3#readme)
8
+ - [Issues](https://github.com/johanns/sha3/issues)
9
+ - [Documentation](http://rubydoc.info/gems/sha3/frames)
10
10
 
11
11
  ## Warnings
12
12
 
13
- - Version 1.0+ breaks compatibility with previous versions of this gem.
14
- - Do NOT use SHA3 to hash passwords; use either ```bcrypt``` or ```scrypt``` instead!
13
+ - Please do NOT use SHA3 to hash passwords -- use a slow hashing function instead (e.g.: `pbkdf2`, `argon2`, `bcrypt` or `scrypt`)
14
+ - Version 1.0 introduces new API and is incompatible with previous versions (0.x).
15
15
 
16
16
  ## Module details
17
17
 
@@ -97,7 +97,7 @@ s = SHA3::Digest.file("tests.sh")
97
97
  * Native build tools (e.g., GCC, Minigw, etc.)
98
98
  * Gems: rubygems-tasks, rake, rspec, yard
99
99
 
100
- ### Testing + RSpec
100
+ ### Testing
101
101
 
102
102
  Call ```rake``` to run the included RSpec tests.
103
103
 
@@ -107,39 +107,14 @@ Only a small subset of test vectors are included in the source repository; howev
107
107
 
108
108
  ### Rubies
109
109
 
110
- Tested with Rubies:
110
+ Supported Ruby versions:
111
111
 
112
- - MRI Ruby-Head
113
- - MRI 2.1.0
114
- - MRI 2.0.0
115
- - MRI 1.9.3
116
- - MRI 1.9.2
117
- - MRI 1.8.7
118
- - Rubinius 2
112
+ - MRI Ruby 2.4 - 3.1
119
113
 
120
- On:
121
114
 
122
- - Ubuntu 12.04, 12.10, 13.04, 14.04, 15.04
123
- - Windows 7, 8, 8.1, 10
124
- - Mac OS X 10.6 - 10.11
125
-
126
- ## Releases
127
-
128
- - *1.0.1* :: FIPS 202 compliance (breaks compatibility with earlier releases)
129
- - *0.2.6* :: Fixed bug #4
130
- - *0.2.5* :: Bug fixes. (See ChangeLog.rdoc)
131
- - *0.2.4* :: Bug fixes. (YANKED)
132
- - *0.2.3* :: Added documentation file (decoupled form C source); refactored C source.
133
- - *0.2.2* :: Added sub-class for each SHA3 supported bit-lengths (example: SHA3::Digest::SHA256). Minor bug fix.
134
- - *0.2.0* :: Production worthy, but breaks API compatibility with 0.1.x. Backward-compatibility will be maintained henceforth.
135
- - *0.1.x* :: Alpha code, and not suitable for production.
136
-
137
- ## TO DO
138
-
139
- - Add SHAKE128/256 support
140
115
 
141
116
  ## Copyright
142
117
 
143
- Copyright (c) 2012 - 2015 Johanns Gregorian (https://github.com/johanns)
118
+ Copyright (c) 2012 - 2022 Johanns Gregorian (https://github.com/johanns)
144
119
 
145
120
  **See LICENSE.txt for details.**
data/Rakefile CHANGED
@@ -1,56 +1,27 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'rubygems'
4
- require 'rake'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
5
 
6
- begin
7
- gem 'rubygems-tasks'
8
- require 'rubygems/tasks'
6
+ RSpec::Core::RakeTask.new(:spec)
9
7
 
10
- Gem::Tasks.new
11
- rescue LoadError => e
12
- warn e.message
13
- warn "Run `gem install rubygems-tasks` to install Gem::Tasks."
14
- end
8
+ require 'rubocop/rake_task'
15
9
 
16
- begin
17
- gem 'rspec', '~> 3.3'
18
- require 'rspec/core/rake_task'
10
+ RuboCop::RakeTask.new
19
11
 
20
- RSpec::Core::RakeTask.new
21
- rescue LoadError => e
22
- task :spec do
23
- abort "Please run `gem install rspec` to install RSpec."
24
- end
25
- end
26
-
27
- task :test => :spec
28
- task :default => [:compile, :spec]
12
+ require 'rake/extensiontask'
29
13
 
30
14
  begin
31
- gem 'yard'
32
- require 'yard'
33
-
34
- YARD::Rake::YardocTask.new
35
- rescue LoadError => e
36
- task :yard do
37
- abort "Please run `gem install yard` to install YARD."
38
- end
39
- end
40
- task :doc => :yard
41
-
42
- begin
43
- gem 'rake-compiler'
44
- require 'rake/extensiontask'
45
-
46
- Rake::ExtensionTask.new do |ext|
15
+ Rake::ExtensionTask.new :compile do |ext|
47
16
  ext.name = 'sha3_n'
48
17
  ext.ext_dir = 'ext/sha3'
49
18
  ext.tmp_dir = 'tmp'
50
- ext.source_pattern = "*.{c}"
19
+ ext.source_pattern = '*.{c}'
51
20
  end
52
- rescue LoadError => e
21
+ rescue LoadError
53
22
  task :compile do
54
- abort "Please run `gem install rake-compiler` to install Rake-Compiler."
23
+ abort 'Please run `gem install rake-compiler` to install Rake-Compiler.'
55
24
  end
56
25
  end
26
+
27
+ task default: %i[compile spec]
data/certs/johanns.pem ADDED
@@ -0,0 +1,24 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEFDCCAnygAwIBAgIBATANBgkqhkiG9w0BAQsFADAaMRgwFgYDVQQDDA9pby9E
3
+ Qz1qc2cvREM9aW8wHhcNMjIwMzA5MDcyODEwWhcNMjMwMzA5MDcyODEwWjAaMRgw
4
+ FgYDVQQDDA9pby9EQz1qc2cvREM9aW8wggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAw
5
+ ggGKAoIBgQCuPwkDs4uoBV6pQhKp3uA58LkDPRR7VoI4QryuH7PbIwuO3xbuyR6T
6
+ xEqwcb9GgVnMxiFyXeVkWiriuGXqZ26Pp5zH/xxQNgPGCIyXuXeJMpASkWjJ9J6h
7
+ +jOdaG5jdL6zRcw/5XaJSQTLGO32BNOIezUZCF9JtMoEBQYHRWFjoD5BryjYnU3f
8
+ SstXg9nD8KOyk5Vzqk/wvJs5X+b2jcZL0KmSP1UgaCakfTCKf8LA9I5zAbvv2M9n
9
+ F0JQfKw4AUlmykzmOMEsITVxtM8lIVxm99KAS+lFn19xEjOs7nr3GktyLJxav8qI
10
+ 3yXW/q5wUbOFrj/e1tk6r/W3bM6TQzGFBsBJgXEoFFZ+OQdEW8PXU0H5CIg59SxB
11
+ lXwSC1rKlxSeGNcKDXeIN1AU3pqv/PJj2SKg2+uQ9Staya0Mtek/caPDVfIy8AgH
12
+ ulTeTVN7m5SdVjVBl3rIehH/j0R/JF7ygsMkJEQAxrwPxqmazXkPAGLNNIqppaxi
13
+ VyHhxSnGefUCAwEAAaNlMGMwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O
14
+ BBYEFBZFuuJYra4ctzMvtfzlTq3pW4idMBQGA1UdEQQNMAuBCWlvQGpzZy5pbzAU
15
+ BgNVHRIEDTALgQlpb0Bqc2cuaW8wDQYJKoZIhvcNAQELBQADggGBAF2jXh+4LzPg
16
+ Mj8gTCVTxj4/OhQ303cS5gasgh+R9Zxcf1cfTXOxEeIXdrqJiMhILWVWgCYZxagL
17
+ B5pU6MOyIOi4XAm5RMO3mIyTrY05Q/JbQ5j2ccuIOlL+XvhjAYKZmt9BkJLKfLEp
18
+ hagXHZe40Hd3qwkgJ3ug3T28gkBWaTq8FSbTOfCVX9uNjQrukAP/VrfnKZiNnpJ7
19
+ FOtEKDk3l9JWCyX8LQwfljPmQhvFHYnMRyt0l2b0I5li6MWUE4JmrbtK8/Fyo0I0
20
+ PChRryVpy0HnKvqoKQyqeMspLbbKCsO1PRS5VFZ/ybrPy63paFPdikeKMZIH3oJQ
21
+ wePRJFEp3gdQZcSjfkzC38QFh9JuKFJFMC4Y3e5uVeeydEGGUekH1s/B5/EuBCRi
22
+ vaoldWA6C6e/X/XQsMNdC04+d7IdgNRf8NkN/atZdJLcFVcaXGS9MncPZnwBC2GD
23
+ GBMFs3s6mx0HRpclYTMXK71jLsfqtM2SriIbx1VqiuUMUtddNrGcOw==
24
+ -----END CERTIFICATE-----
data/ext/sha3/digest.c CHANGED
@@ -21,232 +21,250 @@ VALUE eSHA3DigestError;
21
21
 
22
22
  static void free_allox(MDX *mdx)
23
23
  {
24
- if (mdx) {
25
- if (mdx->state) {
26
- free(mdx->state);
24
+ if (mdx)
25
+ {
26
+ if (mdx->state)
27
+ {
28
+ free(mdx->state);
29
+ }
30
+
31
+ free(mdx);
27
32
  }
28
33
 
29
- free(mdx);
30
- }
31
-
32
- return;
34
+ return;
33
35
  }
34
36
 
35
37
  static VALUE c_digest_alloc(VALUE klass)
36
38
  {
37
- MDX *mdx;
38
- VALUE obj;
39
+ MDX *mdx;
40
+ VALUE obj;
39
41
 
40
- mdx = (MDX *) malloc(sizeof(MDX));
41
- if (!mdx) {
42
- rb_raise(eSHA3DigestError, "failed to allocate object memory");
43
- }
42
+ mdx = (MDX *)malloc(sizeof(MDX));
43
+ if (!mdx)
44
+ {
45
+ rb_raise(eSHA3DigestError, "failed to allocate object memory");
46
+ }
44
47
 
45
- mdx->state = (Keccak_HashInstance *) malloc(sizeof(Keccak_HashInstance));
46
- if (!mdx->state) {
47
- free_allox(mdx);
48
- rb_raise(eSHA3DigestError, "failed to allocate state memory");
49
- }
48
+ mdx->state = (Keccak_HashInstance *)malloc(sizeof(Keccak_HashInstance));
49
+ if (!mdx->state)
50
+ {
51
+ free_allox(mdx);
52
+ rb_raise(eSHA3DigestError, "failed to allocate state memory");
53
+ }
50
54
 
51
- obj = Data_Wrap_Struct(klass, 0, free_allox, mdx);
55
+ obj = Data_Wrap_Struct(klass, 0, free_allox, mdx);
52
56
 
53
- memset(mdx->state, 0, sizeof(Keccak_HashInstance));
54
- mdx->hashbitlen = 0;
57
+ memset(mdx->state, 0, sizeof(Keccak_HashInstance));
58
+ mdx->hashbitlen = 0;
55
59
 
56
- return obj;
60
+ return obj;
57
61
  }
58
62
 
59
63
  static VALUE c_digest_update(VALUE, VALUE);
60
64
 
61
65
  HashReturn c_keccak_hash_initialize(MDX *mdx)
62
66
  {
63
- HashReturn r = FAIL;
67
+ HashReturn r = FAIL;
64
68
 
65
- switch (mdx->hashbitlen) {
69
+ switch (mdx->hashbitlen)
70
+ {
66
71
  case 224:
67
- r = Keccak_HashInitialize_SHA3_224(mdx->state);
68
- break;
72
+ r = Keccak_HashInitialize_SHA3_224(mdx->state);
73
+ break;
69
74
  case 256:
70
- r = Keccak_HashInitialize_SHA3_256(mdx->state);
71
- break;
75
+ r = Keccak_HashInitialize_SHA3_256(mdx->state);
76
+ break;
72
77
  case 384:
73
- r = Keccak_HashInitialize_SHA3_384(mdx->state);
74
- break;
78
+ r = Keccak_HashInitialize_SHA3_384(mdx->state);
79
+ break;
75
80
  case 512:
76
- r = Keccak_HashInitialize_SHA3_512(mdx->state);
77
- break;
81
+ r = Keccak_HashInitialize_SHA3_512(mdx->state);
82
+ break;
78
83
  }
79
84
 
80
- return r;
85
+ return r;
81
86
  }
82
87
 
83
88
  // SHA3::Digest.new(type, [data]) -> self
84
89
  static VALUE c_digest_init(int argc, VALUE *argv, VALUE self)
85
90
  {
86
- MDX *mdx;
87
- VALUE hlen, data;
91
+ MDX *mdx;
92
+ VALUE hlen, data;
88
93
 
89
- rb_scan_args(argc, argv, "02", &hlen, &data);
90
- GETMDX(self, mdx);
94
+ rb_scan_args(argc, argv, "02", &hlen, &data);
95
+ GETMDX(self, mdx);
91
96
 
92
- if (!NIL_P(hlen)) {
93
- mdx->hashbitlen = get_hlen(hlen);
94
- } else {
95
- mdx->hashbitlen = 256;
96
- }
97
+ if (!NIL_P(hlen))
98
+ {
99
+ mdx->hashbitlen = get_hlen(hlen);
100
+ }
101
+ else
102
+ {
103
+ mdx->hashbitlen = 256;
104
+ }
97
105
 
98
- if (c_keccak_hash_initialize(mdx) != SUCCESS) {
99
- rb_raise(eSHA3DigestError, "failed to initialize algorithm state");
100
- }
106
+ if (c_keccak_hash_initialize(mdx) != SUCCESS)
107
+ {
108
+ rb_raise(eSHA3DigestError, "failed to initialize algorithm state");
109
+ }
101
110
 
102
- if (!NIL_P(data)) {
103
- return c_digest_update(self, data);
104
- }
111
+ if (!NIL_P(data))
112
+ {
113
+ return c_digest_update(self, data);
114
+ }
105
115
 
106
- return self;
116
+ return self;
107
117
  }
108
118
 
109
119
  // SHA3::Digest.update(data) -> self
110
120
  static VALUE c_digest_update(VALUE self, VALUE data)
111
121
  {
112
- MDX *mdx;
113
- DataLength dlen;
122
+ MDX *mdx;
123
+ DataLength dlen;
114
124
 
115
- StringValue(data);
116
- GETMDX(self, mdx);
125
+ StringValue(data);
126
+ GETMDX(self, mdx);
117
127
 
118
- dlen = (RSTRING_LEN(data) * 8);
128
+ dlen = (RSTRING_LEN(data) * 8);
119
129
 
120
- if (Keccak_HashUpdate(mdx->state, (BitSequence *) RSTRING_PTR(data), dlen) != SUCCESS) {
121
- rb_raise(eSHA3DigestError, "failed to update hash data");
122
- }
130
+ if (Keccak_HashUpdate(mdx->state, (BitSequence *)RSTRING_PTR(data), dlen) != SUCCESS)
131
+ {
132
+ rb_raise(eSHA3DigestError, "failed to update hash data");
133
+ }
123
134
 
124
- return self;
135
+ return self;
125
136
  }
126
137
 
127
138
  // SHA3::Digest.reset() -> self
128
139
  static VALUE c_digest_reset(VALUE self)
129
140
  {
130
- MDX *mdx;
141
+ MDX *mdx;
131
142
 
132
- GETMDX(self, mdx);
143
+ GETMDX(self, mdx);
133
144
 
134
- memset(mdx->state, 0, sizeof(Keccak_HashInstance));
145
+ memset(mdx->state, 0, sizeof(Keccak_HashInstance));
135
146
 
136
- if (c_keccak_hash_initialize(mdx) != SUCCESS) {
137
- rb_raise(eSHA3DigestError, "failed to reset internal state");
138
- }
147
+ if (c_keccak_hash_initialize(mdx) != SUCCESS)
148
+ {
149
+ rb_raise(eSHA3DigestError, "failed to reset internal state");
150
+ }
139
151
 
140
- return self;
152
+ return self;
141
153
  }
142
154
 
143
155
  static int cmp_states(MDX *mdx1, MDX *mdx2)
144
156
  {
145
157
  return (
146
- (mdx1->hashbitlen == mdx2->hashbitlen) &&
147
- (strcmp((const char *) mdx1->state->sponge.state, (const char *)mdx2->state->sponge.state) == 0) &&
148
- (mdx1->state->sponge.rate == mdx2->state->sponge.rate) &&
149
- (mdx1->state->sponge.byteIOIndex == mdx2->state->sponge.byteIOIndex) &&
150
- (mdx1->state->sponge.squeezing == mdx2->state->sponge.squeezing) &&
151
- (mdx1->state->fixedOutputLength == mdx2->state->fixedOutputLength) &&
152
- (mdx1->state->delimitedSuffix == mdx2->state->delimitedSuffix)
153
- );
158
+ (mdx1->hashbitlen == mdx2->hashbitlen) &&
159
+ (strcmp((const char *)mdx1->state->sponge.state, (const char *)mdx2->state->sponge.state) == 0) &&
160
+ (mdx1->state->sponge.rate == mdx2->state->sponge.rate) &&
161
+ (mdx1->state->sponge.byteIOIndex == mdx2->state->sponge.byteIOIndex) &&
162
+ (mdx1->state->sponge.squeezing == mdx2->state->sponge.squeezing) &&
163
+ (mdx1->state->fixedOutputLength == mdx2->state->fixedOutputLength) &&
164
+ (mdx1->state->delimitedSuffix == mdx2->state->delimitedSuffix));
154
165
  }
155
166
 
156
167
  // SHA3::Digest.copy(obj) -> self
157
168
  static VALUE c_digest_copy(VALUE self, VALUE obj)
158
169
  {
159
- MDX *mdx1, *mdx2;
170
+ MDX *mdx1, *mdx2;
160
171
 
161
- rb_check_frozen(self);
162
- if (self == obj) {
163
- return self;
164
- }
172
+ rb_check_frozen(self);
173
+ if (self == obj)
174
+ {
175
+ return self;
176
+ }
177
+
178
+ GETMDX(self, mdx1);
179
+ SAFEGETMDX(obj, mdx2);
165
180
 
166
- GETMDX(self, mdx1);
167
- SAFEGETMDX(obj, mdx2);
181
+ memcpy(mdx1->state, mdx2->state, sizeof(Keccak_HashInstance));
182
+ mdx1->hashbitlen = mdx2->hashbitlen;
168
183
 
169
- memcpy(mdx1->state, mdx2->state, sizeof(Keccak_HashInstance));
170
- mdx1->hashbitlen = mdx2->hashbitlen;
184
+ // Fetch the data again to make sure it was copied
185
+ GETMDX(self, mdx1);
186
+ SAFEGETMDX(obj, mdx2);
171
187
 
172
- // Fetch the data again to make sure it was copied
173
- GETMDX(self, mdx1);
174
- SAFEGETMDX(obj, mdx2);
175
- if (!cmp_states(mdx1, mdx2)) {
176
- rb_raise(eSHA3DigestError, "failed to copy state");
177
- }
188
+ if (!cmp_states(mdx1, mdx2))
189
+ {
190
+ rb_raise(eSHA3DigestError, "failed to copy state");
191
+ }
178
192
 
179
- return self;
193
+ return self;
180
194
  }
181
195
 
182
196
  // SHA3::Digest.digest_length -> Integer
183
197
  static VALUE c_digest_length(VALUE self)
184
198
  {
185
- MDX *mdx;
186
- GETMDX(self, mdx);
199
+ MDX *mdx;
200
+ GETMDX(self, mdx);
187
201
 
188
- return ULL2NUM(mdx->hashbitlen / 8);
202
+ return ULL2NUM(mdx->hashbitlen / 8);
189
203
  }
190
204
 
191
205
  // SHA3::Digest.block_length -> Integer
192
206
  static VALUE c_digest_block_length(VALUE self)
193
207
  {
194
- MDX *mdx;
195
- GETMDX(self, mdx);
208
+ MDX *mdx;
209
+ GETMDX(self, mdx);
196
210
 
197
- return ULL2NUM(200 - (2 * (mdx->hashbitlen / 8)));
211
+ return ULL2NUM(200 - (2 * (mdx->hashbitlen / 8)));
198
212
  }
199
213
 
200
214
  // SHA3::Digest.name -> String
201
215
  static VALUE c_digest_name(VALUE self)
202
216
  {
203
- return rb_str_new2("SHA3");
217
+ return rb_str_new2("SHA3");
204
218
  }
205
219
 
206
220
  // SHA3::Digest.finish() -> String
207
221
  static VALUE c_digest_finish(int argc, VALUE *argv, VALUE self)
208
222
  {
209
- MDX *mdx;
210
- VALUE str;
223
+ MDX *mdx;
224
+ VALUE str;
211
225
 
212
- rb_scan_args(argc, argv, "01", &str);
213
- GETMDX(self, mdx);
226
+ rb_scan_args(argc, argv, "01", &str);
227
+ GETMDX(self, mdx);
214
228
 
215
- if (NIL_P(str)) {
216
- str = rb_str_new(0, mdx->hashbitlen / 8);
217
- } else {
218
- StringValue(str);
219
- rb_str_resize(str, mdx->hashbitlen / 8);
220
- }
229
+ if (NIL_P(str))
230
+ {
231
+ str = rb_str_new(0, mdx->hashbitlen / 8);
232
+ }
233
+ else
234
+ {
235
+ StringValue(str);
236
+ rb_str_resize(str, mdx->hashbitlen / 8);
237
+ }
221
238
 
222
- if (Keccak_HashFinal(mdx->state, (BitSequence *) RSTRING_PTR(str)) != SUCCESS) {
223
- rb_raise(eSHA3DigestError, "failed to finalize digest");
224
- }
239
+ if (Keccak_HashFinal(mdx->state, (BitSequence *)RSTRING_PTR(str)) != SUCCESS)
240
+ {
241
+ rb_raise(eSHA3DigestError, "failed to finalize digest");
242
+ }
225
243
 
226
- return str;
244
+ return str;
227
245
  }
228
246
 
229
247
  void Init_sha3_n_digest()
230
248
  {
231
- rb_require("digest");
232
-
233
- /* SHA3::Digest (class) */
234
- cSHA3Digest = rb_define_class_under(mSHA3, "Digest", rb_path2class("Digest::Class"));
235
- /* SHA3::Digest::DigestError (class) */
236
- eSHA3DigestError = rb_define_class_under(cSHA3Digest, "DigestError", rb_eStandardError);
237
-
238
- // SHA3::Digest (class) methods
239
- rb_define_alloc_func(cSHA3Digest, c_digest_alloc);
240
- rb_define_method(cSHA3Digest, "initialize", c_digest_init, -1);
241
- rb_define_method(cSHA3Digest, "update", c_digest_update, 1);
242
- rb_define_method(cSHA3Digest, "reset", c_digest_reset, 0);
243
- rb_define_method(cSHA3Digest, "initialize_copy", c_digest_copy, 1);
244
- rb_define_method(cSHA3Digest, "digest_length", c_digest_length, 0);
245
- rb_define_method(cSHA3Digest, "block_length", c_digest_block_length, 0);
246
- rb_define_method(cSHA3Digest, "name", c_digest_name, 0);
247
- rb_define_private_method(cSHA3Digest, "finish", c_digest_finish, -1);
248
-
249
- rb_define_alias(cSHA3Digest, "<<", "update");
250
-
251
- return;
249
+ rb_require("digest");
250
+
251
+ /* SHA3::Digest (class) */
252
+ cSHA3Digest = rb_define_class_under(mSHA3, "Digest", rb_path2class("Digest::Class"));
253
+ /* SHA3::Digest::DigestError (class) */
254
+ eSHA3DigestError = rb_define_class_under(cSHA3Digest, "DigestError", rb_eStandardError);
255
+
256
+ // SHA3::Digest (class) methods
257
+ rb_define_alloc_func(cSHA3Digest, c_digest_alloc);
258
+ rb_define_method(cSHA3Digest, "initialize", c_digest_init, -1);
259
+ rb_define_method(cSHA3Digest, "update", c_digest_update, 1);
260
+ rb_define_method(cSHA3Digest, "reset", c_digest_reset, 0);
261
+ rb_define_method(cSHA3Digest, "initialize_copy", c_digest_copy, 1);
262
+ rb_define_method(cSHA3Digest, "digest_length", c_digest_length, 0);
263
+ rb_define_method(cSHA3Digest, "block_length", c_digest_block_length, 0);
264
+ rb_define_method(cSHA3Digest, "name", c_digest_name, 0);
265
+ rb_define_private_method(cSHA3Digest, "finish", c_digest_finish, -1);
266
+
267
+ rb_define_alias(cSHA3Digest, "<<", "update");
268
+
269
+ return;
252
270
  }
data/ext/sha3/digest.h CHANGED
@@ -3,37 +3,45 @@
3
3
  #ifndef _DIGEST_H_
4
4
  #define _DIGEST_H_
5
5
 
6
- #ifdef __cplusplus
7
- extern "C" {
6
+ #ifdef __cplusplus
7
+ extern "C"
8
+ {
8
9
  #endif
9
10
 
10
11
  // From ruby/ext/openssl/ossl_digest.c
11
- #define GETMDX(obj, mdx) do { \
12
- Data_Get_Struct((obj), MDX, (mdx)); \
13
- if (!(mdx)) { \
14
- rb_raise(rb_eRuntimeError, "Digest data not initialized!"); \
15
- } \
16
- } while (0)
17
-
18
- #define SAFEGETMDX(obj, mdx) do { \
19
- if (!rb_obj_is_kind_of(obj, cSHA3Digest)) { \
20
- rb_raise(rb_eTypeError, "wrong argument (%s)! (expected %s)",\
21
- rb_obj_classname(obj), rb_class2name(cSHA3Digest)); \
22
- } \
23
- GETMDX(obj, mdx); \
24
- } while(0)
25
-
26
- extern VALUE cSHA3Digest;
27
- extern VALUE eSHA3DigestError;
28
-
29
- typedef struct {
30
- Keccak_HashInstance *state;
31
- int hashbitlen;
32
- } MDX;
33
-
34
- void Init_sha3_n_digest(void);
35
-
36
- #ifdef __cplusplus
12
+ #define GETMDX(obj, mdx) \
13
+ do \
14
+ { \
15
+ Data_Get_Struct((obj), MDX, (mdx)); \
16
+ if (!(mdx)) \
17
+ { \
18
+ rb_raise(rb_eRuntimeError, "Digest data not initialized!"); \
19
+ } \
20
+ } while (0)
21
+
22
+ #define SAFEGETMDX(obj, mdx) \
23
+ do \
24
+ { \
25
+ if (!rb_obj_is_kind_of(obj, cSHA3Digest)) \
26
+ { \
27
+ rb_raise(rb_eTypeError, "wrong argument (%s)! (expected %s)", \
28
+ rb_obj_classname(obj), rb_class2name(cSHA3Digest)); \
29
+ } \
30
+ GETMDX(obj, mdx); \
31
+ } while (0)
32
+
33
+ extern VALUE cSHA3Digest;
34
+ extern VALUE eSHA3DigestError;
35
+
36
+ typedef struct
37
+ {
38
+ Keccak_HashInstance *state;
39
+ int hashbitlen;
40
+ } MDX;
41
+
42
+ void Init_sha3_n_digest(void);
43
+
44
+ #ifdef __cplusplus
37
45
  }
38
46
  #endif
39
47