cryptopp 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/Gemfile +14 -0
  4. data/Guardfile +17 -0
  5. data/MIT-LICENSE +1 -1
  6. data/Rakefile +20 -27
  7. data/VERSION +1 -0
  8. data/cryptopp.gemspec +7 -182
  9. data/ext/Rakefile +2 -2
  10. data/ext/ciphers.cpp +619 -619
  11. data/ext/cryptopp.cpp +220 -217
  12. data/ext/cryptopp_ruby_api.h +7 -7
  13. data/ext/defs/hashes.def +16 -0
  14. data/ext/defs/hmacs.def +16 -0
  15. data/ext/digests.cpp +635 -601
  16. data/ext/extconf.rb +61 -19
  17. data/ext/j3way.cpp +5 -5
  18. data/ext/j3way.h +6 -6
  19. data/ext/jadler32.h +6 -6
  20. data/ext/jaes.cpp +5 -5
  21. data/ext/jaes.h +6 -6
  22. data/ext/jarc4.cpp +5 -5
  23. data/ext/jarc4.h +6 -6
  24. data/ext/jbase.cpp +94 -94
  25. data/ext/jbase.h +49 -55
  26. data/ext/jbasiccipherinfo.h +23 -23
  27. data/ext/jblowfish.cpp +5 -5
  28. data/ext/jblowfish.h +6 -6
  29. data/ext/jcamellia.cpp +5 -7
  30. data/ext/jcamellia.h +6 -8
  31. data/ext/jcast128.cpp +5 -5
  32. data/ext/jcast128.h +6 -6
  33. data/ext/jcast256.cpp +5 -5
  34. data/ext/jcast256.h +6 -6
  35. data/ext/jcipher.cpp +62 -62
  36. data/ext/jcipher.h +24 -24
  37. data/ext/jcipher_t.h +383 -383
  38. data/ext/jconfig.h +29 -46
  39. data/ext/jconstants.h +120 -122
  40. data/ext/jcrc32.h +6 -6
  41. data/ext/jdes.cpp +5 -5
  42. data/ext/jdes.h +6 -6
  43. data/ext/jdes_ede2.cpp +5 -5
  44. data/ext/jdes_ede2.h +6 -6
  45. data/ext/jdes_ede3.cpp +5 -5
  46. data/ext/jdes_ede3.h +6 -6
  47. data/ext/jdes_xex3.cpp +5 -5
  48. data/ext/jdes_xex3.h +6 -6
  49. data/ext/jdiamond2.cpp +5 -5
  50. data/ext/jdiamond2.h +6 -6
  51. data/ext/jdiamond2lite.cpp +5 -5
  52. data/ext/jdiamond2lite.h +6 -6
  53. data/ext/jexception.h +5 -5
  54. data/ext/jgost.cpp +5 -5
  55. data/ext/jgost.h +6 -6
  56. data/ext/jhash.cpp +51 -51
  57. data/ext/jhash.h +22 -27
  58. data/ext/jhash_t.h +59 -59
  59. data/ext/jhaval.h +15 -15
  60. data/ext/jhelpers.cpp +54 -54
  61. data/ext/jhelpers.h +3 -3
  62. data/ext/jhmac.cpp +20 -20
  63. data/ext/jhmac.h +15 -15
  64. data/ext/jhmac_t.h +40 -72
  65. data/ext/jidea.cpp +5 -5
  66. data/ext/jidea.h +6 -6
  67. data/ext/jmarc4.cpp +5 -5
  68. data/ext/jmarc4.h +6 -6
  69. data/ext/jmars.cpp +5 -5
  70. data/ext/jmars.h +6 -6
  71. data/ext/jmd2.h +9 -9
  72. data/ext/jmd4.h +9 -9
  73. data/ext/jmd5.h +9 -9
  74. data/ext/jpanamacipher.cpp +7 -7
  75. data/ext/jpanamacipher.h +9 -9
  76. data/ext/jpanamahash.h +9 -9
  77. data/ext/jrc2.cpp +15 -15
  78. data/ext/jrc2.h +11 -11
  79. data/ext/jrc5.cpp +5 -5
  80. data/ext/jrc5.h +6 -6
  81. data/ext/jrc6.cpp +5 -5
  82. data/ext/jrc6.h +6 -6
  83. data/ext/jripemd160.h +28 -28
  84. data/ext/jsafer.cpp +7 -7
  85. data/ext/jsafer.h +9 -9
  86. data/ext/jseal.cpp +7 -7
  87. data/ext/jseal.h +9 -9
  88. data/ext/jserpent.cpp +5 -5
  89. data/ext/jserpent.h +6 -6
  90. data/ext/jsha.h +38 -37
  91. data/ext/jsha3.h +130 -0
  92. data/ext/jsha3_blocksizes.h +73 -0
  93. data/ext/jshacal2.cpp +5 -5
  94. data/ext/jshacal2.h +6 -6
  95. data/ext/jshark.cpp +5 -7
  96. data/ext/jshark.h +6 -8
  97. data/ext/jsink.cpp +57 -57
  98. data/ext/jsink.h +127 -123
  99. data/ext/jskipjack.cpp +5 -5
  100. data/ext/jskipjack.h +6 -6
  101. data/ext/jsquare.cpp +5 -5
  102. data/ext/jsquare.h +6 -6
  103. data/ext/jstream.cpp +3 -3
  104. data/ext/jstream.h +3 -3
  105. data/ext/jstream_t.h +74 -126
  106. data/ext/jtea.cpp +5 -5
  107. data/ext/jtea.h +6 -6
  108. data/ext/jtiger.h +9 -11
  109. data/ext/jtwofish.cpp +5 -5
  110. data/ext/jtwofish.h +6 -6
  111. data/ext/jwhirlpool.h +9 -11
  112. data/ext/utils.cpp +3 -3
  113. data/extras/parser_c.rb +102 -102
  114. data/test/ciphers_test.rb +24 -30
  115. data/test/data/ciphers/3desval.yml +13 -0
  116. data/test/data/ciphers/3wayval.yml +17 -0
  117. data/test/data/ciphers/arc4.yml +22 -0
  118. data/test/data/ciphers/blowfish.yml +9 -0
  119. data/test/data/ciphers/camellia.yml +21 -0
  120. data/test/data/ciphers/cast128v.yml +13 -0
  121. data/test/data/ciphers/cast256v.yml +13 -0
  122. data/test/data/ciphers/descert.yml +723 -0
  123. data/test/data/ciphers/diamond.yml +31 -0
  124. data/test/data/ciphers/gostval.yml +33 -0
  125. data/test/data/ciphers/ideaval.yml +45 -0
  126. data/test/data/ciphers/marsval.yml +37 -0
  127. data/test/data/ciphers/panamac.yml +11 -0
  128. data/test/data/ciphers/rc2val.yml +49 -0
  129. data/test/data/ciphers/rc5val.yml +26 -0
  130. data/test/data/ciphers/rc6val.yml +25 -0
  131. data/test/data/ciphers/rijndael.yml +37 -0
  132. data/test/data/ciphers/saferval.yml +77 -0
  133. data/test/data/ciphers/seal.yml +6 -0
  134. data/test/data/ciphers/serpentv.yml +37 -0
  135. data/test/data/ciphers/shacal2.yml +21 -0
  136. data/test/data/ciphers/sharkval.yml +29 -0
  137. data/test/data/ciphers/skipjack.yml +5 -0
  138. data/test/data/ciphers/squareva.yml +33 -0
  139. data/test/data/ciphers/twofishv.yml +37 -0
  140. data/test/data/digests/adler32.yml +19 -0
  141. data/test/data/digests/crc32.yml +25 -0
  142. data/test/data/digests/haval.yml +7 -0
  143. data/test/data/digests/havalcer.yml +1 -0
  144. data/test/data/digests/md2.yml +22 -0
  145. data/test/data/digests/md4.yml +22 -0
  146. data/test/data/digests/md5.yml +22 -0
  147. data/test/data/digests/panamah.yml +13 -0
  148. data/test/data/digests/ripemd.yml +97 -0
  149. data/test/data/digests/sha.yml +25 -0
  150. data/test/data/digests/sha3.yml +23 -0
  151. data/test/data/digests/tiger.yml +30 -0
  152. data/test/data/digests/whirlpool.yml +25 -0
  153. data/test/data/hmacs/hmac.yml +14 -0
  154. data/test/digests_test.rb +17 -23
  155. data/test/hmacs_test.rb +25 -31
  156. data/test/test_helper.rb +31 -37
  157. metadata +70 -82
  158. data/test/data/ciphers/3desval.dat +0 -7
  159. data/test/data/ciphers/3wayval.dat +0 -6
  160. data/test/data/ciphers/arc4.dat +0 -8
  161. data/test/data/ciphers/blowfish.dat +0 -5
  162. data/test/data/ciphers/camellia.dat +0 -7
  163. data/test/data/ciphers/cast128v.dat +0 -5
  164. data/test/data/ciphers/cast256v.dat +0 -5
  165. data/test/data/ciphers/descert.dat +0 -198
  166. data/test/data/ciphers/diamond.dat +0 -9
  167. data/test/data/ciphers/gostval.dat +0 -10
  168. data/test/data/ciphers/ideaval.dat +0 -13
  169. data/test/data/ciphers/marsval.dat +0 -11
  170. data/test/data/ciphers/panamac.dat +0 -7
  171. data/test/data/ciphers/rc2val.dat +0 -10
  172. data/test/data/ciphers/rc5val.dat +0 -7
  173. data/test/data/ciphers/rc6val.dat +0 -8
  174. data/test/data/ciphers/rijndael.dat +0 -11
  175. data/test/data/ciphers/saferval.dat +0 -27
  176. data/test/data/ciphers/seal.dat +0 -3
  177. data/test/data/ciphers/serpentv.dat +0 -11
  178. data/test/data/ciphers/shacal2.dat +0 -7
  179. data/test/data/ciphers/sharkval.dat +0 -9
  180. data/test/data/ciphers/skipjack.dat +0 -3
  181. data/test/data/ciphers/squareva.dat +0 -10
  182. data/test/data/ciphers/twofishv.dat +0 -11
  183. data/test/data/digests/adler32.dat +0 -8
  184. data/test/data/digests/crc32.dat +0 -10
  185. data/test/data/digests/haval.dat +0 -4
  186. data/test/data/digests/havalcer.dat +0 -23
  187. data/test/data/digests/md2.dat +0 -9
  188. data/test/data/digests/md4.dat +0 -9
  189. data/test/data/digests/md5.dat +0 -9
  190. data/test/data/digests/panamah.dat +0 -8
  191. data/test/data/digests/ripemd.dat +0 -43
  192. data/test/data/digests/sha.dat +0 -19
  193. data/test/data/digests/tiger.dat +0 -11
  194. data/test/data/digests/whirlpool.dat +0 -13
  195. data/test/data/hmacs/hmac.dat +0 -6
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*
3
- * Copyright (c) 2002-2010 J Smith <dark.panda@gmail.com>
4
- * Crypto++ copyright (c) 1995-2010 Wei Dai
5
- * See COPYING for the extact license
3
+ * Copyright (c) 2002-2014 J Smith <dark.panda@gmail.com>
4
+ * Crypto++ copyright (c) 1995-2013 Wei Dai
5
+ * See MIT-LICENSE for the extact license
6
6
  */
7
7
 
8
8
  #ifndef __CRYPTOPP_RUBY_API_H__
@@ -17,19 +17,19 @@ extern VALUE rb_cCryptoPP_Digest;
17
17
  extern VALUE rb_cCryptoPP_Digest_HMAC;
18
18
 
19
19
  #define CIPHER_ALGORITHM_X(klass, r, c, s) \
20
- extern VALUE rb_cCryptoPP_Cipher_ ## r ;
20
+ extern VALUE rb_cCryptoPP_Cipher_ ## r ;
21
21
  #include "defs/ciphers.def"
22
22
 
23
23
  #define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
24
- extern VALUE rb_cCryptoPP_Digest_ ## r ;
24
+ extern VALUE rb_cCryptoPP_Digest_ ## r ;
25
25
  #include "defs/checksums.def"
26
26
 
27
27
  #define HASH_ALGORITHM_X(klass, r, c, s) \
28
- extern VALUE rb_cCryptoPP_Digest_ ## r ;
28
+ extern VALUE rb_cCryptoPP_Digest_ ## r ;
29
29
  #include "defs/hashes.def"
30
30
 
31
31
  #define HMAC_ALGORITHM_X(klass, r, c, s) \
32
- extern VALUE rb_cCryptoPP_Digest_HMAC_ ## r ;
32
+ extern VALUE rb_cCryptoPP_Digest_HMAC_ ## r ;
33
33
  #include "defs/hmacs.def"
34
34
 
35
35
  VALUE rb_module_cipher_factory(int argc, VALUE *argv, VALUE self);
@@ -66,6 +66,22 @@ HASH_ALGORITHM_X(SHA384, SHA384, JSHA384, sha384)
66
66
  HASH_ALGORITHM_X(SHA512, SHA512, JSHA512, sha512)
67
67
  #endif
68
68
 
69
+ #if ENABLED_SHA3_224_HASH || defined(HASH_ALGORITHM_X_FORCE)
70
+ HASH_ALGORITHM_X(SHA3_224, SHA3_224, JSHA3_224, sha3_224)
71
+ #endif
72
+
73
+ #if ENABLED_SHA3_256_HASH || defined(HASH_ALGORITHM_X_FORCE)
74
+ HASH_ALGORITHM_X(SHA3_256, SHA3_256, JSHA3_256, sha3_256)
75
+ #endif
76
+
77
+ #if ENABLED_SHA3_384_HASH || defined(HASH_ALGORITHM_X_FORCE)
78
+ HASH_ALGORITHM_X(SHA3_384, SHA3_384, JSHA3_384, sha3_384)
79
+ #endif
80
+
81
+ #if ENABLED_SHA3_512_HASH || defined(HASH_ALGORITHM_X_FORCE)
82
+ HASH_ALGORITHM_X(SHA3_512, SHA3_512, JSHA3_512, sha3_512)
83
+ #endif
84
+
69
85
  #if ENABLED_TIGER_HASH || defined(HASH_ALGORITHM_X_FORCE)
70
86
  HASH_ALGORITHM_X(Tiger, TIGER, JTiger, tiger)
71
87
  #endif
@@ -42,6 +42,22 @@ HMAC_ALGORITHM_X(SHA384_HMAC, SHA384, JSHA384_HMAC, sha384_hmac)
42
42
  HMAC_ALGORITHM_X(SHA512_HMAC, SHA512, JSHA512_HMAC, sha512_hmac)
43
43
  #endif
44
44
 
45
+ #if ENABLED_SHA3_224_HMAC || HMAC_ALGORITHM_X_FORCE
46
+ HMAC_ALGORITHM_X(SHA3_224_HMAC, SHA3_224, JSHA3_224_HMAC, sha3_224_hmac)
47
+ #endif
48
+
49
+ #if ENABLED_SHA3_256_HMAC || HMAC_ALGORITHM_X_FORCE
50
+ HMAC_ALGORITHM_X(SHA3_256_HMAC, SHA3_256, JSHA3_256_HMAC, sha3_256_hmac)
51
+ #endif
52
+
53
+ #if ENABLED_SHA3_384_HMAC || HMAC_ALGORITHM_X_FORCE
54
+ HMAC_ALGORITHM_X(SHA3_384_HMAC, SHA3_384, JSHA3_384_HMAC, sha3_384_hmac)
55
+ #endif
56
+
57
+ #if ENABLED_SHA3_512_HMAC || HMAC_ALGORITHM_X_FORCE
58
+ HMAC_ALGORITHM_X(SHA3_512_HMAC, SHA3_512, JSHA3_512_HMAC, sha3_512_hmac)
59
+ #endif
60
+
45
61
  #if ENABLED_TIGER_HMAC || HMAC_ALGORITHM_X_FORCE
46
62
  HMAC_ALGORITHM_X(Tiger_HMAC, TIGER, JTiger_HMAC, tiger_hmac)
47
63
  #endif
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*
3
- * Copyright (c) 2002-2010 J Smith <dark.panda@gmail.com>
4
- * Crypto++ copyright (c) 1995-2010 Wei Dai
5
- * See COPYING for the extact license
3
+ * Copyright (c) 2002-2014 J Smith <dark.panda@gmail.com>
4
+ * Crypto++ copyright (c) 1995-2013 Wei Dai
5
+ * See MIT-LICENSE for the extact license
6
6
  */
7
7
 
8
8
  // hash algorithms:
@@ -16,6 +16,7 @@
16
16
  #include "jpanamahash.h"
17
17
  #include "jripemd160.h"
18
18
  #include "jsha.h"
19
+ #include "jsha3.h"
19
20
  #include "jtiger.h"
20
21
  #include "jwhirlpool.h"
21
22
 
@@ -29,6 +30,8 @@ extern void hash_free(JHash *c);
29
30
  // forward declarations
30
31
 
31
32
  static HashEnum digest_sym_to_const(VALUE hash);
33
+ static bool digest_is_hmac(HashEnum hash);
34
+ static bool digest_is_non_hmac(HashEnum hash);
32
35
  static bool digest_enabled(HashEnum hash);
33
36
  static void digest_options(VALUE self, VALUE options);
34
37
  static JHash* digest_factory(VALUE algorithm);
@@ -48,163 +51,194 @@ static string module_hmac_digest(int argc, VALUE *argv, VALUE self, bool hex);
48
51
 
49
52
  static HashEnum digest_sym_to_const(VALUE c)
50
53
  {
51
- HashEnum hash = UNKNOWN_HASH;
52
- ID id = SYM2ID(c);
53
-
54
- if (id == rb_intern("panama")) {
55
- hash = PANAMA_HASH;
56
- }
57
- else if (id == rb_intern("sha")) {
58
- hash = SHA1_HASH;
59
- }
60
- else if (id == rb_intern("sha_hmac")) {
61
- hash = SHA1_HMAC;
62
- }
63
- # define CHECKSUM_ALGORITHM_X_FORCE 1
64
- # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
65
- else if (id == rb_intern(# s)) { \
66
- hash = r ## _CHECKSUM; \
67
- }
68
- # include "defs/checksums.def"
69
-
70
- # define HASH_ALGORITHM_X_FORCE 1
71
- # define HASH_ALGORITHM_X(klass, r, c, s) \
72
- else if (id == rb_intern(# s)) { \
73
- hash = r ## _HASH; \
74
- }
75
- # include "defs/hashes.def"
76
-
77
- # define HMAC_ALGORITHM_X_FORCE 1
78
- # define HMAC_ALGORITHM_X(klass, r, c, s) \
79
- else if (id == rb_intern(# s)) { \
80
- hash = r ## _HMAC; \
81
- }
82
- # include "defs/hmacs.def"
83
- return hash;
54
+ HashEnum hash = UNKNOWN_HASH;
55
+ ID id = SYM2ID(c);
56
+
57
+ if (id == rb_intern("panama")) {
58
+ hash = PANAMA_HASH;
59
+ }
60
+ else if (id == rb_intern("sha")) {
61
+ hash = SHA1_HASH;
62
+ }
63
+ else if (id == rb_intern("sha_hmac")) {
64
+ hash = SHA1_HMAC;
65
+ }
66
+ # define CHECKSUM_ALGORITHM_X_FORCE 1
67
+ # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
68
+ else if (id == rb_intern(# s)) { \
69
+ hash = r ## _CHECKSUM; \
70
+ }
71
+ # include "defs/checksums.def"
72
+
73
+ # define HASH_ALGORITHM_X_FORCE 1
74
+ # define HASH_ALGORITHM_X(klass, r, c, s) \
75
+ else if (id == rb_intern(# s)) { \
76
+ hash = r ## _HASH; \
77
+ }
78
+ # include "defs/hashes.def"
79
+
80
+ # define HMAC_ALGORITHM_X_FORCE 1
81
+ # define HMAC_ALGORITHM_X(klass, r, c, s) \
82
+ else if (id == rb_intern(# s)) { \
83
+ hash = r ## _HMAC; \
84
+ }
85
+ # include "defs/hmacs.def"
86
+ return hash;
87
+ }
88
+
89
+ static bool digest_is_hmac(HashEnum hash)
90
+ {
91
+ switch (hash) {
92
+ case MD2_HMAC:
93
+ case MD4_HMAC:
94
+ case MD5_HMAC:
95
+ case RIPEMD160_HMAC:
96
+ case SHA1_HMAC:
97
+ case SHA256_HMAC:
98
+ case SHA384_HMAC:
99
+ case SHA512_HMAC:
100
+ case TIGER_HMAC:
101
+ case RIPEMD128_HMAC:
102
+ case RIPEMD256_HMAC:
103
+ case RIPEMD320_HMAC:
104
+ case WHIRLPOOL_HMAC:
105
+ case SHA3_224_HMAC:
106
+ case SHA3_256_HMAC:
107
+ case SHA3_384_HMAC:
108
+ case SHA3_512_HMAC:
109
+ return true;
110
+ default:
111
+ return false;
112
+ }
113
+ }
114
+
115
+ static bool digest_is_non_hmac(HashEnum hash)
116
+ {
117
+ return !digest_is_hmac(hash);
84
118
  }
85
119
 
86
120
 
87
121
  /* See if a hash algorithm is enabled. */
88
122
  static bool digest_enabled(HashEnum hash)
89
123
  {
90
- switch (hash) {
91
- # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
92
- case r ##_CHECKSUM:
93
- # include "defs/checksums.def"
94
-
95
- # define HASH_ALGORITHM_X(klass, r, c, s) \
96
- case r ##_HASH:
97
- # include "defs/hashes.def"
98
-
99
- # define HMAC_ALGORITHM_X(klass, r, c, s) \
100
- case r ##_HMAC:
101
- # include "defs/hmacs.def"
102
- return true;
103
- }
104
- return false;
124
+ switch (hash) {
125
+ # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
126
+ case r ##_CHECKSUM:
127
+ # include "defs/checksums.def"
128
+
129
+ # define HASH_ALGORITHM_X(klass, r, c, s) \
130
+ case r ##_HASH:
131
+ # include "defs/hashes.def"
132
+
133
+ # define HMAC_ALGORITHM_X(klass, r, c, s) \
134
+ case r ##_HMAC:
135
+ # include "defs/hmacs.def"
136
+ return true;
137
+ }
138
+ return false;
105
139
  }
106
140
 
107
141
 
108
142
  /* Figure out options for a digest. */
109
143
  static void digest_options(VALUE self, VALUE options)
110
144
  {
111
- Check_Type(options, T_HASH);
112
-
113
- {
114
- VALUE plaintext = rb_hash_aref(options, ID2SYM(rb_intern("plaintext")));
115
- VALUE plaintext_hex = rb_hash_aref(options, ID2SYM(rb_intern("plaintext_hex")));
116
- if (!NIL_P(plaintext) && !NIL_P(plaintext_hex)) {
117
- rb_raise(rb_eCryptoPP_Error, "can't set both plaintext and plaintext_hex in options");
118
- }
119
- else if (!NIL_P(plaintext)) {
120
- digest_plaintext_eq(self, plaintext, false);
121
- }
122
- else if (!NIL_P(plaintext_hex)) {
123
- digest_plaintext_eq(self, plaintext_hex, true);
124
- }
125
- }
126
-
127
- {
128
- VALUE digest = rb_hash_aref(options, ID2SYM(rb_intern("digest")));
129
- VALUE digest_hex = rb_hash_aref(options, ID2SYM(rb_intern("digest_hex")));
130
- if (!NIL_P(digest) && !NIL_P(digest_hex)) {
131
- rb_raise(rb_eCryptoPP_Error, "can't set both digest and digest_hex in options");
132
- }
133
- else if (!NIL_P(digest)) {
134
- digest_digest_eq(self, digest, false);
135
- }
136
- else if (!NIL_P(digest_hex)) {
137
- digest_digest_eq(self, digest_hex, true);
138
- }
139
- }
145
+ Check_Type(options, T_HASH);
146
+
147
+ {
148
+ VALUE plaintext = rb_hash_aref(options, ID2SYM(rb_intern("plaintext")));
149
+ VALUE plaintext_hex = rb_hash_aref(options, ID2SYM(rb_intern("plaintext_hex")));
150
+ if (!NIL_P(plaintext) && !NIL_P(plaintext_hex)) {
151
+ rb_raise(rb_eCryptoPP_Error, "can't set both plaintext and plaintext_hex in options");
152
+ }
153
+ else if (!NIL_P(plaintext)) {
154
+ digest_plaintext_eq(self, plaintext, false);
155
+ }
156
+ else if (!NIL_P(plaintext_hex)) {
157
+ digest_plaintext_eq(self, plaintext_hex, true);
158
+ }
159
+ }
160
+
161
+ {
162
+ VALUE digest = rb_hash_aref(options, ID2SYM(rb_intern("digest")));
163
+ VALUE digest_hex = rb_hash_aref(options, ID2SYM(rb_intern("digest_hex")));
164
+ if (!NIL_P(digest) && !NIL_P(digest_hex)) {
165
+ rb_raise(rb_eCryptoPP_Error, "can't set both digest and digest_hex in options");
166
+ }
167
+ else if (!NIL_P(digest)) {
168
+ digest_digest_eq(self, digest, false);
169
+ }
170
+ else if (!NIL_P(digest_hex)) {
171
+ digest_digest_eq(self, digest_hex, true);
172
+ }
173
+ }
140
174
  }
141
175
 
142
176
 
143
177
  /* Creates a new Digest object. */
144
178
  static JHash* digest_factory(VALUE algorithm)
145
179
  {
146
- try {
147
- switch (digest_sym_to_const(algorithm)) {
148
- default:
149
- throw JException("the requested algorithm cannot be found");
150
- break;
151
-
152
- # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
153
- case r ## _CHECKSUM: \
154
- return static_cast<c*>(new c);
155
- # include "defs/checksums.def"
156
-
157
- # define HASH_ALGORITHM_X(klass, r, c, s) \
158
- case r ## _HASH: \
159
- return static_cast<c*>(new c);
160
- # include "defs/hashes.def"
161
-
162
- # define HMAC_ALGORITHM_X(klass, r, c, s) \
163
- case r ## _HMAC: \
164
- return static_cast<c*>(new c);
165
- # include "defs/hmacs.def"
166
- }
167
- }
168
- catch (Exception& e) {
169
- throw JException("Crypto++ exception: " + e.GetWhat());
170
- }
180
+ try {
181
+ switch (digest_sym_to_const(algorithm)) {
182
+ default:
183
+ throw JException("the requested algorithm cannot be found");
184
+ break;
185
+
186
+ # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
187
+ case r ## _CHECKSUM: \
188
+ return static_cast<c*>(new c);
189
+ # include "defs/checksums.def"
190
+
191
+ # define HASH_ALGORITHM_X(klass, r, c, s) \
192
+ case r ## _HASH: \
193
+ return static_cast<c*>(new c);
194
+ # include "defs/hashes.def"
195
+
196
+ # define HMAC_ALGORITHM_X(klass, r, c, s) \
197
+ case r ## _HMAC: \
198
+ return static_cast<c*>(new c);
199
+ # include "defs/hmacs.def"
200
+ }
201
+ }
202
+ catch (Exception& e) {
203
+ throw JException("Crypto++ exception: " + e.GetWhat());
204
+ }
171
205
  }
172
206
 
173
207
  /* Wraps a Digest/HMAC object into a Ruby object. May throw a JException if no
174
208
  * suitable algorithm is found. */
175
209
  static VALUE wrap_digest_in_ruby(JHash* hash)
176
210
  {
177
- const type_info& info = typeid(*hash);
178
- # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
179
- if (info == typeid(c)) { \
180
- return Data_Wrap_Struct(rb_cCryptoPP_Digest_## r, hash_mark, hash_free, hash); \
181
- } \
182
- else
183
- # include "defs/checksums.def"
184
-
185
- # define HASH_ALGORITHM_X(klass, r, c, s) \
186
- if (info == typeid(c)) { \
187
- return Data_Wrap_Struct(rb_cCryptoPP_Digest_## r, hash_mark, hash_free, hash); \
188
- } \
189
- else
190
- # include "defs/hashes.def"
191
-
192
- # define HMAC_ALGORITHM_X(klass, r, c, s) \
193
- if (info == typeid(c)) { \
194
- return Data_Wrap_Struct(rb_cCryptoPP_Digest_HMAC_## r, hash_mark, hash_free, hash); \
195
- } \
196
- else
197
- # include "defs/hmacs.def"
198
- {
199
- throw JException("the requested algorithm has been disabled");
200
- }
211
+ const type_info& info = typeid(*hash);
212
+ # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
213
+ if (info == typeid(c)) { \
214
+ return Data_Wrap_Struct(rb_cCryptoPP_Digest_## r, hash_mark, hash_free, hash); \
215
+ } \
216
+ else
217
+ # include "defs/checksums.def"
218
+
219
+ # define HASH_ALGORITHM_X(klass, r, c, s) \
220
+ if (info == typeid(c)) { \
221
+ return Data_Wrap_Struct(rb_cCryptoPP_Digest_## r, hash_mark, hash_free, hash); \
222
+ } \
223
+ else
224
+ # include "defs/hashes.def"
225
+
226
+ # define HMAC_ALGORITHM_X(klass, r, c, s) \
227
+ if (info == typeid(c)) { \
228
+ return Data_Wrap_Struct(rb_cCryptoPP_Digest_HMAC_## r, hash_mark, hash_free, hash); \
229
+ } \
230
+ else
231
+ # include "defs/hmacs.def"
232
+ {
233
+ throw JException("the requested algorithm has been disabled");
234
+ }
201
235
  }
202
236
 
203
237
  /**
204
- * call-seq:
205
- * digest_factory(algorithm) => CryptoPP::Digest
206
- * digest_factory(algorithm, plaintext) => CryptoPP::Digest
207
- * digest_factory(algorithm, options) => CryptoPP::Digest
238
+ * call-seq:
239
+ * digest_factory(algorithm) => CryptoPP::Digest
240
+ * digest_factory(algorithm, plaintext) => CryptoPP::Digest
241
+ * digest_factory(algorithm, options) => CryptoPP::Digest
208
242
  *
209
243
  * Creates a new Digest object.
210
244
  *
@@ -212,300 +246,300 @@ static VALUE wrap_digest_in_ruby(JHash* hash)
212
246
  */
213
247
  VALUE rb_module_digest_factory(int argc, VALUE *argv, VALUE self)
214
248
  {
215
- JHash* hash = NULL;
216
- VALUE algorithm, options, retval;
217
-
218
- rb_scan_args(argc, argv, "11", &algorithm, &options);
219
- {
220
- HashEnum a = digest_sym_to_const(algorithm);
221
- if (!IS_NON_HMAC(a)) {
222
- rb_raise(rb_eCryptoPP_Error, "invalid digest algorithm");
223
- }
224
- else {
225
- try {
226
- hash = digest_factory(algorithm);
227
- retval = wrap_digest_in_ruby(hash);
228
- }
229
- catch (Exception& e) {
230
- if (hash != NULL) {
231
- delete hash;
232
- }
233
- rb_raise(rb_eCryptoPP_Error, e.GetWhat().c_str());
234
- }
235
- if (argc == 2) {
236
- if (TYPE(options) == T_STRING) {
237
- rb_digest_plaintext_eq(retval, options);
238
- hash->hash();
239
- }
240
- else {
241
- digest_options(retval, options);
242
- }
243
- }
244
- return retval;
245
- }
246
- }
249
+ JHash* hash = NULL;
250
+ VALUE algorithm, options, retval;
251
+
252
+ rb_scan_args(argc, argv, "11", &algorithm, &options);
253
+ {
254
+ HashEnum a = digest_sym_to_const(algorithm);
255
+ if (!digest_is_non_hmac(a)) {
256
+ rb_raise(rb_eCryptoPP_Error, "invalid digest algorithm");
257
+ }
258
+ else {
259
+ try {
260
+ hash = digest_factory(algorithm);
261
+ retval = wrap_digest_in_ruby(hash);
262
+ }
263
+ catch (Exception& e) {
264
+ if (hash != NULL) {
265
+ delete hash;
266
+ }
267
+ rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
268
+ }
269
+ if (argc == 2) {
270
+ if (TYPE(options) == T_STRING) {
271
+ rb_digest_plaintext_eq(retval, options);
272
+ hash->hash();
273
+ }
274
+ else {
275
+ digest_options(retval, options);
276
+ }
277
+ }
278
+ return retval;
279
+ }
280
+ }
247
281
  }
248
282
 
249
283
  #define CHECKSUM_ALGORITHM_X(klass, r, n, s) \
250
284
  VALUE rb_digest_ ## r ##_new(int argc, VALUE *argv, VALUE self) \
251
285
  { \
252
- VALUE options, retval; \
253
- JHash* hash = NULL; \
254
- try { \
255
- hash = digest_factory(ID2SYM(rb_intern(# s))); \
256
- retval = wrap_digest_in_ruby(hash); \
257
- } \
258
- catch (Exception& e) { \
259
- if (hash != NULL) { \
260
- delete hash; \
261
- } \
262
- rb_raise(rb_eCryptoPP_Error, e.GetWhat().c_str()); \
263
- } \
264
- rb_scan_args(argc, argv, "01", &options); \
265
- if (!NIL_P(options)) { \
266
- if (TYPE(options) == T_STRING) { \
267
- rb_digest_plaintext_eq(retval, options); \
268
- hash->hash(); \
269
- } \
270
- else { \
271
- digest_options(retval, options); \
272
- } \
273
- } \
274
- return retval; \
286
+ VALUE options, retval; \
287
+ JHash* hash = NULL; \
288
+ try { \
289
+ hash = digest_factory(ID2SYM(rb_intern(# s))); \
290
+ retval = wrap_digest_in_ruby(hash); \
291
+ } \
292
+ catch (Exception& e) { \
293
+ if (hash != NULL) { \
294
+ delete hash; \
295
+ } \
296
+ rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str()); \
297
+ } \
298
+ rb_scan_args(argc, argv, "01", &options); \
299
+ if (!NIL_P(options)) { \
300
+ if (TYPE(options) == T_STRING) { \
301
+ rb_digest_plaintext_eq(retval, options); \
302
+ hash->hash(); \
303
+ } \
304
+ else { \
305
+ digest_options(retval, options); \
306
+ } \
307
+ } \
308
+ return retval; \
275
309
  }
276
310
  #include "defs/checksums.def"
277
311
 
278
312
  #define HASH_ALGORITHM_X(klass, r, n, s) \
279
313
  VALUE rb_digest_ ## r ##_new(int argc, VALUE *argv, VALUE self) \
280
314
  { \
281
- VALUE options, retval; \
282
- JHash* hash = NULL; \
283
- try { \
284
- hash = digest_factory(ID2SYM(rb_intern(# s))); \
285
- retval = wrap_digest_in_ruby(hash); \
286
- } \
287
- catch (Exception& e) { \
288
- if (hash != NULL) { \
289
- delete hash; \
290
- } \
291
- rb_raise(rb_eCryptoPP_Error, e.GetWhat().c_str()); \
292
- } \
293
- rb_scan_args(argc, argv, "01", &options); \
294
- if (!NIL_P(options)) { \
295
- if (TYPE(options) == T_STRING) { \
296
- rb_digest_plaintext_eq(retval, options); \
297
- hash->hash(); \
298
- } \
299
- else { \
300
- digest_options(retval, options); \
301
- } \
302
- } \
303
- return retval; \
315
+ VALUE options, retval; \
316
+ JHash* hash = NULL; \
317
+ try { \
318
+ hash = digest_factory(ID2SYM(rb_intern(# s))); \
319
+ retval = wrap_digest_in_ruby(hash); \
320
+ } \
321
+ catch (Exception& e) { \
322
+ if (hash != NULL) { \
323
+ delete hash; \
324
+ } \
325
+ rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str()); \
326
+ } \
327
+ rb_scan_args(argc, argv, "01", &options); \
328
+ if (!NIL_P(options)) { \
329
+ if (TYPE(options) == T_STRING) { \
330
+ rb_digest_plaintext_eq(retval, options); \
331
+ hash->hash(); \
332
+ } \
333
+ else { \
334
+ digest_options(retval, options); \
335
+ } \
336
+ } \
337
+ return retval; \
304
338
  }
305
339
  #include "defs/hashes.def"
306
340
 
307
341
 
308
342
  /**
309
343
  * call-seq:
310
- * update(plaintext) => String
344
+ * update(plaintext) => String
311
345
  *
312
346
  * Updates the plaintext on a Digest and returns the new digested text.
313
347
  */
314
348
  VALUE rb_digest_update(VALUE self, VALUE plaintext)
315
349
  {
316
- JHash *hash = NULL;
317
- Check_Type(plaintext, T_STRING);
318
- Data_Get_Struct(self, JHash, hash);
319
- hash->updatePlaintext(string(StringValuePtr(plaintext), RSTRING(plaintext)->len));
320
- hash->hash();
321
- return rb_tainted_str_new(hash->getHashtext().data(), hash->getHashtext().length());
350
+ JHash *hash = NULL;
351
+ Check_Type(plaintext, T_STRING);
352
+ Data_Get_Struct(self, JHash, hash);
353
+ hash->updatePlaintext(string(StringValuePtr(plaintext), RSTRING_LEN(plaintext)));
354
+ hash->hash();
355
+ return rb_tainted_str_new(hash->getHashtext().data(), hash->getHashtext().length());
322
356
  }
323
357
 
324
358
 
325
359
  /* Returns the digested text. */
326
360
  static string digest_digest(VALUE self, bool hex)
327
361
  {
328
- JHash *hash = NULL;
329
- Data_Get_Struct(self, JHash, hash);
330
- return hash->getHashtext(hex);
362
+ JHash *hash = NULL;
363
+ Data_Get_Struct(self, JHash, hash);
364
+ return hash->getHashtext(hex);
331
365
  }
332
366
 
333
367
  /**
334
368
  * call-seq:
335
- * digest => String
369
+ * digest => String
336
370
  *
337
371
  * Returns the digested text in binary.
338
372
  */
339
373
  VALUE rb_digest_digest(VALUE self)
340
374
  {
341
- string retval = digest_digest(self, false);
342
- return rb_tainted_str_new(retval.data(), retval.length());
375
+ string retval = digest_digest(self, false);
376
+ return rb_tainted_str_new(retval.data(), retval.length());
343
377
  }
344
378
 
345
379
  /**
346
380
  * call-seq:
347
- * digest_hex => String
381
+ * digest_hex => String
348
382
  *
349
383
  * Returns the digested text in hex.
350
384
  */
351
385
  VALUE rb_digest_digest_hex(VALUE self)
352
386
  {
353
- string retval = digest_digest(self, true);
354
- return rb_tainted_str_new(retval.data(), retval.length());
387
+ string retval = digest_digest(self, true);
388
+ return rb_tainted_str_new(retval.data(), retval.length());
355
389
  }
356
390
 
357
391
 
358
392
  /* Gets the plaintext from a hash. */
359
393
  static string digest_plaintext(VALUE self, bool hex)
360
394
  {
361
- JHash *hash = NULL;
362
- Data_Get_Struct(self, JHash, hash);
363
- return hash->getPlaintext(hex);;
395
+ JHash *hash = NULL;
396
+ Data_Get_Struct(self, JHash, hash);
397
+ return hash->getPlaintext(hex);;
364
398
  }
365
399
 
366
400
  /**
367
401
  * call-seq:
368
- * plaintext => String
402
+ * plaintext => String
369
403
  *
370
404
  * Returns the plaintext used to generate the digest in binary.
371
405
  */
372
406
  VALUE rb_digest_plaintext(VALUE self)
373
407
  {
374
- string retval = digest_plaintext(self, false);
375
- return rb_tainted_str_new(retval.data(), retval.length());
408
+ string retval = digest_plaintext(self, false);
409
+ return rb_tainted_str_new(retval.data(), retval.length());
376
410
  }
377
411
 
378
412
  /**
379
413
  * call-seq:
380
- * plaintext_hex => String
414
+ * plaintext_hex => String
381
415
  *
382
416
  * Returns the plaintext used to generate the digest in hex.
383
417
  */
384
418
  VALUE rb_digest_plaintext_hex(VALUE self)
385
419
  {
386
- string retval = digest_plaintext(self, true);
387
- return rb_tainted_str_new(retval.data(), retval.length());
420
+ string retval = digest_plaintext(self, true);
421
+ return rb_tainted_str_new(retval.data(), retval.length());
388
422
  }
389
423
 
390
424
 
391
425
  /* Sets the plaintext on a digest. */
392
426
  static string digest_plaintext_eq(VALUE self, VALUE plaintext, bool hex)
393
427
  {
394
- JHash *hash = NULL;
395
- Check_Type(plaintext, T_STRING);
396
- Data_Get_Struct(self, JHash, hash);
397
- hash->setPlaintext(string(StringValuePtr(plaintext), RSTRING(plaintext)->len), hex);
398
- return hash->getPlaintext(hex);
428
+ JHash *hash = NULL;
429
+ Check_Type(plaintext, T_STRING);
430
+ Data_Get_Struct(self, JHash, hash);
431
+ hash->setPlaintext(string(StringValuePtr(plaintext), RSTRING_LEN(plaintext)), hex);
432
+ return hash->getPlaintext(hex);
399
433
  }
400
434
 
401
435
  /**
402
436
  * call-seq:
403
- * plaintext=(plaintext)
437
+ * plaintext=(plaintext)
404
438
  *
405
439
  * Sets the plaintext on a Digest in binary.
406
440
  */
407
441
  VALUE rb_digest_plaintext_eq(VALUE self, VALUE plaintext)
408
442
  {
409
- digest_plaintext_eq(self, plaintext, false);
410
- return plaintext;
443
+ digest_plaintext_eq(self, plaintext, false);
444
+ return plaintext;
411
445
  }
412
446
 
413
447
  /**
414
448
  * call-seq:
415
- * plaintext=(plaintext)
449
+ * plaintext=(plaintext)
416
450
  *
417
451
  * Sets the plaintext on a Digest in hex.
418
452
  */
419
453
  VALUE rb_digest_plaintext_hex_eq(VALUE self, VALUE plaintext)
420
454
  {
421
- digest_plaintext_eq(self, plaintext, true);
422
- return plaintext;
455
+ digest_plaintext_eq(self, plaintext, true);
456
+ return plaintext;
423
457
  }
424
458
 
425
459
 
426
460
  /* Calculates the digest. */
427
461
  static string digest_calculate(VALUE self, bool hex)
428
462
  {
429
- JHash *hash = NULL;
430
- Data_Get_Struct(self, JHash, hash);
431
- hash->hash();
432
- return hash->getHashtext(hex);
463
+ JHash *hash = NULL;
464
+ Data_Get_Struct(self, JHash, hash);
465
+ hash->hash();
466
+ return hash->getHashtext(hex);
433
467
  }
434
468
 
435
469
  /**
436
470
  * call-seq:
437
- * calculate => String
471
+ * calculate => String
438
472
  *
439
473
  * Calculates the digest and returns the result in binary.
440
474
  */
441
475
  VALUE rb_digest_calculate(VALUE self)
442
476
  {
443
- string retval = digest_calculate(self, false);
444
- return rb_tainted_str_new(retval.data(), retval.length());
477
+ string retval = digest_calculate(self, false);
478
+ return rb_tainted_str_new(retval.data(), retval.length());
445
479
  }
446
480
 
447
481
  /**
448
482
  * call-seq:
449
- * calculate_hex => String
483
+ * calculate_hex => String
450
484
  *
451
485
  * Calculates the digest and returns the result in hex.
452
486
  */
453
487
  VALUE rb_digest_calculate_hex(VALUE self)
454
488
  {
455
- string retval = digest_calculate(self, true);
456
- return rb_tainted_str_new(retval.data(), retval.length());
489
+ string retval = digest_calculate(self, true);
490
+ return rb_tainted_str_new(retval.data(), retval.length());
457
491
  }
458
492
 
459
493
 
460
494
  /* Sets the hashtext on a digest. */
461
495
  static string digest_digest_eq(VALUE self, VALUE digest, bool hex)
462
496
  {
463
- JHash *hash = NULL;
464
- Check_Type(digest, T_STRING);
465
- Data_Get_Struct(self, JHash, hash);
466
- hash->setHashtext(string(StringValuePtr(digest), RSTRING(digest)->len), hex);
467
- return hash->getHashtext(hex);
497
+ JHash *hash = NULL;
498
+ Check_Type(digest, T_STRING);
499
+ Data_Get_Struct(self, JHash, hash);
500
+ hash->setHashtext(string(StringValuePtr(digest), RSTRING_LEN(digest)), hex);
501
+ return hash->getHashtext(hex);
468
502
  }
469
503
 
470
504
  /**
471
505
  * call-seq:
472
- * digest=(bin)
506
+ * digest=(bin)
473
507
  *
474
508
  * Sets the digest text on a Digest in binary.
475
509
  */
476
510
  VALUE rb_digest_digest_eq(VALUE self, VALUE digest)
477
511
  {
478
- digest_digest_eq(self, digest, false);
479
- return digest;
512
+ digest_digest_eq(self, digest, false);
513
+ return digest;
480
514
  }
481
515
 
482
516
  /**
483
517
  * call-seq:
484
- * digest_hex=(hex)
518
+ * digest_hex=(hex)
485
519
  *
486
520
  * Sets the digest text on a Digest in hex.
487
521
  */
488
522
  VALUE rb_digest_digest_hex_eq(VALUE self, VALUE digest)
489
523
  {
490
- digest_digest_eq(self, digest, true);
491
- return digest;
524
+ digest_digest_eq(self, digest, true);
525
+ return digest;
492
526
  }
493
527
 
494
528
 
495
529
  /**
496
530
  * call-seq:
497
- * inspect => String
531
+ * inspect => String
498
532
  *
499
533
  * Inspect method.
500
534
  */
501
535
  VALUE rb_digest_inspect(VALUE self)
502
536
  {
503
- JHash* hash = NULL;
504
- string retval;
505
- string cname = rb_obj_classname(self);
506
- Data_Get_Struct(self, JHash, hash);
507
- retval = "#<" + cname + ": " + hash->getHashtext(true) + ">";
508
- return rb_str_new(retval.c_str(), retval.length());
537
+ JHash* hash = NULL;
538
+ string retval;
539
+ string cname = rb_obj_classname(self);
540
+ Data_Get_Struct(self, JHash, hash);
541
+ retval = "#<" + cname + ": " + hash->getHashtext(true) + ">";
542
+ return rb_str_new(retval.c_str(), retval.length());
509
543
  }
510
544
 
511
545
 
@@ -517,122 +551,122 @@ VALUE rb_digest_inspect(VALUE self)
517
551
  */
518
552
  VALUE rb_digest_equals(VALUE self, VALUE compare)
519
553
  {
520
- JHash *hash = NULL;
521
- VALUE str1, str2;
522
- Check_Type(compare, T_STRING);
523
- Data_Get_Struct(self, JHash, hash);
524
- if (RSTRING(compare)->len == ((long) hash->getDigestSize() / 2)) {
525
- str1 = rb_str_new(hash->getHashtext(false).data(), hash->getHashtext(false).length());
526
- str2 = compare;
527
- }
528
- else if (RSTRING(compare)->len == ((long) hash->getDigestSize())) {
529
- str1 = rb_str_new(hash->getHashtext(true).data(), hash->getHashtext(true).length());
530
- str2 = rb_funcall(compare, rb_intern("downcase"), 0);
531
- }
532
- else {
533
- rb_raise(rb_eCryptoPP_Error, "expected %d bytes, got %d", hash->getDigestSize() / 2, RSTRING(compare)->len);
534
- }
535
-
536
- if (rb_str_cmp(str1, str2) == 0) {
537
- return Qtrue;
538
- }
539
- else {
540
- return Qfalse;
541
- }
554
+ JHash *hash = NULL;
555
+ VALUE str1, str2;
556
+ Check_Type(compare, T_STRING);
557
+ Data_Get_Struct(self, JHash, hash);
558
+ if (RSTRING_LEN(compare) == ((long) hash->getDigestSize() / 2)) {
559
+ str1 = rb_str_new(hash->getHashtext(false).data(), hash->getHashtext(false).length());
560
+ str2 = compare;
561
+ }
562
+ else if (RSTRING_LEN(compare) == ((long) hash->getDigestSize())) {
563
+ str1 = rb_str_new(hash->getHashtext(true).data(), hash->getHashtext(true).length());
564
+ str2 = rb_funcall(compare, rb_intern("downcase"), 0);
565
+ }
566
+ else {
567
+ rb_raise(rb_eCryptoPP_Error, "expected %d bytes, got %ld", hash->getDigestSize() / 2, RSTRING_LEN(compare));
568
+ }
569
+
570
+ if (rb_str_cmp(str1, str2) == 0) {
571
+ return Qtrue;
572
+ }
573
+ else {
574
+ return Qfalse;
575
+ }
542
576
  }
543
577
 
544
578
 
545
579
  /* Singleton method for digesting good stuff. */
546
580
  static string module_digest(int argc, VALUE *argv, VALUE self, bool hex)
547
581
  {
548
- JHash* hash = NULL;
549
- VALUE algorithm, plaintext, key;
550
- if (argc < 2) {
551
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc);
552
- }
553
-
554
- if (IS_HMAC(digest_sym_to_const(argv[0]))) {
555
- rb_scan_args(argc, argv, "21", &algorithm, &plaintext, &key);
556
- Check_Type(plaintext, T_STRING);
557
- Check_Type(key, T_STRING);
558
- }
559
- else {
560
- rb_scan_args(argc, argv, "2", &algorithm, &plaintext);
561
- Check_Type(plaintext, T_STRING);
562
- }
563
-
564
- try {
565
- string retval;
566
- hash = digest_factory(algorithm);
567
- hash->setPlaintext(string(StringValuePtr(plaintext), RSTRING(plaintext)->len));
568
- if (IS_HMAC(digest_sym_to_const(algorithm))) {
569
- ((JHMAC*) hash)->setKey(string(StringValuePtr(key), RSTRING(key)->len));
570
- }
571
- hash->hash();
572
- retval = hash->getHashtext(hex);
573
-
574
- delete hash;
575
- return retval;
576
- }
577
- catch (Exception& e) {
578
- if (hash != NULL) {
579
- delete hash;
580
- }
581
- rb_raise(rb_eCryptoPP_Error, e.GetWhat().c_str());
582
- }
582
+ JHash* hash = NULL;
583
+ VALUE algorithm, plaintext, key;
584
+ if (argc < 2) {
585
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc);
586
+ }
587
+
588
+ if (digest_is_hmac(digest_sym_to_const(argv[0]))) {
589
+ rb_scan_args(argc, argv, "21", &algorithm, &plaintext, &key);
590
+ Check_Type(plaintext, T_STRING);
591
+ Check_Type(key, T_STRING);
592
+ }
593
+ else {
594
+ rb_scan_args(argc, argv, "2", &algorithm, &plaintext);
595
+ Check_Type(plaintext, T_STRING);
596
+ }
597
+
598
+ try {
599
+ string retval;
600
+ hash = digest_factory(algorithm);
601
+ hash->setPlaintext(string(StringValuePtr(plaintext), RSTRING_LEN(plaintext)));
602
+ if (digest_is_hmac(digest_sym_to_const(algorithm))) {
603
+ ((JHMAC*) hash)->setKey(string(StringValuePtr(key), RSTRING_LEN(key)));
604
+ }
605
+ hash->hash();
606
+ retval = hash->getHashtext(hex);
607
+
608
+ delete hash;
609
+ return retval;
610
+ }
611
+ catch (Exception& e) {
612
+ if (hash != NULL) {
613
+ delete hash;
614
+ }
615
+ rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
616
+ }
583
617
  }
584
618
 
585
619
  /**
586
620
  * call-seq:
587
- * digest(algorithm, plaintext) => String
621
+ * digest(algorithm, plaintext) => String
588
622
  *
589
623
  * Digest the plaintext and returns the result in binary.
590
624
  */
591
625
  VALUE rb_module_digest(int argc, VALUE *argv, VALUE self)
592
626
  {
593
- string retval = module_digest(argc, argv, self, false);
594
- return rb_tainted_str_new(retval.data(), retval.length());
627
+ string retval = module_digest(argc, argv, self, false);
628
+ return rb_tainted_str_new(retval.data(), retval.length());
595
629
  }
596
630
 
597
631
  /**
598
632
  * call-seq:
599
- * digest_hex(algorithm, plaintext) => String
633
+ * digest_hex(algorithm, plaintext) => String
600
634
  *
601
635
  * Digest the plaintext and returns the result in hex.
602
636
  */
603
637
  VALUE rb_module_digest_hex(int argc, VALUE *argv, VALUE self)
604
638
  {
605
- string retval = module_digest(argc, argv, self, true);
606
- return rb_tainted_str_new(retval.data(), retval.length());
639
+ string retval = module_digest(argc, argv, self, true);
640
+ return rb_tainted_str_new(retval.data(), retval.length());
607
641
  }
608
642
 
609
643
 
610
644
  /* Digests an appropriate Ruby IO object. */
611
645
  static string module_digest_io(int argc, VALUE *argv, VALUE self, bool hex)
612
646
  {
613
- JHash* hash = NULL;
614
- VALUE algorithm, io;
615
-
616
- rb_scan_args(argc, argv, "2", &algorithm, &io);
617
- try {
618
- string retval;
619
- hash = digest_factory(algorithm);
620
- retval = hash->hashRubyIO(&io, hex);
621
-
622
- delete hash;
623
- return retval;
624
- }
625
- catch (Exception& e) {
626
- if (hash != NULL) {
627
- delete hash;
628
- }
629
- rb_raise(rb_eCryptoPP_Error, e.GetWhat().c_str());
630
- }
647
+ JHash* hash = NULL;
648
+ VALUE algorithm, io;
649
+
650
+ rb_scan_args(argc, argv, "2", &algorithm, &io);
651
+ try {
652
+ string retval;
653
+ hash = digest_factory(algorithm);
654
+ retval = hash->hashRubyIO(&io, hex);
655
+
656
+ delete hash;
657
+ return retval;
658
+ }
659
+ catch (Exception& e) {
660
+ if (hash != NULL) {
661
+ delete hash;
662
+ }
663
+ rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
664
+ }
631
665
  }
632
666
 
633
667
  /**
634
668
  * call-seq:
635
- * digest_io(io) => String
669
+ * digest_io(io) => String
636
670
  *
637
671
  * Digests a Ruby IO object and spits out the result in binary. You can use
638
672
  * any sort of Ruby object as long as it implements <tt>eof?</tt>,
@@ -640,17 +674,17 @@ static string module_digest_io(int argc, VALUE *argv, VALUE self, bool hex)
640
674
  *
641
675
  * Example:
642
676
  *
643
- * cipher.digest_io(File.open("http://example.com/"))
677
+ * cipher.digest_io(File.open("http://example.com/"))
644
678
  */
645
679
  VALUE rb_module_digest_io(int argc, VALUE *argv, VALUE self)
646
680
  {
647
- string retval = module_digest_io(argc, argv, self, false);
648
- return rb_tainted_str_new(retval.data(), retval.length());
681
+ string retval = module_digest_io(argc, argv, self, false);
682
+ return rb_tainted_str_new(retval.data(), retval.length());
649
683
  }
650
684
 
651
685
  /**
652
686
  * call-seq:
653
- * digest_io_hex(io) => String
687
+ * digest_io_hex(io) => String
654
688
  *
655
689
  * Digests a Ruby IO object and spits out the result in hex. You can use
656
690
  * any sort of Ruby object as long as it implements <tt>eof?</tt>,
@@ -658,69 +692,69 @@ VALUE rb_module_digest_io(int argc, VALUE *argv, VALUE self)
658
692
  *
659
693
  * Example:
660
694
  *
661
- * cipher.digest_io_hex(File.open("http://example.com/"))
695
+ * cipher.digest_io_hex(File.open("http://example.com/"))
662
696
  */
663
697
  VALUE rb_module_digest_io_hex(int argc, VALUE *argv, VALUE self)
664
698
  {
665
- string retval = module_digest_io(argc, argv, self, true);
666
- return rb_tainted_str_new(retval.data(), retval.length());
699
+ string retval = module_digest_io(argc, argv, self, true);
700
+ return rb_tainted_str_new(retval.data(), retval.length());
667
701
  }
668
702
 
669
703
 
670
704
  /**
671
705
  * call-seq:
672
- * digest_enabled? => Boolean
706
+ * digest_enabled? => Boolean
673
707
  *
674
708
  * Is a Digest/HMAC algorithm available?
675
709
  */
676
710
  VALUE rb_module_digest_enabled(VALUE self, VALUE d)
677
711
  {
678
- if (digest_enabled(digest_sym_to_const(d))) {
679
- return Qtrue;
680
- }
681
- else {
682
- return Qfalse;
683
- }
712
+ if (digest_enabled(digest_sym_to_const(d))) {
713
+ return Qtrue;
714
+ }
715
+ else {
716
+ return Qfalse;
717
+ }
684
718
  }
685
719
 
686
720
 
687
721
  /* Returns the name of a hash algorithm. */
688
722
  VALUE rb_module_digest_name(VALUE self, VALUE h)
689
723
  {
690
- switch ((enum HashEnum) NUM2INT(h)) {
691
- default:
692
- rb_raise(rb_eCryptoPP_Error, "could not find a valid digest type");
693
- break;
694
-
695
- # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
696
- case r ## _CHECKSUM: \
697
- return rb_tainted_str_new2(c::getHashName().c_str());
698
- # include "defs/checksums.def"
699
-
700
- # define HASH_ALGORITHM_X(klass, r, c, s) \
701
- case r ## _HASH: \
702
- return rb_tainted_str_new2(c::getHashName().c_str());
703
- # include "defs/hashes.def"
704
-
705
- # define HMAC_ALGORITHM_X(klass, r, c, s) \
706
- case r ## _HMAC: \
707
- return rb_tainted_str_new2(c::getHashName().c_str());
708
- # include "defs/hmacs.def"
709
- }
724
+ switch ((enum HashEnum) NUM2INT(h)) {
725
+ default:
726
+ rb_raise(rb_eCryptoPP_Error, "could not find a valid digest type");
727
+ break;
728
+
729
+ # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
730
+ case r ## _CHECKSUM: \
731
+ return rb_tainted_str_new2(c::getHashName().c_str());
732
+ # include "defs/checksums.def"
733
+
734
+ # define HASH_ALGORITHM_X(klass, r, c, s) \
735
+ case r ## _HASH: \
736
+ return rb_tainted_str_new2(c::getHashName().c_str());
737
+ # include "defs/hashes.def"
738
+
739
+ # define HMAC_ALGORITHM_X(klass, r, c, s) \
740
+ case r ## _HMAC: \
741
+ return rb_tainted_str_new2(c::getHashName().c_str());
742
+ # include "defs/hmacs.def"
743
+ }
710
744
  }
711
745
 
712
746
 
713
747
  /**
714
748
  * call-seq:
715
- * algorithm_name => String
749
+ * algorithm_name => String
716
750
  *
717
751
  * Returns the name of the algorithm being used.
718
752
  */
719
753
  VALUE rb_digest_algorithm_name(VALUE self)
720
754
  {
721
- JHash *hash = NULL;
722
- Data_Get_Struct(self, JHash, hash);
723
- return rb_module_digest_name(self, INT2NUM(hash->getHashType()));
755
+ JHash *hash = NULL;
756
+ Data_Get_Struct(self, JHash, hash);
757
+ return rb_module_digest_name(self, INT2NUM(hash->getHashType()));
724
758
  }
725
759
 
726
760
 
@@ -729,211 +763,211 @@ VALUE rb_digest_algorithm_name(VALUE self)
729
763
  */
730
764
  VALUE rb_digest_clear(VALUE self)
731
765
  {
732
- JHash *hash = NULL;
733
- Data_Get_Struct(self, JHash, hash);
734
- hash->clear();
735
- return Qnil;
766
+ JHash *hash = NULL;
767
+ Data_Get_Struct(self, JHash, hash);
768
+ hash->clear();
769
+ return Qnil;
736
770
  }
737
771
 
738
772
 
739
773
  /**
740
774
  * call-seq:
741
- * validate => Boolean
775
+ * validate => Boolean
742
776
  *
743
777
  * Validates if the digest text is a valid digest for plaintext.
744
778
  */
745
779
  VALUE rb_digest_validate(VALUE self)
746
780
  {
747
- JHash *hash = NULL;
748
- Data_Get_Struct(self, JHash, hash);
749
- if (hash->validate()) {
750
- return Qtrue;
751
- }
752
- else {
753
- return Qfalse;
754
- }
781
+ JHash *hash = NULL;
782
+ Data_Get_Struct(self, JHash, hash);
783
+ if (hash->validate()) {
784
+ return Qtrue;
785
+ }
786
+ else {
787
+ return Qfalse;
788
+ }
755
789
  }
756
790
 
757
791
 
758
792
  /* Instance version of <tt>CryptoPP#digest_io</tt>. */
759
793
  static string digest_digest_io(VALUE self, VALUE io, bool hex)
760
794
  {
761
- try {
762
- JHash *hash;
763
- Data_Get_Struct(self, JHash, hash);
764
- return hash->hashRubyIO(&io, hex);
765
- }
766
- catch (Exception& e) {
767
- rb_raise(rb_eCryptoPP_Error, e.GetWhat().c_str());
768
- }
795
+ try {
796
+ JHash *hash;
797
+ Data_Get_Struct(self, JHash, hash);
798
+ return hash->hashRubyIO(&io, hex);
799
+ }
800
+ catch (Exception& e) {
801
+ rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
802
+ }
769
803
  }
770
804
 
771
805
  /**
772
806
  * call-seq:
773
- * digest_io(in) => String
807
+ * digest_io(in) => String
774
808
  *
775
809
  * Instance version of <tt>CryptoPP#digest_io</tt>.
776
810
  */
777
811
  VALUE rb_digest_digest_io(VALUE self, VALUE io)
778
812
  {
779
- string retval = digest_digest_io(self, io, false);
780
- return rb_tainted_str_new(retval.data(), retval.length());
813
+ string retval = digest_digest_io(self, io, false);
814
+ return rb_tainted_str_new(retval.data(), retval.length());
781
815
  }
782
816
 
783
817
  /**
784
818
  * call-seq:
785
- * digest_io_hex(in) => String
819
+ * digest_io_hex(in) => String
786
820
  *
787
821
  * Instance version of <tt>CryptoPP#digest_io_hex</tt>.
788
822
  */
789
823
  VALUE rb_digest_digest_io_hex(VALUE self, VALUE io)
790
824
  {
791
- string retval = digest_digest_io(self, io, true);
792
- return rb_tainted_str_new(retval.data(), retval.length());
825
+ string retval = digest_digest_io(self, io, true);
826
+ return rb_tainted_str_new(retval.data(), retval.length());
793
827
  }
794
828
 
795
829
 
796
830
  /**
797
831
  * call-seq:
798
- * digest_list => Array
832
+ * digest_list => Array
799
833
  *
800
834
  * Returns an Array of available Digest algorithms.
801
835
  */
802
836
  VALUE rb_module_digest_list(VALUE self)
803
837
  {
804
- VALUE ary;
805
- ary = rb_ary_new();
838
+ VALUE ary;
839
+ ary = rb_ary_new();
806
840
 
807
- # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
808
- rb_ary_push(ary, INT2NUM(r ##_CHECKSUM));
809
- # include "defs/checksums.def"
841
+ # define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
842
+ rb_ary_push(ary, INT2NUM(r ##_CHECKSUM));
843
+ # include "defs/checksums.def"
810
844
 
811
- # define HASH_ALGORITHM_X(klass, r, c, s) \
812
- rb_ary_push(ary, INT2NUM(r ##_HASH));
813
- # include "defs/hashes.def"
845
+ # define HASH_ALGORITHM_X(klass, r, c, s) \
846
+ rb_ary_push(ary, INT2NUM(r ##_HASH));
847
+ # include "defs/hashes.def"
814
848
 
815
- return ary;
849
+ return ary;
816
850
  }
817
851
 
818
852
 
819
853
  /* Figure out options for a HMAC. */
820
854
  static void digest_hmac_options(VALUE self, VALUE options)
821
855
  {
822
- digest_options(self, options);
823
-
824
- {
825
- VALUE key = rb_hash_aref(options, ID2SYM(rb_intern("key")));
826
- VALUE key_hex = rb_hash_aref(options, ID2SYM(rb_intern("key_hex")));
827
- if (!NIL_P(key) && !NIL_P(key_hex)) {
828
- rb_raise(rb_eCryptoPP_Error, "can't set both key and key_hex in options");
829
- }
830
- else if (!NIL_P(key)) {
831
- digest_hmac_key_eq(self, key, false);
832
- }
833
- else if (!NIL_P(key_hex)) {
834
- digest_hmac_key_eq(self, key_hex, true);
835
- }
836
- }
837
-
838
- {
839
- VALUE key_length = rb_hash_aref(options, ID2SYM(rb_intern("key_length")));
840
- if (!NIL_P(key_length)) {
841
- rb_digest_hmac_key_length_eq(self, key_length);
842
- }
843
- }
856
+ digest_options(self, options);
857
+
858
+ {
859
+ VALUE key = rb_hash_aref(options, ID2SYM(rb_intern("key")));
860
+ VALUE key_hex = rb_hash_aref(options, ID2SYM(rb_intern("key_hex")));
861
+ if (!NIL_P(key) && !NIL_P(key_hex)) {
862
+ rb_raise(rb_eCryptoPP_Error, "can't set both key and key_hex in options");
863
+ }
864
+ else if (!NIL_P(key)) {
865
+ digest_hmac_key_eq(self, key, false);
866
+ }
867
+ else if (!NIL_P(key_hex)) {
868
+ digest_hmac_key_eq(self, key_hex, true);
869
+ }
870
+ }
871
+
872
+ {
873
+ VALUE key_length = rb_hash_aref(options, ID2SYM(rb_intern("key_length")));
874
+ if (!NIL_P(key_length)) {
875
+ rb_digest_hmac_key_length_eq(self, key_length);
876
+ }
877
+ }
844
878
  }
845
879
 
846
880
 
847
881
  /**
848
882
  * call-seq:
849
- * hmac_factory(algorithm) => CryptoPP::HMAC
850
- * hmac_factory(algorithm, plaintext, key) => CryptoPP::HMAC
851
- * hmac_factory(algorithm, options) => CryptoPP::HMAC
883
+ * hmac_factory(algorithm) => CryptoPP::HMAC
884
+ * hmac_factory(algorithm, plaintext, key) => CryptoPP::HMAC
885
+ * hmac_factory(algorithm, options) => CryptoPP::HMAC
852
886
  *
853
887
  * Creates a new HMAC object.
854
888
  */
855
889
  VALUE rb_module_hmac_factory(int argc, VALUE *argv, VALUE self)
856
890
  {
857
- if (argc < 1 || argc > 3) {
858
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1-3)", argc);
859
- }
860
- else {
861
- JHash* hash = NULL;
862
- VALUE algorithm = argv[0];
863
- VALUE retval;
864
-
865
- if (!IS_HMAC(digest_sym_to_const(algorithm))) {
866
- rb_raise(rb_eCryptoPP_Error, "invalid HMAC algorithm");
867
- }
868
- else {
869
- try {
870
- hash = digest_factory(algorithm);
871
- retval = wrap_digest_in_ruby(hash);
872
- }
873
- catch (Exception& e) {
874
- if (hash != NULL) {
875
- delete hash;
876
- }
877
- rb_raise(rb_eCryptoPP_Error, e.GetWhat().c_str());
878
- }
879
- if (argc >= 2) {
880
- if (TYPE(argv[1]) == T_STRING) {
881
- digest_plaintext_eq(retval, argv[1], false);
882
- if (argc == 3) {
883
- Check_Type(argv[2], T_STRING);
884
- digest_hmac_key_eq(retval, argv[2], false);
885
- }
886
- hash->hash();
887
- }
888
- else if (argc > 2) {
889
- rb_raise(rb_eArgError, "wrong argument types (expected a String or a Hash");
890
- }
891
- else {
892
- digest_hmac_options(retval, argv[1]);
893
- }
894
- }
895
- return retval;
896
- }
897
- }
891
+ if (argc < 1 || argc > 3) {
892
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1-3)", argc);
893
+ }
894
+ else {
895
+ JHash* hash = NULL;
896
+ VALUE algorithm = argv[0];
897
+ VALUE retval;
898
+
899
+ if (!digest_is_hmac(digest_sym_to_const(algorithm))) {
900
+ rb_raise(rb_eCryptoPP_Error, "invalid HMAC algorithm");
901
+ }
902
+ else {
903
+ try {
904
+ hash = digest_factory(algorithm);
905
+ retval = wrap_digest_in_ruby(hash);
906
+ }
907
+ catch (Exception& e) {
908
+ if (hash != NULL) {
909
+ delete hash;
910
+ }
911
+ rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
912
+ }
913
+ if (argc >= 2) {
914
+ if (TYPE(argv[1]) == T_STRING) {
915
+ digest_plaintext_eq(retval, argv[1], false);
916
+ if (argc == 3) {
917
+ Check_Type(argv[2], T_STRING);
918
+ digest_hmac_key_eq(retval, argv[2], false);
919
+ }
920
+ hash->hash();
921
+ }
922
+ else if (argc > 2) {
923
+ rb_raise(rb_eArgError, "wrong argument types (expected a String or a Hash");
924
+ }
925
+ else {
926
+ digest_hmac_options(retval, argv[1]);
927
+ }
928
+ }
929
+ return retval;
930
+ }
931
+ }
898
932
  }
899
933
 
900
934
  #define HMAC_ALGORITHM_X(klass, r, n, s) \
901
935
  VALUE rb_digest_hmac_ ## r ##_new(int argc, VALUE *argv, VALUE self) \
902
936
  { \
903
- if (argc > 2) { \
904
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc); \
905
- } \
906
- else { \
907
- VALUE retval; \
908
- JHash* hash = NULL; \
909
- try { \
910
- hash = digest_factory(ID2SYM(rb_intern(# s))); \
911
- retval = wrap_digest_in_ruby(hash); \
912
- } \
913
- catch (Exception& e) { \
914
- if (hash != NULL) { \
915
- delete hash; \
916
- } \
917
- rb_raise(rb_eCryptoPP_Error, e.GetWhat().c_str()); \
918
- } \
919
- if (argc >= 1) { \
920
- if (TYPE(argv[0]) == T_STRING) { \
921
- digest_plaintext_eq(retval, argv[0], false); \
922
- if (argc == 2) { \
923
- Check_Type(argv[1], T_STRING); \
924
- digest_hmac_key_eq(retval, argv[1], false); \
925
- } \
926
- hash->hash(); \
927
- } \
928
- else if (argc > 1) { \
929
- rb_raise(rb_eArgError, "wrong argument types (expected a String or a Hash"); \
930
- } \
931
- else { \
932
- digest_hmac_options(retval, argv[0]); \
933
- } \
934
- } \
935
- return retval; \
936
- } \
937
+ if (argc > 2) { \
938
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc); \
939
+ } \
940
+ else { \
941
+ VALUE retval; \
942
+ JHash* hash = NULL; \
943
+ try { \
944
+ hash = digest_factory(ID2SYM(rb_intern(# s))); \
945
+ retval = wrap_digest_in_ruby(hash); \
946
+ } \
947
+ catch (Exception& e) { \
948
+ if (hash != NULL) { \
949
+ delete hash; \
950
+ } \
951
+ rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str()); \
952
+ } \
953
+ if (argc >= 1) { \
954
+ if (TYPE(argv[0]) == T_STRING) { \
955
+ digest_plaintext_eq(retval, argv[0], false); \
956
+ if (argc == 2) { \
957
+ Check_Type(argv[1], T_STRING); \
958
+ digest_hmac_key_eq(retval, argv[1], false); \
959
+ } \
960
+ hash->hash(); \
961
+ } \
962
+ else if (argc > 1) { \
963
+ rb_raise(rb_eArgError, "wrong argument types (expected a String or a Hash"); \
964
+ } \
965
+ else { \
966
+ digest_hmac_options(retval, argv[0]); \
967
+ } \
968
+ } \
969
+ return retval; \
970
+ } \
937
971
  }
938
972
  #include "defs/hmacs.def"
939
973
 
@@ -942,74 +976,74 @@ VALUE rb_digest_hmac_ ## r ##_new(int argc, VALUE *argv, VALUE self) \
942
976
  * as different algorithms behave differently */
943
977
  static string digest_hmac_key_eq(VALUE self, VALUE key, bool hex)
944
978
  {
945
- JHash *hash = NULL;
946
- Check_Type(key, T_STRING);
947
- Data_Get_Struct(self, JHash, hash);
948
- ((JHMAC*) hash)->setKey(string(StringValuePtr(key), RSTRING(key)->len), hex);
949
- return ((JHMAC*) hash)->getKey(hex);
979
+ JHash *hash = NULL;
980
+ Check_Type(key, T_STRING);
981
+ Data_Get_Struct(self, JHash, hash);
982
+ ((JHMAC*) hash)->setKey(string(StringValuePtr(key), RSTRING_LEN(key)), hex);
983
+ return ((JHMAC*) hash)->getKey(hex);
950
984
  }
951
985
 
952
986
  /**
953
987
  * call-seq:
954
- * key=(key)
988
+ * key=(key)
955
989
  *
956
990
  * Sets the key on a HMAC in binary.
957
991
  */
958
992
  VALUE rb_digest_hmac_key_eq(VALUE self, VALUE key)
959
993
  {
960
- digest_hmac_key_eq(self, key, false);
961
- return key;
994
+ digest_hmac_key_eq(self, key, false);
995
+ return key;
962
996
  }
963
997
 
964
998
  /**
965
999
  * call-seq:
966
- * key_hex=(key)
1000
+ * key_hex=(key)
967
1001
  *
968
1002
  * Sets the key on a HMAC in hex.
969
1003
  */
970
1004
  VALUE rb_digest_hmac_key_hex_eq(VALUE self, VALUE key)
971
1005
  {
972
- digest_hmac_key_eq(self, key, true);
973
- return key;
1006
+ digest_hmac_key_eq(self, key, true);
1007
+ return key;
974
1008
  }
975
1009
 
976
1010
 
977
1011
  /* Get the key. */
978
1012
  static string digest_hmac_key(VALUE self, bool hex)
979
1013
  {
980
- JHash *hash = NULL;
981
- Data_Get_Struct(self, JHash, hash);
982
- return ((JHMAC*) hash)->getKey(hex);
1014
+ JHash *hash = NULL;
1015
+ Data_Get_Struct(self, JHash, hash);
1016
+ return ((JHMAC*) hash)->getKey(hex);
983
1017
  }
984
1018
 
985
1019
  /**
986
1020
  * call-seq:
987
- * key => String
1021
+ * key => String
988
1022
  *
989
1023
  * Returns the key from the HMAC in binary.
990
1024
  */
991
1025
  VALUE rb_digest_hmac_key(VALUE self)
992
1026
  {
993
- string retval = digest_hmac_key(self, false);
994
- return rb_tainted_str_new(retval.data(), retval.length());
1027
+ string retval = digest_hmac_key(self, false);
1028
+ return rb_tainted_str_new(retval.data(), retval.length());
995
1029
  }
996
1030
 
997
1031
  /**
998
1032
  * call-seq:
999
- * key_hex => String
1033
+ * key_hex => String
1000
1034
  *
1001
1035
  * Returns the key from the HMAC in hex.
1002
1036
  */
1003
1037
  VALUE rb_digest_hmac_key_hex(VALUE self)
1004
1038
  {
1005
- string retval = digest_hmac_key(self, false);
1006
- return rb_tainted_str_new(retval.data(), retval.length());
1039
+ string retval = digest_hmac_key(self, false);
1040
+ return rb_tainted_str_new(retval.data(), retval.length());
1007
1041
  }
1008
1042
 
1009
1043
 
1010
1044
  /**
1011
1045
  * call-seq:
1012
- * key_length=(length)
1046
+ * key_length=(length)
1013
1047
  *
1014
1048
  * Sets the key length. Some HMACs require rather specific key lengths,
1015
1049
  * and if the key length you attempt to set is invalid, an exception will
@@ -1018,22 +1052,22 @@ VALUE rb_digest_hmac_key_hex(VALUE self)
1018
1052
  */
1019
1053
  VALUE rb_digest_hmac_key_length_eq(VALUE self, VALUE l)
1020
1054
  {
1021
- JHash *hash = NULL;
1022
- unsigned int length = NUM2UINT(l);
1023
- Data_Get_Struct(self, JHash, hash);
1024
- ((JHMAC*) hash)->setKeylength(length);
1025
- if (((JHMAC*) hash)->getKeylength() != length) {
1026
- rb_raise(rb_eCryptoPP_Error, "tried to set a key length of %d but %d was used", length, ((JHMAC*) hash)->getKeylength());
1027
- }
1028
- else {
1029
- return l;
1030
- }
1055
+ JHash *hash = NULL;
1056
+ unsigned int length = NUM2UINT(l);
1057
+ Data_Get_Struct(self, JHash, hash);
1058
+ ((JHMAC*) hash)->setKeylength(length);
1059
+ if (((JHMAC*) hash)->getKeylength() != length) {
1060
+ rb_raise(rb_eCryptoPP_Error, "tried to set a key length of %d but %d was used", length, ((JHMAC*) hash)->getKeylength());
1061
+ }
1062
+ else {
1063
+ return l;
1064
+ }
1031
1065
  }
1032
1066
 
1033
1067
 
1034
1068
  /**
1035
1069
  * call-seq:
1036
- * key_length=(length) => Integer
1070
+ * key_length=(length) => Integer
1037
1071
  *
1038
1072
  * Sets the key length. Some HMACs require rather specific key lengths,
1039
1073
  * and if the key length you attempt to set is invalid, an exception will
@@ -1042,79 +1076,79 @@ VALUE rb_digest_hmac_key_length_eq(VALUE self, VALUE l)
1042
1076
  */
1043
1077
  VALUE rb_digest_hmac_key_length(VALUE self)
1044
1078
  {
1045
- JHash *hash = NULL;
1046
- Data_Get_Struct(self, JHash, hash);
1047
- return rb_fix_new(((JHMAC*) hash)->getKeylength());
1079
+ JHash *hash = NULL;
1080
+ Data_Get_Struct(self, JHash, hash);
1081
+ return rb_fix_new(((JHMAC*) hash)->getKeylength());
1048
1082
  }
1049
1083
 
1050
1084
 
1051
1085
  /* Digest the plaintext. */
1052
1086
  static string module_hmac_digest(int argc, VALUE *argv, VALUE self, bool hex)
1053
1087
  {
1054
- JHash *hash;
1055
- VALUE algorithm, plaintext, key;
1056
-
1057
- rb_scan_args(argc, argv, "12", &algorithm, &plaintext, &key);
1058
- Check_Type(plaintext, T_STRING);
1059
- {
1060
- string retval;
1061
- hash = digest_factory(algorithm);
1062
- hash->setPlaintext(string(StringValuePtr(plaintext), RSTRING(plaintext)->len));
1063
- if (argc == 3) {
1064
- Check_Type(plaintext, T_STRING);
1065
- ((JHMAC*) hash)->setKey(string(StringValuePtr(key), RSTRING(key)->len));
1066
- }
1067
- hash->hash();
1068
- retval = hash->getHashtext(hex);
1069
-
1070
- delete hash;
1071
- return retval;
1072
- }
1088
+ JHash *hash;
1089
+ VALUE algorithm, plaintext, key;
1090
+
1091
+ rb_scan_args(argc, argv, "12", &algorithm, &plaintext, &key);
1092
+ Check_Type(plaintext, T_STRING);
1093
+ {
1094
+ string retval;
1095
+ hash = digest_factory(algorithm);
1096
+ hash->setPlaintext(string(StringValuePtr(plaintext), RSTRING_LEN(plaintext)));
1097
+ if (argc == 3) {
1098
+ Check_Type(plaintext, T_STRING);
1099
+ ((JHMAC*) hash)->setKey(string(StringValuePtr(key), RSTRING_LEN(key)));
1100
+ }
1101
+ hash->hash();
1102
+ retval = hash->getHashtext(hex);
1103
+
1104
+ delete hash;
1105
+ return retval;
1106
+ }
1073
1107
  }
1074
1108
 
1075
1109
  /**
1076
1110
  * call-seq:
1077
- * digest(algorithm, plaintext) => String
1078
- * digest(algorithm, plaintext, key) => String
1111
+ * digest(algorithm, plaintext) => String
1112
+ * digest(algorithm, plaintext, key) => String
1079
1113
  *
1080
1114
  * Singleton method for digesting with a HMAC. The plaintext and key values
1081
1115
  * are in binary and the return value is in binary.
1082
1116
  */
1083
1117
  VALUE rb_module_hmac_digest(int argc, VALUE *argv, VALUE self)
1084
1118
  {
1085
- string retval = module_hmac_digest(argc, argv, self, false);
1086
- return rb_tainted_str_new(retval.data(), retval.length());
1119
+ string retval = module_hmac_digest(argc, argv, self, false);
1120
+ return rb_tainted_str_new(retval.data(), retval.length());
1087
1121
  }
1088
1122
 
1089
1123
  /**
1090
1124
  * call-seq:
1091
- * digest_hex(algorithm, plaintext) => String
1092
- * digest_hex(algorithm, plaintext, key) => String
1125
+ * digest_hex(algorithm, plaintext) => String
1126
+ * digest_hex(algorithm, plaintext, key) => String
1093
1127
  *
1094
1128
  * Singleton method for digesting with a HMAC. The plaintext and key values
1095
1129
  * are in binary and the return value is in hex.
1096
1130
  */
1097
1131
  VALUE rb_module_hmac_digest_hex(int argc, VALUE *argv, VALUE self)
1098
1132
  {
1099
- string retval = module_hmac_digest(argc, argv, self, true);
1100
- return rb_tainted_str_new(retval.data(), retval.length());
1133
+ string retval = module_hmac_digest(argc, argv, self, true);
1134
+ return rb_tainted_str_new(retval.data(), retval.length());
1101
1135
  }
1102
1136
 
1103
1137
 
1104
1138
  /**
1105
1139
  * call-seq:
1106
- * hmac_list => Array
1140
+ * hmac_list => Array
1107
1141
  *
1108
1142
  * Returns an Array of available HMAC algorithms.
1109
1143
  */
1110
1144
  VALUE rb_module_hmac_list(VALUE self)
1111
1145
  {
1112
- VALUE ary;
1113
- ary = rb_ary_new();
1146
+ VALUE ary;
1147
+ ary = rb_ary_new();
1114
1148
 
1115
- # define HMAC_ALGORITHM_X(klass, r, c, s) \
1116
- rb_ary_push(ary, ID2SYM(rb_intern(# s)));
1117
- # include "defs/hmacs.def"
1149
+ # define HMAC_ALGORITHM_X(klass, r, c, s) \
1150
+ rb_ary_push(ary, ID2SYM(rb_intern(# s)));
1151
+ # include "defs/hmacs.def"
1118
1152
 
1119
- return ary;
1153
+ return ary;
1120
1154
  }