Our Hash code in C++.
Hash Class Header File:
#pragma once
#include "BigInteger.h"
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
class Hashes
{
public:
Hashes();
virtual ~Hashes();
string hashWord(vector<char>);
};
Hash Class File:
#include "stdafx.h"
#include "Hashes.h"
Hashes::Hashes()
{
}
string Hashes::hashWord(vector<char> msg) {
// encoding eqn { Tn = (n-2)t1 + 2^n } - please use your own eqn
string hash = "", t1, x;
int n;
for (size_t i = 0; i < msg.size(); i++) {
// get unicode of this character as n
n = (int)msg[i];
t1 = to_string(i + 1);
// use recurrence series equation to hash
x = BigInteger::addition(BigInteger::multiplication(to_string(n-2), t1), BigInteger::exponentialPower("2", n));
if (i == 0) {
hash = x;
continue;
}
// bitwise rotate left with the modulo of x
string binary = BigInteger::decToBin(hash);
x = BigInteger::modulus(x, to_string(binary.size()));
string slice_1 = binary.substr(stoi(x));
// keep as '1' to preserve hash size
slice_1[0] = '1';
string slice_2 = binary.substr(0, stoi(x));
hash = slice_1 + slice_2;
hash = BigInteger::binToDec(hash);
}
hash = BigInteger::decToHex(hash);
transform(hash.begin(), hash.end(), hash.begin(), ::toupper);
return hash;
}
Hashes::~Hashes()
{
}
Main Class:
#include "stdafx.h"
#include "Hashes.h"
#include <iostream>
using namespace std;
int main()
{
char message[] = "merry xmas";
unsigned i = 0;
vector<char> msg = {};
while (message[i] != '\0') {
msg.push_back(message[i++]);
}
msg.shrink_to_fit();
Hashes one_way;
string hashed = one_way.hashWord(msg);
cout << "Message is '" << string(message) << "';\nMessage hash is " << hashed << endl;
return 0;
}
Try it out!
Elegance (0.0)