1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-07-01 03:13:11 +00:00
codecrypt/src/sha_hash.h

87 lines
2.0 KiB
C
Raw Normal View History

2012-12-29 19:35:42 +00:00
/*
* This file is part of Codecrypt.
*
2016-04-17 13:47:47 +00:00
* Copyright (C) 2013-2016 Mirek Kratochvil <exa.exa@gmail.com>
*
2012-12-29 19:35:42 +00:00
* Codecrypt is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Codecrypt is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Codecrypt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _ccr_sha_hash_h_
#define _ccr_sha_hash_h_
2012-12-29 19:35:42 +00:00
#if HAVE_CRYPTOPP==1
2012-12-29 19:35:42 +00:00
#include "hash.h"
#if CRYPTOPP_DIR_PLUS
# include <crypto++/sha.h>
#else
# include <cryptopp/sha.h>
#endif
2012-12-29 19:35:42 +00:00
template <class shatype>
class shahash : public hash_func
2012-12-29 19:35:42 +00:00
{
public:
uint size() {
return shatype::DIGESTSIZE;
2012-12-29 19:35:42 +00:00
}
std::vector<byte> operator() (const std::vector<byte>&a) {
std::vector<byte> r;
2015-10-31 21:58:17 +00:00
r.resize (size());
shatype().CalculateDigest (& (r[0]),
& (a[0]),
2015-10-31 21:58:17 +00:00
a.size());
2012-12-29 19:35:42 +00:00
return r;
}
};
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
2013-05-01 13:49:40 +00:00
{
shatype state;
2013-05-01 13:49:40 +00:00
public:
uint size() {
return shatype::DIGESTSIZE;
2013-05-01 13:49:40 +00:00
}
void init() {
state.Restart();
2013-05-01 13:49:40 +00:00
}
2014-04-06 11:37:26 +00:00
void eat (const byte*a, const byte*aend) {
state.Update (a, aend - a);
2012-12-29 19:35:42 +00:00
}
std::vector<byte> finish() {
2012-12-29 19:35:42 +00:00
std::vector<byte> r;
2015-10-31 21:58:17 +00:00
r.resize (size());
state.Final (& (r[0]));
2012-12-29 19:35:42 +00:00
return r;
}
};
class sha256proc : public shaproc<CryptoPP::SHA256> {};
class sha384proc : public shaproc<CryptoPP::SHA384> {};
class sha512proc : public shaproc<CryptoPP::SHA512> {};
#endif //HAVE_CRYPTOPP==1
2012-12-29 19:35:42 +00:00
#endif