cryptopp 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/MIT-LICENSE +22 -0
- data/README +7 -0
- data/Rakefile +42 -0
- data/cryptopp.gemspec +199 -0
- data/ext/.gitignore +5 -0
- data/ext/Rakefile +16 -0
- data/ext/ciphers.cpp +1402 -0
- data/ext/cryptopp.cpp +285 -0
- data/ext/cryptopp_ruby_api.h +139 -0
- data/ext/defs/block_modes.def +10 -0
- data/ext/defs/checksums.def +10 -0
- data/ext/defs/ciphers.def +136 -0
- data/ext/defs/hashes.def +78 -0
- data/ext/defs/hmacs.def +54 -0
- data/ext/defs/paddings.def +9 -0
- data/ext/defs/rngs.def +7 -0
- data/ext/digests.cpp +1120 -0
- data/ext/extconf.rb +39 -0
- data/ext/j3way.cpp +22 -0
- data/ext/j3way.h +29 -0
- data/ext/jadler32.h +32 -0
- data/ext/jaes.cpp +22 -0
- data/ext/jaes.h +31 -0
- data/ext/jarc4.cpp +22 -0
- data/ext/jarc4.h +37 -0
- data/ext/jbase.cpp +172 -0
- data/ext/jbase.h +92 -0
- data/ext/jbasiccipherinfo.h +74 -0
- data/ext/jblowfish.cpp +22 -0
- data/ext/jblowfish.h +29 -0
- data/ext/jcamellia.cpp +24 -0
- data/ext/jcamellia.h +33 -0
- data/ext/jcast128.cpp +22 -0
- data/ext/jcast128.h +31 -0
- data/ext/jcast256.cpp +22 -0
- data/ext/jcast256.h +31 -0
- data/ext/jcipher.cpp +112 -0
- data/ext/jcipher.h +42 -0
- data/ext/jcipher_t.h +469 -0
- data/ext/jconfig.h +127 -0
- data/ext/jconstants.h +189 -0
- data/ext/jcrc32.h +32 -0
- data/ext/jdes.cpp +22 -0
- data/ext/jdes.h +31 -0
- data/ext/jdes_ede2.cpp +22 -0
- data/ext/jdes_ede2.h +31 -0
- data/ext/jdes_ede3.cpp +22 -0
- data/ext/jdes_ede3.h +31 -0
- data/ext/jdes_xex3.cpp +22 -0
- data/ext/jdes_xex3.h +31 -0
- data/ext/jdiamond2.cpp +22 -0
- data/ext/jdiamond2.h +31 -0
- data/ext/jdiamond2lite.cpp +22 -0
- data/ext/jdiamond2lite.h +31 -0
- data/ext/jexception.h +20 -0
- data/ext/jgost.cpp +22 -0
- data/ext/jgost.h +31 -0
- data/ext/jhash.cpp +92 -0
- data/ext/jhash.h +54 -0
- data/ext/jhash_t.h +121 -0
- data/ext/jhaval.h +64 -0
- data/ext/jhelpers.cpp +90 -0
- data/ext/jhelpers.h +38 -0
- data/ext/jhmac.cpp +44 -0
- data/ext/jhmac.h +34 -0
- data/ext/jhmac_t.h +125 -0
- data/ext/jidea.cpp +22 -0
- data/ext/jidea.h +31 -0
- data/ext/jmarc4.cpp +22 -0
- data/ext/jmarc4.h +37 -0
- data/ext/jmars.cpp +22 -0
- data/ext/jmars.h +31 -0
- data/ext/jmd2.h +56 -0
- data/ext/jmd4.h +56 -0
- data/ext/jmd5.h +56 -0
- data/ext/jpanamacipher.cpp +32 -0
- data/ext/jpanamacipher.h +46 -0
- data/ext/jpanamahash.h +44 -0
- data/ext/jrc2.cpp +44 -0
- data/ext/jrc2.h +39 -0
- data/ext/jrc5.cpp +22 -0
- data/ext/jrc5.h +31 -0
- data/ext/jrc6.cpp +22 -0
- data/ext/jrc6.h +31 -0
- data/ext/jripemd160.h +113 -0
- data/ext/jsafer.cpp +32 -0
- data/ext/jsafer.h +42 -0
- data/ext/jseal.cpp +32 -0
- data/ext/jseal.h +42 -0
- data/ext/jserpent.cpp +22 -0
- data/ext/jserpent.h +31 -0
- data/ext/jsha.h +122 -0
- data/ext/jshacal2.cpp +22 -0
- data/ext/jshacal2.h +31 -0
- data/ext/jshark.cpp +24 -0
- data/ext/jshark.h +33 -0
- data/ext/jsink.cpp +90 -0
- data/ext/jsink.h +154 -0
- data/ext/jskipjack.cpp +22 -0
- data/ext/jskipjack.h +31 -0
- data/ext/jsquare.cpp +22 -0
- data/ext/jsquare.h +31 -0
- data/ext/jstream.cpp +8 -0
- data/ext/jstream.h +20 -0
- data/ext/jstream_t.h +175 -0
- data/ext/jtea.cpp +22 -0
- data/ext/jtea.h +31 -0
- data/ext/jtiger.h +52 -0
- data/ext/jtwofish.cpp +22 -0
- data/ext/jtwofish.h +31 -0
- data/ext/jwhirlpool.h +52 -0
- data/ext/utils.cpp +8 -0
- data/extras/parser_c.rb +114 -0
- data/test/ciphers_test.rb +37 -0
- data/test/data/ciphers/3desval.dat +7 -0
- data/test/data/ciphers/3wayval.dat +6 -0
- data/test/data/ciphers/arc4.dat +8 -0
- data/test/data/ciphers/blowfish.dat +5 -0
- data/test/data/ciphers/camellia.dat +7 -0
- data/test/data/ciphers/cast128v.dat +5 -0
- data/test/data/ciphers/cast256v.dat +5 -0
- data/test/data/ciphers/descert.dat +198 -0
- data/test/data/ciphers/diamond.dat +9 -0
- data/test/data/ciphers/gostval.dat +10 -0
- data/test/data/ciphers/ideaval.dat +13 -0
- data/test/data/ciphers/marsval.dat +11 -0
- data/test/data/ciphers/panamac.dat +7 -0
- data/test/data/ciphers/rc2val.dat +10 -0
- data/test/data/ciphers/rc5val.dat +7 -0
- data/test/data/ciphers/rc6val.dat +8 -0
- data/test/data/ciphers/rijndael.dat +11 -0
- data/test/data/ciphers/saferval.dat +27 -0
- data/test/data/ciphers/seal.dat +3 -0
- data/test/data/ciphers/serpentv.dat +11 -0
- data/test/data/ciphers/shacal2.dat +7 -0
- data/test/data/ciphers/sharkval.dat +9 -0
- data/test/data/ciphers/skipjack.dat +3 -0
- data/test/data/ciphers/squareva.dat +10 -0
- data/test/data/ciphers/twofishv.dat +11 -0
- data/test/data/digests/adler32.dat +8 -0
- data/test/data/digests/crc32.dat +10 -0
- data/test/data/digests/haval.dat +4 -0
- data/test/data/digests/havalcer.dat +23 -0
- data/test/data/digests/md2.dat +9 -0
- data/test/data/digests/md4.dat +9 -0
- data/test/data/digests/md5.dat +9 -0
- data/test/data/digests/panamah.dat +8 -0
- data/test/data/digests/ripemd.dat +43 -0
- data/test/data/digests/sha.dat +19 -0
- data/test/data/digests/tiger.dat +11 -0
- data/test/data/digests/whirlpool.dat +13 -0
- data/test/data/hmacs/hmac.dat +6 -0
- data/test/digests_test.rb +29 -0
- data/test/hmacs_test.rb +38 -0
- data/test/test_helper.rb +42 -0
- metadata +220 -0
data/ext/jhash_t.h
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
|
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
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef __JHASH_T_H__
|
9
|
+
#define __JHASH_T_H__
|
10
|
+
|
11
|
+
#include "jhash.h"
|
12
|
+
|
13
|
+
#include "files.h"
|
14
|
+
|
15
|
+
using namespace CryptoPP;
|
16
|
+
|
17
|
+
template <typename HASH, enum HashEnum TYPE>
|
18
|
+
class JHash_Template : public JHash
|
19
|
+
{
|
20
|
+
public:
|
21
|
+
JHash_Template(string plaintext = "");
|
22
|
+
enum HashEnum getHashType() const;
|
23
|
+
bool hash();
|
24
|
+
bool validate();
|
25
|
+
bool validate(string plaintext, string hashtext);
|
26
|
+
string hashRubyIO(VALUE* in, bool hex = true);
|
27
|
+
|
28
|
+
/* This is deprecated. It was used before using RubyIO. Use it
|
29
|
+
if you're using this code in something other than the CryptoPP Ruby
|
30
|
+
extension... */
|
31
|
+
//string hashFile(const string filename, bool hex = true);
|
32
|
+
};
|
33
|
+
|
34
|
+
#define HASH_TYPE TYPE
|
35
|
+
|
36
|
+
template <typename HASH, enum HashEnum TYPE>
|
37
|
+
JHash_Template<HASH, TYPE>::JHash_Template(string plaintext) : JHash(plaintext)
|
38
|
+
{
|
39
|
+
itsHashModule = new HASH;
|
40
|
+
}
|
41
|
+
|
42
|
+
template <typename HASH, enum HashEnum TYPE>
|
43
|
+
HashEnum JHash_Template<HASH, TYPE>::getHashType() const
|
44
|
+
{
|
45
|
+
return TYPE;
|
46
|
+
}
|
47
|
+
|
48
|
+
template <typename HASH, enum HashEnum TYPE>
|
49
|
+
bool JHash_Template<HASH, TYPE>::hash()
|
50
|
+
{
|
51
|
+
itsHashtext.erase();
|
52
|
+
|
53
|
+
StringSource s(itsPlaintext, true, new HashFilter(*itsHashModule, new StringSink(itsHashtext)));
|
54
|
+
return true;
|
55
|
+
}
|
56
|
+
|
57
|
+
template <typename HASH, enum HashEnum TYPE>
|
58
|
+
bool JHash_Template<HASH, TYPE>::validate()
|
59
|
+
{
|
60
|
+
return validate(itsPlaintext, itsHashtext);
|
61
|
+
}
|
62
|
+
|
63
|
+
template <typename HASH, enum HashEnum TYPE>
|
64
|
+
bool JHash_Template<HASH, TYPE>::validate(string plaintext, string hashtext)
|
65
|
+
{
|
66
|
+
if (itsHashModule == NULL) {
|
67
|
+
throw;
|
68
|
+
}
|
69
|
+
|
70
|
+
return itsHashModule->VerifyDigest((const byte*) hashtext.data(), (const byte*) plaintext.data(), plaintext.length());
|
71
|
+
}
|
72
|
+
|
73
|
+
template <typename HASH, enum HashEnum TYPE>
|
74
|
+
string JHash_Template<HASH, TYPE>::hashRubyIO(VALUE* in, bool hex)
|
75
|
+
{
|
76
|
+
if (itsHashModule == NULL) {
|
77
|
+
throw;
|
78
|
+
}
|
79
|
+
|
80
|
+
string retval;
|
81
|
+
try {
|
82
|
+
if (hex) {
|
83
|
+
RubyIOSource f(&in, true, new HashFilter(*itsHashModule, new HexEncoder(new StringSink(retval), false)));
|
84
|
+
}
|
85
|
+
else {
|
86
|
+
RubyIOSource f(&in, true, new HashFilter(*itsHashModule, new StringSink(retval)));
|
87
|
+
}
|
88
|
+
}
|
89
|
+
catch (Exception e) {
|
90
|
+
throw e;
|
91
|
+
}
|
92
|
+
return retval;
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
/* This is deprecated. It was used before using RubyIO. Use it
|
97
|
+
if you're using this code in something other than the CryptoPP Ruby
|
98
|
+
extension... */
|
99
|
+
/*template <typename HASH, enum HashEnum TYPE>
|
100
|
+
string JHash_Template<HASH, TYPE>::hashFile(const string filename, bool hex)
|
101
|
+
{
|
102
|
+
if (itsHashModule == NULL) {
|
103
|
+
throw;
|
104
|
+
}
|
105
|
+
|
106
|
+
string retval;
|
107
|
+
try {
|
108
|
+
if (hex) {
|
109
|
+
FileSource f(filename.c_str(), true, new HashFilter(*itsHashModule, new HexEncoder(new StringSink(retval), false)));
|
110
|
+
}
|
111
|
+
else {
|
112
|
+
FileSource f(filename.c_str(), true, new HashFilter(*itsHashModule, new StringSink(retval)));
|
113
|
+
}
|
114
|
+
}
|
115
|
+
catch (FileStore::OpenErr e) {
|
116
|
+
throw e;
|
117
|
+
}
|
118
|
+
return retval;
|
119
|
+
}*/
|
120
|
+
|
121
|
+
#endif
|
data/ext/jhaval.h
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
|
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
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef __JHAVAL_H__
|
9
|
+
#define __JHAVAL_H__
|
10
|
+
|
11
|
+
#include "jconfig.h"
|
12
|
+
|
13
|
+
#if ENABLED_HAVAL_HASH || ENABLED_HAVAL3_HASH || ENABLED_HAVAL4_HASH || ENABLED_HAVAL5_HASH
|
14
|
+
|
15
|
+
#include "jhash_t.h"
|
16
|
+
|
17
|
+
// Crypto++ headers...
|
18
|
+
|
19
|
+
#include "haval.h"
|
20
|
+
|
21
|
+
using namespace CryptoPP;
|
22
|
+
|
23
|
+
#if ENABLED_HAVAL_HASH
|
24
|
+
class JHAVAL : public JHash_Template<HAVAL, HAVAL_HASH>
|
25
|
+
{
|
26
|
+
public:
|
27
|
+
JHAVAL(string plaintext = "") : JHash_Template<HAVAL, HAVAL_HASH>(plaintext) { }
|
28
|
+
|
29
|
+
static string getHashName() { return "HAVAL"; }
|
30
|
+
};
|
31
|
+
#endif
|
32
|
+
|
33
|
+
#if ENABLED_HAVAL3_HASH
|
34
|
+
class JHAVAL3 : public JHash_Template<HAVAL3, HAVAL3_HASH>
|
35
|
+
{
|
36
|
+
public:
|
37
|
+
JHAVAL3(string plaintext = "") : JHash_Template<HAVAL3, HAVAL3_HASH>(plaintext) { }
|
38
|
+
|
39
|
+
static string getHashName() { return "HAVAL3"; }
|
40
|
+
};
|
41
|
+
#endif
|
42
|
+
|
43
|
+
#if ENABLED_HAVAL4_HASH
|
44
|
+
class JHAVAL4 : public JHash_Template<HAVAL4, HAVAL4_HASH>
|
45
|
+
{
|
46
|
+
public:
|
47
|
+
JHAVAL4(string plaintext = "") : JHash_Template<HAVAL4, HAVAL4_HASH>(plaintext) { }
|
48
|
+
|
49
|
+
static string getHashName() { return "HAVAL4"; }
|
50
|
+
};
|
51
|
+
#endif
|
52
|
+
|
53
|
+
#if ENABLED_HAVAL5_HASH
|
54
|
+
class JHAVAL5 : public JHash_Template<HAVAL5, HAVAL5_HASH>
|
55
|
+
{
|
56
|
+
public:
|
57
|
+
JHAVAL5(string plaintext = "") : JHash_Template<HAVAL5, HAVAL5_HASH>(plaintext) { }
|
58
|
+
|
59
|
+
static string getHashName() { return "HAVAL5"; }
|
60
|
+
};
|
61
|
+
#endif
|
62
|
+
|
63
|
+
#endif
|
64
|
+
#endif
|
data/ext/jhelpers.cpp
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
|
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
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include "jhelpers.h"
|
9
|
+
|
10
|
+
using namespace CryptoPP;
|
11
|
+
|
12
|
+
string bin2hex(const string bin, const bool uppercase)
|
13
|
+
{
|
14
|
+
string retval;
|
15
|
+
StringSource(bin, true, new HexEncoder(new StringSink(retval), uppercase));
|
16
|
+
return retval;
|
17
|
+
}
|
18
|
+
|
19
|
+
string hex2bin(const string hex)
|
20
|
+
{
|
21
|
+
string retval;
|
22
|
+
StringSource(hex, true, new HexDecoder(new StringSink(retval)));
|
23
|
+
return retval;
|
24
|
+
}
|
25
|
+
|
26
|
+
char* bin2hex(const char* bin, size_t length, const bool uppercase)
|
27
|
+
{
|
28
|
+
char* retval = new char[length * 2 + 1];
|
29
|
+
StringSource(bin, true, new HexEncoder(new ArraySink((byte*) retval, length * 2), uppercase));
|
30
|
+
return retval;
|
31
|
+
}
|
32
|
+
|
33
|
+
char* hex2bin(const char* hex, size_t length)
|
34
|
+
{
|
35
|
+
char* retval = new char[length / 2 + 1];
|
36
|
+
StringSource(hex, true, new HexDecoder(new ArraySink((byte*) retval, length / 2)));
|
37
|
+
return retval;
|
38
|
+
}
|
39
|
+
|
40
|
+
string generateIV(const unsigned int size, const enum RNGEnum rng)
|
41
|
+
{
|
42
|
+
string retval;
|
43
|
+
RandomNumberGenerator* randPool = NULL;
|
44
|
+
|
45
|
+
#ifdef NONBLOCKING_RNG_AVAILABLE
|
46
|
+
if (rng == NON_BLOCKING_RNG) {
|
47
|
+
randPool = new NonblockingRng;
|
48
|
+
}
|
49
|
+
#endif
|
50
|
+
|
51
|
+
#if defined(NONBLOCKING_RNG_AVAILABLE) && defined(BLOCKING_RNG_AVAILABLE)
|
52
|
+
else
|
53
|
+
#endif
|
54
|
+
|
55
|
+
#ifdef BLOCKING_RNG_AVAILABLE
|
56
|
+
if (rng == BLOCKING_RNG) {
|
57
|
+
randPool = new BlockingRng;
|
58
|
+
}
|
59
|
+
#endif
|
60
|
+
|
61
|
+
if (rng == RAND_RNG) {
|
62
|
+
for (unsigned int i = 0; i < size; i++) {
|
63
|
+
retval += (char)(255.0 * rand() / RAND_MAX);
|
64
|
+
}
|
65
|
+
}
|
66
|
+
else if (randPool != NULL) {
|
67
|
+
for (unsigned int i = 0; i < size; i++) {
|
68
|
+
retval += (char) randPool->GenerateByte();
|
69
|
+
}
|
70
|
+
delete randPool;
|
71
|
+
}
|
72
|
+
|
73
|
+
return retval;
|
74
|
+
}
|
75
|
+
|
76
|
+
unsigned int checkBounds(unsigned int length, unsigned int min, unsigned int max, unsigned short int multiple)
|
77
|
+
{
|
78
|
+
if (min == max) {
|
79
|
+
return min;
|
80
|
+
}
|
81
|
+
else if (length < min) {
|
82
|
+
return min;
|
83
|
+
}
|
84
|
+
else if (length > max) {
|
85
|
+
return max;
|
86
|
+
}
|
87
|
+
else {
|
88
|
+
return (length + multiple - 1) - ((length + multiple - 1) % multiple);
|
89
|
+
}
|
90
|
+
}
|
data/ext/jhelpers.h
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
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
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef __JHELPERS_H__
|
9
|
+
#define __JHELPERS_H__
|
10
|
+
|
11
|
+
#include <string>
|
12
|
+
#include <cctype>
|
13
|
+
|
14
|
+
// Crypto++ headers...
|
15
|
+
|
16
|
+
#include "hex.h"
|
17
|
+
#include "osrng.h"
|
18
|
+
|
19
|
+
#include "jconstants.h"
|
20
|
+
|
21
|
+
typedef unsigned char byte;
|
22
|
+
|
23
|
+
using namespace std;
|
24
|
+
|
25
|
+
string bin2hex(const string bin, const bool uppercase = false);
|
26
|
+
string hex2bin(const string hex);
|
27
|
+
|
28
|
+
char* bin2hex(const char* bin, size_t length, const bool uppercase = false);
|
29
|
+
char* hex2bin(const char* hex, size_t length);
|
30
|
+
|
31
|
+
string generateIV(const unsigned int size, const enum RNGEnum rng = DEFAULT_RNG);
|
32
|
+
|
33
|
+
// used to check the bounds of things like keylengths,
|
34
|
+
// rounds to perform, etc.:
|
35
|
+
|
36
|
+
unsigned int checkBounds(unsigned int length, unsigned int min, unsigned int max, unsigned short int multiple = 1);
|
37
|
+
|
38
|
+
#endif
|
data/ext/jhmac.cpp
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
|
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
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include "jhmac.h"
|
9
|
+
|
10
|
+
unsigned int JHMAC::getKeylength() const
|
11
|
+
{
|
12
|
+
return itsKeylength;
|
13
|
+
}
|
14
|
+
|
15
|
+
string JHMAC::getKey(const bool hex) const
|
16
|
+
{
|
17
|
+
if (hex) {
|
18
|
+
return bin2hex(itsKey);
|
19
|
+
}
|
20
|
+
else {
|
21
|
+
return itsKey;
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
unsigned int JHMAC::setKeylength(const unsigned int keylength)
|
26
|
+
{
|
27
|
+
itsKeylength = checkBounds(keylength, 0, UINT_MAX);
|
28
|
+
|
29
|
+
return itsKeylength;
|
30
|
+
}
|
31
|
+
|
32
|
+
unsigned int JHMAC::setKey(const string key, const bool hex)
|
33
|
+
{
|
34
|
+
if (hex) {
|
35
|
+
itsKey = hex2bin(key);
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
itsKey = key;
|
39
|
+
}
|
40
|
+
|
41
|
+
itsKey.resize(setKeylength(itsKey.length()));
|
42
|
+
|
43
|
+
return itsKeylength;
|
44
|
+
}
|
data/ext/jhmac.h
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
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
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef __JHMAC_H__
|
9
|
+
#define __JHMAC_H__
|
10
|
+
|
11
|
+
#include "jhash.h"
|
12
|
+
|
13
|
+
using namespace CryptoPP;
|
14
|
+
|
15
|
+
class JHMAC : public JHash
|
16
|
+
{
|
17
|
+
public:
|
18
|
+
JHMAC(string plaintext = "") : JHash(plaintext)
|
19
|
+
{
|
20
|
+
itsKeylength = 16;
|
21
|
+
}
|
22
|
+
|
23
|
+
unsigned int getKeylength() const;
|
24
|
+
string getKey(const bool hex = false) const;
|
25
|
+
|
26
|
+
unsigned int setKeylength(const unsigned int keylength);
|
27
|
+
unsigned int setKey(const string key, const bool hex = false);
|
28
|
+
|
29
|
+
protected:
|
30
|
+
string itsKey;
|
31
|
+
unsigned int itsKeylength;
|
32
|
+
};
|
33
|
+
|
34
|
+
#endif
|
data/ext/jhmac_t.h
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
|
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
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef __JHMAC_T_H__
|
9
|
+
#define __JHMAC_T_H__
|
10
|
+
|
11
|
+
#include "jhmac.h"
|
12
|
+
|
13
|
+
// Crypto++ headers...
|
14
|
+
|
15
|
+
#include "hmac.h"
|
16
|
+
|
17
|
+
using namespace CryptoPP;
|
18
|
+
|
19
|
+
template <typename HASH, enum HashEnum TYPE>
|
20
|
+
class JHMAC_Template : public JHMAC
|
21
|
+
{
|
22
|
+
public:
|
23
|
+
JHMAC_Template(string plaintext = "");
|
24
|
+
inline enum HashEnum getHashType() const;
|
25
|
+
bool hash();
|
26
|
+
bool validate();
|
27
|
+
bool validate(string plaintext, string hashtext);
|
28
|
+
string hashRubyIO(VALUE* in, bool hex = true);
|
29
|
+
|
30
|
+
/* This is deprecated. It was used before using php_streams. Use it
|
31
|
+
if you're using this code in something other than the cryptopp PHP
|
32
|
+
extension...*/
|
33
|
+
//string hashFile(const string filename, bool hex = true);
|
34
|
+
};
|
35
|
+
|
36
|
+
template <typename HASH, enum HashEnum TYPE>
|
37
|
+
JHMAC_Template<HASH, TYPE>::JHMAC_Template(string plaintext) : JHMAC(plaintext)
|
38
|
+
{
|
39
|
+
itsHashModule = new HMAC<HASH>;
|
40
|
+
}
|
41
|
+
|
42
|
+
template <typename HASH, enum HashEnum TYPE>
|
43
|
+
HashEnum JHMAC_Template<HASH, TYPE>::getHashType() const
|
44
|
+
{
|
45
|
+
return TYPE;
|
46
|
+
}
|
47
|
+
|
48
|
+
template <typename HASH, enum HashEnum TYPE>
|
49
|
+
bool JHMAC_Template<HASH, TYPE>::hash()
|
50
|
+
{
|
51
|
+
((HMAC<HASH>*) itsHashModule)->SetKey((byte*) itsKey.data(), itsKeylength);
|
52
|
+
itsHashtext.erase();
|
53
|
+
StringSource s(itsPlaintext, true, new HashFilter(*itsHashModule, new StringSink(itsHashtext)));
|
54
|
+
return true;
|
55
|
+
}
|
56
|
+
|
57
|
+
template <typename HASH, enum HashEnum TYPE>
|
58
|
+
bool JHMAC_Template<HASH, TYPE>::validate()
|
59
|
+
{
|
60
|
+
return validate(itsPlaintext, itsHashtext);
|
61
|
+
}
|
62
|
+
|
63
|
+
template <typename HASH, enum HashEnum TYPE>
|
64
|
+
bool JHMAC_Template<HASH, TYPE>::validate(string plaintext, string hashtext)
|
65
|
+
{
|
66
|
+
if (itsHashModule == NULL) {
|
67
|
+
throw;
|
68
|
+
}
|
69
|
+
|
70
|
+
((HMAC<HASH>*) itsHashModule)->SetKey((byte*) itsKey.data(), itsKeylength);
|
71
|
+
|
72
|
+
return itsHashModule->VerifyDigest((const byte*) hashtext.data(), (const byte*) plaintext.data(), plaintext.length());
|
73
|
+
}
|
74
|
+
|
75
|
+
template <typename HASH, enum HashEnum TYPE>
|
76
|
+
string JHMAC_Template<HASH, TYPE>::hashRubyIO(VALUE* in, bool hex)
|
77
|
+
{
|
78
|
+
if (itsHashModule == NULL) {
|
79
|
+
throw;
|
80
|
+
}
|
81
|
+
|
82
|
+
((HMAC<HASH>*) itsHashModule)->SetKey((byte*) itsKey.data(), itsKeylength);
|
83
|
+
string retval;
|
84
|
+
try {
|
85
|
+
if (hex) {
|
86
|
+
RubyIOSource f(&in, true, new HashFilter(*itsHashModule, new HexEncoder(new StringSink(retval), false)));
|
87
|
+
}
|
88
|
+
else {
|
89
|
+
RubyIOSource f(&in, true, new HashFilter(*itsHashModule, new StringSink(retval)));
|
90
|
+
}
|
91
|
+
}
|
92
|
+
catch (Exception e) {
|
93
|
+
throw e;
|
94
|
+
}
|
95
|
+
return retval;
|
96
|
+
}
|
97
|
+
|
98
|
+
|
99
|
+
/* This is deprecated. It was used before using php_streams. Use it
|
100
|
+
if you're using this code in something other than the cryptopp PHP
|
101
|
+
extension... */
|
102
|
+
/*template <typename HASH, enum HashEnum TYPE>
|
103
|
+
string JHMAC_Template<HASH, TYPE>::hashFile(const string filename, bool hex)
|
104
|
+
{
|
105
|
+
if (itsHashModule == NULL) {
|
106
|
+
throw;
|
107
|
+
}
|
108
|
+
|
109
|
+
((HMAC<HASH>*) itsHashModule)->SetKey((byte*) itsKey.data(), itsKeylength);
|
110
|
+
string retval;
|
111
|
+
try {
|
112
|
+
if (hex) {
|
113
|
+
FileSource f(filename.c_str(), true, new HashFilter(*itsHashModule, new HexEncoder(new StringSink(retval), false)));
|
114
|
+
}
|
115
|
+
else {
|
116
|
+
FileSource f(filename.c_str(), true, new HashFilter(*itsHashModule, new StringSink(retval)));
|
117
|
+
}
|
118
|
+
}
|
119
|
+
catch (FileStore::OpenErr e) {
|
120
|
+
throw e;
|
121
|
+
}
|
122
|
+
return retval;
|
123
|
+
}*/
|
124
|
+
|
125
|
+
#endif
|