digest-sha3 1.0.1 → 1.0.2

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.
data.tar.gz.asc CHANGED
@@ -2,7 +2,11 @@
2
2
  Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
3
3
  Comment: GPGTools - http://gpgtools.org
4
4
 
5
- iEYEABECAAYFAlE+IngACgkQBqExCUtvQzLnZwCdG/Nk+tuRvO+wEaGf46BVEA4a
6
- uHcAoKZ2LgmqNEwmsDnqnvErgvi6BUDz
7
- =3siJ
5
+ iQEcBAABAgAGBQJRnfcdAAoJECrHRaUKISqMiYIH/AoNOUG3VBjW9BJWSkRjJr7H
6
+ QyweWpKE+H7AEAzIyZnMH4qICKBfNfRgMVL6H07Ie+xX8Tm2YBOoHWYXvn2So5bL
7
+ SULpEktHsbc0QSEEWnMUb9oA91qM3e514oJ+xhGQokvVZoFeG6q/WSunuY3TE7Y/
8
+ nxe5PiYCkwCCWdPRtMhgOKgPjK9RDmINml58NI46CL4wscbduXzrv0U91CjVkzsj
9
+ WkCiXCoYQ0hb7vzofbtMppFEhBtO9XlfUoAfl6u7OQOyh553swIP4xoVTKEQ9DMz
10
+ RL6MAzaMtmzCgijT7JYGza/Cmezg0dRzrwWtLPpX43RIk4jWdFCBcwNSHHB5CXA=
11
+ =zd36
8
12
  -----END PGP SIGNATURE-----
data/Makefile CHANGED
@@ -7,7 +7,7 @@ ext/digest/Makefile: ext/digest/extconf.rb
7
7
  cd ext/digest && ruby extconf.rb
8
8
 
9
9
  clean:
10
- if [[ -f ext/digest/Makefile ]]; then make -C ext/digest clean; fi
10
+ if [ -f ext/digest/Makefile ]; then make -C ext/digest clean; fi
11
11
  rm -f ext/digest/Makefile
12
12
  rm -f test/test_vectors.rb
13
13
 
@@ -1,3 +1,7 @@
1
1
  require 'mkmf'
2
+
3
+ have_header('ruby/digest.h')
4
+ have_func('rb_str_set_len')
5
+
2
6
  $CFLAGS << " -fvisibility=hidden"
3
7
  create_makefile('digest/sha3')
@@ -1,137 +1,130 @@
1
1
  #include "ruby.h"
2
+ #ifdef HAVE_RUBY_DIGEST_H
3
+ #include "ruby/digest.h"
4
+ #else
5
+ #include "digest.h"
6
+ #endif
2
7
  #include "KeccakNISTInterface.h"
3
8
 
4
9
  #define MAX_DIGEST_SIZE 64
10
+ #define DEFAULT_DIGEST_LEN 512
11
+
12
+ static void sha3_init_func(hashState *ctx);
13
+ static void sha3_update_func(hashState *ctx, unsigned char *str, size_t len);
14
+
15
+ static rb_digest_metadata_t sha3 = {
16
+ RUBY_DIGEST_API_VERSION,
17
+ DEFAULT_DIGEST_LEN,
18
+ KeccakPermutationSize - (2 * DEFAULT_DIGEST_LEN),
19
+ sizeof(hashState),
20
+ (rb_digest_hash_init_func_t)sha3_init_func,
21
+ (rb_digest_hash_update_func_t)sha3_update_func,
22
+ NULL,
23
+ };
24
+
25
+ static void
26
+ sha3_init(hashState *ctx, size_t bitlen) {
27
+ switch (Init(ctx, bitlen)) {
28
+ case SUCCESS:
29
+ return;
30
+ case FAIL:
31
+ rb_raise(rb_eRuntimeError, "Unknown error");
32
+ case BAD_HASHLEN:
33
+ rb_raise(rb_eArgError, "Bad hash length (must be 0, 224, 256, 384 or 512)");
34
+ default:
35
+ rb_raise(rb_eRuntimeError, "Unknown error code");
36
+ }
37
+ }
5
38
 
6
- static VALUE mDigest, cSHA3;
39
+ static void
40
+ sha3_init_func(hashState *ctx) {
41
+ Init(ctx, ctx->capacity / 2);
42
+ }
7
43
 
8
- typedef struct {
9
- hashState state;
10
- int bitlen;
11
- } RbSHA3;
44
+ static void
45
+ sha3_update_func(hashState *ctx, unsigned char *str, size_t len) {
46
+ Update(ctx, str, len * 8);
47
+ }
12
48
 
13
49
  static VALUE
14
50
  rb_sha3_alloc(VALUE klass) {
15
- RbSHA3 *ctx;
16
-
17
- ctx = (RbSHA3 *) xmalloc(sizeof(RbSHA3));
18
- ctx->bitlen = -1;
51
+ hashState *ctx;
52
+
53
+ ctx = (hashState *) xmalloc(sizeof(hashState));
54
+ sha3_init(ctx, DEFAULT_DIGEST_LEN);
19
55
  return Data_Wrap_Struct(klass, 0, xfree, ctx);
20
56
  }
21
57
 
22
58
  static VALUE
23
59
  rb_sha3_initialize(int argc, VALUE *argv, VALUE self) {
24
- RbSHA3 *ctx;
60
+ hashState *ctx;
25
61
  VALUE hashlen;
26
62
  int i_hashlen;
27
63
 
28
64
  if (rb_scan_args(argc, argv, "01", &hashlen) == 0) {
29
- i_hashlen = 512;
65
+ i_hashlen = DEFAULT_DIGEST_LEN;
30
66
  } else {
31
67
  i_hashlen = NUM2INT(hashlen);
32
68
  }
33
- if (i_hashlen == 0) {
34
- rb_raise(rb_eRuntimeError, "Unsupported hash length");
35
- }
36
-
37
- Data_Get_Struct(self, RbSHA3, ctx);
38
- ctx->bitlen = i_hashlen;
39
-
40
- switch (Init(&ctx->state, i_hashlen)) {
41
- case SUCCESS:
42
- return self;
43
- case FAIL:
44
- rb_raise(rb_eRuntimeError, "Unknown error");
45
- return Qnil;
46
- case BAD_HASHLEN:
47
- rb_raise(rb_eRuntimeError, "Bad hash length (must be 0, 224, 256, 384 or 512)");
48
- return Qnil;
69
+ switch (i_hashlen) {
70
+ case 0:
71
+ rb_raise(rb_eArgError, "Unsupported hash length");
72
+ case DEFAULT_DIGEST_LEN:
73
+ break;
49
74
  default:
50
- rb_raise(rb_eRuntimeError, "Unknown error code");
51
- return Qnil;
75
+ Data_Get_Struct(self, hashState, ctx);
76
+ sha3_init(ctx, i_hashlen);
52
77
  }
53
- }
54
78
 
55
- static VALUE
56
- rb_sha3_initialize_copy(VALUE self, VALUE other) {
57
- RbSHA3 *ctx_self, *ctx_other;
58
-
59
- rb_check_frozen(self);
60
- Data_Get_Struct(self, RbSHA3, ctx_self);
61
- Data_Get_Struct(other, RbSHA3, ctx_other);
62
- memcpy(&ctx_self->state, &ctx_other->state, sizeof(hashState));
63
- ctx_self->bitlen = ctx_other->bitlen;
64
79
  return self;
65
80
  }
66
81
 
67
82
  static VALUE
68
- rb_sha3_reset(VALUE self) {
69
- RbSHA3 *ctx;
83
+ rb_sha3_finish(VALUE self) {
84
+ hashState *ctx;
85
+ VALUE digest;
70
86
 
71
- Data_Get_Struct(self, RbSHA3, ctx);
72
- Init(&ctx->state, ctx->bitlen);
73
- return self;
74
- }
87
+ Data_Get_Struct(self, hashState, ctx);
75
88
 
76
- static VALUE
77
- rb_sha3_update(VALUE self, VALUE str) {
78
- RbSHA3 *ctx;
89
+ digest = rb_str_new(0, ctx->capacity / 2 / 8);
79
90
 
80
- Data_Get_Struct(self, RbSHA3, ctx);
81
- Update(&ctx->state, RSTRING_PTR(str), RSTRING_LEN(str) * 8);
82
- return self;
91
+ Final(ctx, (unsigned char *)RSTRING_PTR(digest));
92
+
93
+ return digest;
83
94
  }
84
95
 
85
96
  static VALUE
86
- rb_sha3_digest(VALUE self, VALUE str) {
87
- RbSHA3 *ctx;
88
- hashState state;
89
- unsigned char digest[MAX_DIGEST_SIZE];
90
-
91
- Data_Get_Struct(self, RbSHA3, ctx);
92
- memcpy(&state, &ctx->state, sizeof(hashState));
93
- Final(&state, digest);
94
- return rb_str_new((const char *) digest, ctx->bitlen / 8);
97
+ rb_sha3_digest_length(VALUE self) {
98
+ hashState *ctx;
99
+
100
+ Data_Get_Struct(self, hashState, ctx);
101
+ return INT2FIX(ctx->capacity / 2 / 8);
95
102
  }
96
103
 
97
104
  static VALUE
98
- rb_sha3_singleton_digest(int argc, VALUE *argv, VALUE klass) {
99
- VALUE data, hashlen;
100
- int i_hashlen;
101
- unsigned char digest[MAX_DIGEST_SIZE];
105
+ rb_sha3_block_length(VALUE self) {
106
+ hashState *ctx;
102
107
 
103
- if (rb_scan_args(argc, argv, "11", &data, &hashlen) == 1) {
104
- i_hashlen = 512;
105
- } else {
106
- i_hashlen = NUM2INT(hashlen);
107
- }
108
-
109
- switch (Hash(i_hashlen, RSTRING_PTR(data), RSTRING_LEN(data) * 8, digest)) {
110
- case SUCCESS:
111
- return rb_str_new(digest, i_hashlen / 8);
112
- case FAIL:
113
- rb_raise(rb_eRuntimeError, "Unknown error");
114
- return Qnil;
115
- case BAD_HASHLEN:
116
- rb_raise(rb_eRuntimeError, "Bad hash length (must be 0, 224, 256, 384 or 512)");
117
- return Qnil;
118
- default:
119
- rb_raise(rb_eRuntimeError, "Unknown error code");
120
- return Qnil;
121
- }
108
+ Data_Get_Struct(self, hashState, ctx);
109
+ return INT2FIX(ctx->rate / 8);
122
110
  }
123
111
 
124
112
  void __attribute__((visibility("default")))
125
113
  Init_sha3() {
126
- mDigest = rb_define_module("Digest");
127
- cSHA3 = rb_define_class_under(mDigest, "SHA3", rb_cObject);
114
+ VALUE mDigest, cDigest_Base, cSHA3;
115
+
116
+ rb_require("digest");
117
+
118
+ mDigest = rb_path2class("Digest");
119
+ cDigest_Base = rb_path2class("Digest::Base");
120
+
121
+ cSHA3 = rb_define_class_under(mDigest, "SHA3", cDigest_Base);
122
+
123
+ rb_ivar_set(cSHA3, rb_intern("metadata"), Data_Wrap_Struct(rb_cObject, 0, 0, &sha3));
124
+
128
125
  rb_define_alloc_func(cSHA3, rb_sha3_alloc);
129
126
  rb_define_method(cSHA3, "initialize", rb_sha3_initialize, -1);
130
- rb_define_method(cSHA3, "initialize_copy", rb_sha3_initialize_copy, 1);
131
- rb_define_method(cSHA3, "reset", rb_sha3_reset, 0);
132
- rb_define_method(cSHA3, "update", rb_sha3_update, 1);
133
- rb_define_method(cSHA3, "<<", rb_sha3_update, 1);
134
- rb_define_method(cSHA3, "digest", rb_sha3_digest, 0);
135
- rb_define_singleton_method(cSHA3, "digest", rb_sha3_singleton_digest, -1);
136
- rb_require("digest/sha3/helpers");
127
+ rb_define_private_method(cSHA3, "finish", rb_sha3_finish, 0);
128
+ rb_define_method(cSHA3, "digest_length", rb_sha3_digest_length, 0);
129
+ rb_define_method(cSHA3, "block_length", rb_sha3_block_length, 0);
137
130
  }
@@ -1,7 +1,7 @@
1
1
  module Digest
2
2
  class SHA3
3
3
  module Version
4
- STRING = "1.0.1"
4
+ STRING = "1.0.2"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: digest-sha3
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-11 00:00:00.000000000 Z
13
+ date: 2013-05-23 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: The SHA-3 (Keccak) hash.
16
16
  email: software-signing@phusion.nl
@@ -35,7 +35,6 @@ files:
35
35
  - ext/digest/KeccakNISTInterface.h
36
36
  - ext/digest/KeccakSponge.h
37
37
  - ext/digest/extconf.rb
38
- - lib/digest/sha3/helpers.rb
39
38
  - lib/digest/sha3/version.rb
40
39
  homepage: https://github.com/phusion/digest-sha3-ruby
41
40
  licenses: []
metadata.gz.asc CHANGED
@@ -2,7 +2,11 @@
2
2
  Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
3
3
  Comment: GPGTools - http://gpgtools.org
4
4
 
5
- iEYEABECAAYFAlE+IngACgkQBqExCUtvQzIAlQCeJVNWeQeu5wA9XlvboL75T0Du
6
- 5sEAoK5eJl4M30VWMTkQWIgKxJKTyiJ2
7
- =Shqt
5
+ iQEcBAABAgAGBQJRnfcdAAoJECrHRaUKISqM52QH/2XN6fviuj7l8v5IMTgp6taA
6
+ oFdoPqVRcyJA+lU7gPK6goHJLPF62f/M0GBaRmpmnjVi0af5nUjB4kCgZC4aOe6t
7
+ N6eCV7We7nT+bSGZN33keaIceGa/V/b7xeK3o9tNbCxv01iMvigmCi86SXjpydvT
8
+ j9hFedYxGmwpkxAiNeoK7fqyChx3AUbNTcHAh9p+uA0liTvXaH8a7Ot+3R4itCdQ
9
+ fKA73NvVGSoIfD9Vs36obCAZeIQp1E9IIHsEODa+ogBRUNcJbclaNZF9XxUEukq9
10
+ gN9i1Anlf9Y3I9MO2NbZUi58PJg8VFl/bG98OHSbim5e8XYWz3Bfkcvojxtbldc=
11
+ =LIg6
8
12
  -----END PGP SIGNATURE-----
@@ -1,20 +0,0 @@
1
- # encoding: ascii
2
- Digest::SHA3.class_eval do
3
- def self.hexdigest(*args)
4
- force_ascii(digest(*args).unpack("H*").first)
5
- end
6
-
7
- def hexdigest
8
- Digest::SHA3.force_ascii(digest.unpack("H*").first)
9
- end
10
-
11
- if ''.respond_to?(:force_encoding)
12
- def self.force_ascii(str)
13
- str.force_encoding('ascii')
14
- end
15
- else
16
- def self.force_ascii(str)
17
- str
18
- end
19
- end
20
- end