1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-27 09:18:16 +00:00

hash: cryptopp templatized, added hash_procs

This commit is contained in:
Mirek Kratochvil 2014-03-10 14:54:36 +01:00
parent c70ed00230
commit 61802a9113
3 changed files with 35 additions and 61 deletions

@ -22,25 +22,12 @@
#if HAVE_CRYPTOPP==1
#include "hash.h"
#include "sha_hash.h"
#include <crypto++/ripemd.h>
class rmd128hash : public hash_func
{
public:
uint size() {
return CryptoPP::RIPEMD128::DIGESTSIZE;
}
std::vector<byte> operator() (const std::vector<byte>&a) {
std::vector<byte> r;
r.resize (size() );
CryptoPP::RIPEMD128().CalculateDigest (& (r[0]),
& (a[0]),
a.size() );
return r;
}
};
//it's used just like SHA, so create it from SHA
class rmd128hash : public shahash<CryptoPP::RIPEMD128> {};
class rmd128proc : public shaproc<CryptoPP::RIPEMD128> {};
#endif //HAVE_CRYPTOPP==1

@ -25,56 +25,56 @@
#include <crypto++/sha.h>
class sha256hash : public hash_func
template <class shatype>
class shahash : public hash_func
{
public:
uint size() {
return CryptoPP::SHA256::DIGESTSIZE;
return shatype::DIGESTSIZE;
}
std::vector<byte> operator() (const std::vector<byte>&a) {
std::vector<byte> r;
r.resize (size() );
CryptoPP::SHA256().CalculateDigest (& (r[0]),
& (a[0]),
a.size() );
shatype().CalculateDigest (& (r[0]),
& (a[0]),
a.size() );
return r;
}
};
class sha384hash : public hash_func
class sha256hash : public shahash<CryptoPP::SHA256> {};
class sha384hash : public shahash<CryptoPP::SHA384> {};
class sha512hash : public shahash<CryptoPP::SHA512> {};
template <class shatype>
class shaproc : public hash_proc
{
shatype state;
public:
uint size() {
return CryptoPP::SHA384::DIGESTSIZE;
return shatype::DIGESTSIZE;
}
std::vector<byte> operator() (const std::vector<byte>&a) {
void init() {
state.Restart();
}
void eat (const std::vector<byte>&a) {
state.Update (& (a[0]), a.size() );
}
std::vector<byte> finish() {
std::vector<byte> r;
r.resize (size() );
CryptoPP::SHA384().CalculateDigest (& (r[0]),
& (a[0]),
a.size() );
state.Final (& (r[0]) );
return r;
}
};
class sha512hash : public hash_func
{
public:
uint size() {
return CryptoPP::SHA512::DIGESTSIZE;
}
std::vector<byte> operator() (const std::vector<byte>&a) {
std::vector<byte> r;
r.resize (size() );
CryptoPP::SHA512().CalculateDigest (& (r[0]),
& (a[0]),
a.size() );
return r;
}
};
class sha256proc : public shaproc<CryptoPP::SHA256> {};
class sha384proc : public shaproc<CryptoPP::SHA384> {};
class sha512proc : public shaproc<CryptoPP::SHA512> {};
#endif //HAVE_CRYPTOPP==1

@ -22,25 +22,12 @@
#if HAVE_CRYPTOPP==1
#include "hash.h"
#include "sha_hash.h"
#include <crypto++/tiger.h>
class tiger192hash : public hash_func
{
public:
uint size() {
return CryptoPP::Tiger::DIGESTSIZE;
}
std::vector<byte> operator() (const std::vector<byte>&a) {
std::vector<byte> r;
r.resize (size() );
CryptoPP::Tiger().CalculateDigest (& (r[0]),
& (a[0]),
a.size() );
return r;
}
};
//it's used just like SHA, so create it from SHA
class tiger192hash : public shahash<CryptoPP::Tiger> {};
class tiger192proc : public shaproc<CryptoPP::Tiger> {};
#endif //HAVE_CRYPTOPP==1