Files
GCrypt/GCryptLib/src/GHash.cpp
T

69 lines
1.8 KiB
C++
Raw Normal View History

2022-05-22 12:55:39 +02:00
#include "GCrypt/GHash.h"
#include "GCrypt/Util.h"
#include "GCrypt/InitializationVector.h"
#include <vector>
namespace Leonetienne::GCrypt {
2022-05-22 12:55:39 +02:00
GHash::GHash() :
// Initialize our cipher with a static, but randomly distributed key.
cipher(
2022-05-22 20:13:41 +02:00
// The key really does not matter, as it gets changed
// each time before digesting anything.
Key(StringToBitblock("nsoCZfvdqpRkeVTt9wzvPR3TT26peOW9E2kTHh3pdPCq2M7BpskvUljJHSrobUTI")),
2022-05-22 12:51:58 +02:00
GCipher::DIRECTION::ENCIPHER
) {
block = InitializationVector(StringToBitblock("3J7IipfQTDJbO8jtasz9PgWui6faPaEMOuVuAqyhB1S2CRcLw5caawewgDUEG1WN"));
return;
}
2022-05-22 12:51:58 +02:00
2022-05-22 12:55:39 +02:00
void GHash::DigestBlock(const Block& data) {
2022-05-22 20:13:41 +02:00
// Set the cipher key to the current data to be hashed
cipher.SetKey(Key(data));
// Encipher the current block, and xor it on the current hashsum
block ^= cipher.Digest(data);
return;
}
2022-05-22 12:55:39 +02:00
const Block& GHash::GetHashsum() const {
return block;
}
2022-05-22 12:55:39 +02:00
Block GHash::CalculateHashsum(const Flexblock& data) {
// Split input into blocks
std::vector<Block> blocks;
for (std::size_t i = 0; i < data.size(); i += Block::BLOCK_SIZE_BITS) {
blocks.push_back(Block(
PadStringToLength(data.substr(i, Block::BLOCK_SIZE_BITS), Block::BLOCK_SIZE_BITS, '0', false))
);
}
// Add an additional block, containing the length of the input
std::stringstream ss;
ss << data.length();
const Block lengthBlock = StringToBitblock(ss.str());
blocks.push_back(lengthBlock);
// Create hasher instance
2022-05-22 12:55:39 +02:00
GHash hasher;
// Digest all blocks
for (Block& block : blocks) {
2022-05-22 12:51:58 +02:00
hasher.DigestBlock(block);
}
// Return the total hashsum
return hasher.GetHashsum();
}
2022-05-22 16:54:40 +02:00
void GHash::operator=(const GHash& other) {
cipher = other.cipher;
return;
}
}