cryptopp 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +14 -0
- data/Guardfile +17 -0
- data/MIT-LICENSE +1 -1
- data/Rakefile +20 -27
- data/VERSION +1 -0
- data/cryptopp.gemspec +7 -182
- data/ext/Rakefile +2 -2
- data/ext/ciphers.cpp +619 -619
- data/ext/cryptopp.cpp +220 -217
- data/ext/cryptopp_ruby_api.h +7 -7
- data/ext/defs/hashes.def +16 -0
- data/ext/defs/hmacs.def +16 -0
- data/ext/digests.cpp +635 -601
- data/ext/extconf.rb +61 -19
- data/ext/j3way.cpp +5 -5
- data/ext/j3way.h +6 -6
- data/ext/jadler32.h +6 -6
- data/ext/jaes.cpp +5 -5
- data/ext/jaes.h +6 -6
- data/ext/jarc4.cpp +5 -5
- data/ext/jarc4.h +6 -6
- data/ext/jbase.cpp +94 -94
- data/ext/jbase.h +49 -55
- data/ext/jbasiccipherinfo.h +23 -23
- data/ext/jblowfish.cpp +5 -5
- data/ext/jblowfish.h +6 -6
- data/ext/jcamellia.cpp +5 -7
- data/ext/jcamellia.h +6 -8
- data/ext/jcast128.cpp +5 -5
- data/ext/jcast128.h +6 -6
- data/ext/jcast256.cpp +5 -5
- data/ext/jcast256.h +6 -6
- data/ext/jcipher.cpp +62 -62
- data/ext/jcipher.h +24 -24
- data/ext/jcipher_t.h +383 -383
- data/ext/jconfig.h +29 -46
- data/ext/jconstants.h +120 -122
- data/ext/jcrc32.h +6 -6
- data/ext/jdes.cpp +5 -5
- data/ext/jdes.h +6 -6
- data/ext/jdes_ede2.cpp +5 -5
- data/ext/jdes_ede2.h +6 -6
- data/ext/jdes_ede3.cpp +5 -5
- data/ext/jdes_ede3.h +6 -6
- data/ext/jdes_xex3.cpp +5 -5
- data/ext/jdes_xex3.h +6 -6
- data/ext/jdiamond2.cpp +5 -5
- data/ext/jdiamond2.h +6 -6
- data/ext/jdiamond2lite.cpp +5 -5
- data/ext/jdiamond2lite.h +6 -6
- data/ext/jexception.h +5 -5
- data/ext/jgost.cpp +5 -5
- data/ext/jgost.h +6 -6
- data/ext/jhash.cpp +51 -51
- data/ext/jhash.h +22 -27
- data/ext/jhash_t.h +59 -59
- data/ext/jhaval.h +15 -15
- data/ext/jhelpers.cpp +54 -54
- data/ext/jhelpers.h +3 -3
- data/ext/jhmac.cpp +20 -20
- data/ext/jhmac.h +15 -15
- data/ext/jhmac_t.h +40 -72
- data/ext/jidea.cpp +5 -5
- data/ext/jidea.h +6 -6
- data/ext/jmarc4.cpp +5 -5
- data/ext/jmarc4.h +6 -6
- data/ext/jmars.cpp +5 -5
- data/ext/jmars.h +6 -6
- data/ext/jmd2.h +9 -9
- data/ext/jmd4.h +9 -9
- data/ext/jmd5.h +9 -9
- data/ext/jpanamacipher.cpp +7 -7
- data/ext/jpanamacipher.h +9 -9
- data/ext/jpanamahash.h +9 -9
- data/ext/jrc2.cpp +15 -15
- data/ext/jrc2.h +11 -11
- data/ext/jrc5.cpp +5 -5
- data/ext/jrc5.h +6 -6
- data/ext/jrc6.cpp +5 -5
- data/ext/jrc6.h +6 -6
- data/ext/jripemd160.h +28 -28
- data/ext/jsafer.cpp +7 -7
- data/ext/jsafer.h +9 -9
- data/ext/jseal.cpp +7 -7
- data/ext/jseal.h +9 -9
- data/ext/jserpent.cpp +5 -5
- data/ext/jserpent.h +6 -6
- data/ext/jsha.h +38 -37
- data/ext/jsha3.h +130 -0
- data/ext/jsha3_blocksizes.h +73 -0
- data/ext/jshacal2.cpp +5 -5
- data/ext/jshacal2.h +6 -6
- data/ext/jshark.cpp +5 -7
- data/ext/jshark.h +6 -8
- data/ext/jsink.cpp +57 -57
- data/ext/jsink.h +127 -123
- data/ext/jskipjack.cpp +5 -5
- data/ext/jskipjack.h +6 -6
- data/ext/jsquare.cpp +5 -5
- data/ext/jsquare.h +6 -6
- data/ext/jstream.cpp +3 -3
- data/ext/jstream.h +3 -3
- data/ext/jstream_t.h +74 -126
- data/ext/jtea.cpp +5 -5
- data/ext/jtea.h +6 -6
- data/ext/jtiger.h +9 -11
- data/ext/jtwofish.cpp +5 -5
- data/ext/jtwofish.h +6 -6
- data/ext/jwhirlpool.h +9 -11
- data/ext/utils.cpp +3 -3
- data/extras/parser_c.rb +102 -102
- data/test/ciphers_test.rb +24 -30
- data/test/data/ciphers/3desval.yml +13 -0
- data/test/data/ciphers/3wayval.yml +17 -0
- data/test/data/ciphers/arc4.yml +22 -0
- data/test/data/ciphers/blowfish.yml +9 -0
- data/test/data/ciphers/camellia.yml +21 -0
- data/test/data/ciphers/cast128v.yml +13 -0
- data/test/data/ciphers/cast256v.yml +13 -0
- data/test/data/ciphers/descert.yml +723 -0
- data/test/data/ciphers/diamond.yml +31 -0
- data/test/data/ciphers/gostval.yml +33 -0
- data/test/data/ciphers/ideaval.yml +45 -0
- data/test/data/ciphers/marsval.yml +37 -0
- data/test/data/ciphers/panamac.yml +11 -0
- data/test/data/ciphers/rc2val.yml +49 -0
- data/test/data/ciphers/rc5val.yml +26 -0
- data/test/data/ciphers/rc6val.yml +25 -0
- data/test/data/ciphers/rijndael.yml +37 -0
- data/test/data/ciphers/saferval.yml +77 -0
- data/test/data/ciphers/seal.yml +6 -0
- data/test/data/ciphers/serpentv.yml +37 -0
- data/test/data/ciphers/shacal2.yml +21 -0
- data/test/data/ciphers/sharkval.yml +29 -0
- data/test/data/ciphers/skipjack.yml +5 -0
- data/test/data/ciphers/squareva.yml +33 -0
- data/test/data/ciphers/twofishv.yml +37 -0
- data/test/data/digests/adler32.yml +19 -0
- data/test/data/digests/crc32.yml +25 -0
- data/test/data/digests/haval.yml +7 -0
- data/test/data/digests/havalcer.yml +1 -0
- data/test/data/digests/md2.yml +22 -0
- data/test/data/digests/md4.yml +22 -0
- data/test/data/digests/md5.yml +22 -0
- data/test/data/digests/panamah.yml +13 -0
- data/test/data/digests/ripemd.yml +97 -0
- data/test/data/digests/sha.yml +25 -0
- data/test/data/digests/sha3.yml +23 -0
- data/test/data/digests/tiger.yml +30 -0
- data/test/data/digests/whirlpool.yml +25 -0
- data/test/data/hmacs/hmac.yml +14 -0
- data/test/digests_test.rb +17 -23
- data/test/hmacs_test.rb +25 -31
- data/test/test_helper.rb +31 -37
- metadata +70 -82
- data/test/data/ciphers/3desval.dat +0 -7
- data/test/data/ciphers/3wayval.dat +0 -6
- data/test/data/ciphers/arc4.dat +0 -8
- data/test/data/ciphers/blowfish.dat +0 -5
- data/test/data/ciphers/camellia.dat +0 -7
- data/test/data/ciphers/cast128v.dat +0 -5
- data/test/data/ciphers/cast256v.dat +0 -5
- data/test/data/ciphers/descert.dat +0 -198
- data/test/data/ciphers/diamond.dat +0 -9
- data/test/data/ciphers/gostval.dat +0 -10
- data/test/data/ciphers/ideaval.dat +0 -13
- data/test/data/ciphers/marsval.dat +0 -11
- data/test/data/ciphers/panamac.dat +0 -7
- data/test/data/ciphers/rc2val.dat +0 -10
- data/test/data/ciphers/rc5val.dat +0 -7
- data/test/data/ciphers/rc6val.dat +0 -8
- data/test/data/ciphers/rijndael.dat +0 -11
- data/test/data/ciphers/saferval.dat +0 -27
- data/test/data/ciphers/seal.dat +0 -3
- data/test/data/ciphers/serpentv.dat +0 -11
- data/test/data/ciphers/shacal2.dat +0 -7
- data/test/data/ciphers/sharkval.dat +0 -9
- data/test/data/ciphers/skipjack.dat +0 -3
- data/test/data/ciphers/squareva.dat +0 -10
- data/test/data/ciphers/twofishv.dat +0 -11
- data/test/data/digests/adler32.dat +0 -8
- data/test/data/digests/crc32.dat +0 -10
- data/test/data/digests/haval.dat +0 -4
- data/test/data/digests/havalcer.dat +0 -23
- data/test/data/digests/md2.dat +0 -9
- data/test/data/digests/md4.dat +0 -9
- data/test/data/digests/md5.dat +0 -9
- data/test/data/digests/panamah.dat +0 -8
- data/test/data/digests/ripemd.dat +0 -43
- data/test/data/digests/sha.dat +0 -19
- data/test/data/digests/tiger.dat +0 -11
- data/test/data/digests/whirlpool.dat +0 -13
- data/test/data/hmacs/hmac.dat +0 -6
data/ext/cryptopp_ruby_api.h
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
|
2
2
|
/*
|
3
|
-
* Copyright (c) 2002-
|
4
|
-
* Crypto++ copyright (c) 1995-
|
5
|
-
* See
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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);
|
data/ext/defs/hashes.def
CHANGED
@@ -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
|
data/ext/defs/hmacs.def
CHANGED
@@ -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
|
data/ext/digests.cpp
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
|
2
2
|
/*
|
3
|
-
* Copyright (c) 2002-
|
4
|
-
* Crypto++ copyright (c) 1995-
|
5
|
-
* See
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
#
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
#
|
69
|
-
|
70
|
-
#
|
71
|
-
#
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
#
|
76
|
-
|
77
|
-
#
|
78
|
-
#
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
#
|
83
|
-
|
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
|
-
|
91
|
-
#
|
92
|
-
|
93
|
-
#
|
94
|
-
|
95
|
-
#
|
96
|
-
|
97
|
-
#
|
98
|
-
|
99
|
-
#
|
100
|
-
|
101
|
-
#
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
#
|
153
|
-
|
154
|
-
|
155
|
-
#
|
156
|
-
|
157
|
-
#
|
158
|
-
|
159
|
-
|
160
|
-
#
|
161
|
-
|
162
|
-
#
|
163
|
-
|
164
|
-
|
165
|
-
#
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
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
|
-
|
178
|
-
#
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
#
|
184
|
-
|
185
|
-
#
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
#
|
191
|
-
|
192
|
-
#
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
#
|
198
|
-
|
199
|
-
|
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
|
-
*
|
205
|
-
*
|
206
|
-
*
|
207
|
-
*
|
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
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
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
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
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
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
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
|
-
*
|
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
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
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
|
-
|
329
|
-
|
330
|
-
|
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
|
-
*
|
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
|
-
|
342
|
-
|
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
|
-
*
|
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
|
-
|
354
|
-
|
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
|
-
|
362
|
-
|
363
|
-
|
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
|
-
*
|
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
|
-
|
375
|
-
|
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
|
-
*
|
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
|
-
|
387
|
-
|
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
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
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
|
-
*
|
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
|
-
|
410
|
-
|
443
|
+
digest_plaintext_eq(self, plaintext, false);
|
444
|
+
return plaintext;
|
411
445
|
}
|
412
446
|
|
413
447
|
/**
|
414
448
|
* call-seq:
|
415
|
-
*
|
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
|
-
|
422
|
-
|
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
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
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
|
-
*
|
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
|
-
|
444
|
-
|
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
|
-
*
|
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
|
-
|
456
|
-
|
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
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
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
|
-
*
|
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
|
-
|
479
|
-
|
512
|
+
digest_digest_eq(self, digest, false);
|
513
|
+
return digest;
|
480
514
|
}
|
481
515
|
|
482
516
|
/**
|
483
517
|
* call-seq:
|
484
|
-
*
|
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
|
-
|
491
|
-
|
524
|
+
digest_digest_eq(self, digest, true);
|
525
|
+
return digest;
|
492
526
|
}
|
493
527
|
|
494
528
|
|
495
529
|
/**
|
496
530
|
* call-seq:
|
497
|
-
*
|
531
|
+
* inspect => String
|
498
532
|
*
|
499
533
|
* Inspect method.
|
500
534
|
*/
|
501
535
|
VALUE rb_digest_inspect(VALUE self)
|
502
536
|
{
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
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
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
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
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
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
|
-
*
|
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
|
-
|
594
|
-
|
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
|
-
*
|
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
|
-
|
606
|
-
|
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
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
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
|
-
*
|
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
|
-
*
|
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
|
-
|
648
|
-
|
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
|
-
*
|
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
|
-
*
|
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
|
-
|
666
|
-
|
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
|
-
*
|
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
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
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
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
#
|
696
|
-
|
697
|
-
|
698
|
-
#
|
699
|
-
|
700
|
-
#
|
701
|
-
|
702
|
-
|
703
|
-
#
|
704
|
-
|
705
|
-
#
|
706
|
-
|
707
|
-
|
708
|
-
#
|
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
|
-
*
|
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
|
-
|
722
|
-
|
723
|
-
|
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
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
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
|
-
*
|
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
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
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
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
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
|
-
*
|
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
|
-
|
780
|
-
|
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
|
-
*
|
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
|
-
|
792
|
-
|
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
|
-
*
|
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
|
-
|
805
|
-
|
838
|
+
VALUE ary;
|
839
|
+
ary = rb_ary_new();
|
806
840
|
|
807
|
-
#
|
808
|
-
|
809
|
-
#
|
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
|
-
#
|
812
|
-
|
813
|
-
#
|
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
|
-
|
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
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
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
|
-
*
|
850
|
-
*
|
851
|
-
*
|
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
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
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
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
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
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
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
|
-
*
|
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
|
-
|
961
|
-
|
994
|
+
digest_hmac_key_eq(self, key, false);
|
995
|
+
return key;
|
962
996
|
}
|
963
997
|
|
964
998
|
/**
|
965
999
|
* call-seq:
|
966
|
-
*
|
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
|
-
|
973
|
-
|
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
|
-
|
981
|
-
|
982
|
-
|
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
|
-
*
|
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
|
-
|
994
|
-
|
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
|
-
*
|
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
|
-
|
1006
|
-
|
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
|
-
*
|
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
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
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
|
-
*
|
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
|
-
|
1046
|
-
|
1047
|
-
|
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
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
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
|
-
*
|
1078
|
-
*
|
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
|
-
|
1086
|
-
|
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
|
-
*
|
1092
|
-
*
|
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
|
-
|
1100
|
-
|
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
|
-
*
|
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
|
-
|
1113
|
-
|
1146
|
+
VALUE ary;
|
1147
|
+
ary = rb_ary_new();
|
1114
1148
|
|
1115
|
-
#
|
1116
|
-
|
1117
|
-
#
|
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
|
-
|
1153
|
+
return ary;
|
1120
1154
|
}
|