1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-25 00:08:20 +00:00
codecrypt/src/cube_hash.h

111 lines
2.8 KiB
C
Raw Normal View History

2013-09-29 17:52:46 +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>
*
2013-09-29 17:52:46 +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/>.
*/
#include "cubehash_impl.h"
#include "hash.h"
template<int I, int R, int B, int F, int H>
class cubehash : public hash_func
{
public:
uint size() {
return H;
}
std::vector<byte> operator() (const std::vector<byte>&a) {
cubehash_state<I, R, B, F, H> state;
size_t i;
state.init();
for (i = 0; i + B <= a.size(); i += B)
2015-10-31 21:58:17 +00:00
state.process_block (& (a[i]));
2013-09-29 17:52:46 +00:00
if (a.size() - i != 0)
state.process_final_incomplete_block (& (a[i]), a.size() - i);
else
state.process_final_incomplete_block (NULL, 0); //empty block, just finalize
2013-09-29 17:52:46 +00:00
std::vector<byte> result;
result.resize (H, 0);
2015-10-31 21:58:17 +00:00
state.get_hash (& (result[0]));
2013-09-29 17:52:46 +00:00
return result;
}
};
2014-03-10 12:57:25 +00:00
template<int I, int R, int B, int F, int H>
class cubehashproc : public hash_proc
{
cubehash_state<I, R, B, F, H> state;
byte buf[B];
int bpos;
public:
uint size() {
return H;
}
void init() {
state.init();
bpos = 0;
}
2014-04-06 11:37:26 +00:00
void eat (const byte*a, const byte*aend) {
2014-03-10 12:57:25 +00:00
int apos = 0;
2014-04-06 11:37:26 +00:00
int asize = aend - a;
2014-03-10 12:57:25 +00:00
if (bpos) {
2014-04-06 11:37:26 +00:00
for (; bpos < B && apos < asize; ++bpos, ++apos)
2014-03-10 12:57:25 +00:00
buf[bpos] = a[apos];
if (bpos == B) {
state.process_block (buf);
bpos = 0;
}
}
2015-10-31 21:58:17 +00:00
while (apos + B <= asize) {
state.process_block (& (a[apos]));
2014-03-10 12:57:25 +00:00
apos += B;
}
2014-04-06 11:37:26 +00:00
for (; apos < asize; ++apos, ++bpos)
2014-03-10 12:57:25 +00:00
buf[bpos] = a[apos];
}
std::vector<byte> finish() {
state.process_final_incomplete_block (buf, bpos);
std::vector<byte> result;
result.resize (H, 0);
2015-10-31 21:58:17 +00:00
state.get_hash (& (result[0]));
2014-03-10 12:57:25 +00:00
return result;
}
};
2013-09-29 17:52:46 +00:00
class cube128hash : public cubehash<16, 16, 32, 32, 16> {};
class cube192hash : public cubehash<16, 16, 32, 32, 24> {};
class cube256hash : public cubehash<16, 16, 32, 32, 32> {};
class cube384hash : public cubehash<16, 16, 32, 32, 48> {};
class cube512hash : public cubehash<16, 16, 32, 32, 64> {};
2014-03-10 12:57:25 +00:00
class cube128proc : public cubehashproc<16, 16, 32, 32, 16> {};
class cube192proc : public cubehashproc<16, 16, 32, 32, 24> {};
class cube256proc : public cubehashproc<16, 16, 32, 32, 32> {};
class cube384proc : public cubehashproc<16, 16, 32, 32, 48> {};
class cube512proc : public cubehashproc<16, 16, 32, 32, 64> {};