添加项目文件。
This commit is contained in:
@@ -0,0 +1,394 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "UOHash.h"
|
||||
#include <stdarg.h>
|
||||
#include "URLCodec.h"
|
||||
#include "basecodec.hpp"
|
||||
#include "base36_codec.hpp"
|
||||
#include <rhash_torrent.h>
|
||||
#include <cryptopp/shake.h>
|
||||
#include <cryptopp/base32.h>
|
||||
#include <cryptopp/base64.h>
|
||||
#include <cryptopp/basecode.h>
|
||||
|
||||
namespace uns
|
||||
{
|
||||
bool UOHash::FileExist(std::string file)
|
||||
{
|
||||
std::fstream fs(file, std::ios::in);
|
||||
if (fs.is_open())
|
||||
{
|
||||
fs.close();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
int UOHash::CalculateHashSize(HashID HashID)
|
||||
{
|
||||
switch (HashID)
|
||||
{
|
||||
case HashID::CRC32: return 8;
|
||||
case HashID::MD4: return 32;
|
||||
case HashID::MD5: return 32;
|
||||
case HashID::SHA1: return 40;
|
||||
case HashID::TIGER: return 48;
|
||||
case HashID::TTH: return 39;
|
||||
case HashID::BTIH: return 40;
|
||||
case HashID::ED2K: return 32;
|
||||
case HashID::AICH: return 32;
|
||||
case HashID::WHIRLPOOL: return 128;
|
||||
case HashID::RIPEMD160: return 40;
|
||||
case HashID::GOST94: return 64;
|
||||
case HashID::GOST94_CRYPTOPRO: return 64;
|
||||
case HashID::HAS160: return 40;
|
||||
case HashID::GOST12_256: return 64;
|
||||
case HashID::GOST12_512: return 128;
|
||||
case HashID::SHA224: return 56;
|
||||
case HashID::SHA256: return 64;
|
||||
case HashID::SHA384: return 96;
|
||||
case HashID::SHA512: return 128;
|
||||
case HashID::EDONR256: return 64;
|
||||
case HashID::EDONR512: return 128;
|
||||
case HashID::SHA3_224: return 56;
|
||||
case HashID::SHA3_256: return 64;
|
||||
case HashID::SHA3_384: return 96;
|
||||
case HashID::SHA3_512: return 128;
|
||||
case HashID::CRC32C: return 8;
|
||||
case HashID::SNEFRU128: return 32;
|
||||
case HashID::SNEFRU256: return 64;
|
||||
case HashID::BLAKE2S: return 64;
|
||||
case HashID::BLAKE2B: return 128;
|
||||
default: return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string UOHash::RemoveWhiteChar(std::string str)
|
||||
{
|
||||
std::string ret;
|
||||
for (auto& ele : str)
|
||||
if ((ele != ' ') && (ele != '\n') && (ele != '\t'))
|
||||
ret.push_back(ele);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int UOHash::CalculateHashExSize(HashID HashID, int output_bits)
|
||||
{
|
||||
if (HashID == HashID::SHAKE128)
|
||||
return 64;
|
||||
else if (HashID == HashID::SHAKE256)
|
||||
return 128;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
rhash_print_sum_flags UOHash::PickOutputType(HashID HashID)
|
||||
{
|
||||
if ((HashID == HashID::TTH) || (HashID == HashID::AICH))
|
||||
return RHPR_BASE32;
|
||||
else
|
||||
return RHPR_HEX;
|
||||
}
|
||||
|
||||
HashResult UOHash::DecodeString(HashID HashID, std::string Source)
|
||||
{
|
||||
if (HashID < HashID::BASE16)
|
||||
return HashResult::Faliure(INVALID_HASH_FUNCTION);
|
||||
else
|
||||
{
|
||||
switch (HashID)
|
||||
{
|
||||
case HashID::BASE16:
|
||||
{
|
||||
std::string output;
|
||||
if (basecodec::decodeBase16(Source, output))
|
||||
return HashResult::Success(output);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
case HashID::BASE32:
|
||||
{
|
||||
std::string ret = "";
|
||||
CryptoPP::StringSource source(Source, true, new CryptoPP::Base32Decoder(new CryptoPP::StringSink(ret)));
|
||||
if (ret == "")
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
else
|
||||
return HashResult::Success(ret);
|
||||
}
|
||||
case HashID::BASE32_HEX:
|
||||
{
|
||||
std::string ret = "";
|
||||
CryptoPP::StringSource source(Source, true, new CryptoPP::Base32HexDecoder(new CryptoPP::StringSink(ret)));
|
||||
if (ret == "")
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
else
|
||||
return HashResult::Success(ret);
|
||||
}
|
||||
case HashID::BASE36:
|
||||
{
|
||||
std::string out;
|
||||
if (basecodec::decodeBase36ToDecimalString(Source, out))
|
||||
return HashResult::Success(out);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
case HashID::BASE62:
|
||||
{
|
||||
std::string out;
|
||||
if (basecodec::decodeBase62(Source, out))
|
||||
return HashResult::Success(out);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
case HashID::BASE58:
|
||||
{
|
||||
std::string out;
|
||||
if (basecodec::decodeBase58(Source, out))
|
||||
return HashResult::Success(out);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
case HashID::BASE64:
|
||||
{
|
||||
std::string ret = "";
|
||||
CryptoPP::StringSource source(Source, true, new CryptoPP::Base64Decoder(new CryptoPP::StringSink(ret)));
|
||||
if (ret == "")
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
else
|
||||
return HashResult::Success(ret);
|
||||
}
|
||||
case HashID::BASE64_URL:
|
||||
{
|
||||
std::string ret = "";
|
||||
CryptoPP::StringSource source(Source, true, new CryptoPP::Base64URLDecoder(new CryptoPP::StringSink(ret)));
|
||||
if (ret == "")
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
else
|
||||
return HashResult::Success(ret);
|
||||
}
|
||||
case HashID::BASE85:
|
||||
{
|
||||
std::string output;
|
||||
if (basecodec::decodeBase85(Source, output))
|
||||
return HashResult::Success(output);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
case HashID::BASE91:
|
||||
{
|
||||
std::string output;
|
||||
if (basecodec::decodeBase91(Source, output))
|
||||
return HashResult::Success(output);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
case HashID::URL:
|
||||
{
|
||||
std::string out;
|
||||
if (urlcodec::url_decode(Source, out))
|
||||
return HashResult::Success(out);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
default:
|
||||
return HashResult::Faliure(INVALID_HASH_FUNCTION);
|
||||
}
|
||||
return HashResult::Faliure(INVALID_HASH_FUNCTION);
|
||||
}
|
||||
}
|
||||
|
||||
HashResult UOHash::HashString(HashID HashID, std::string Source, int ex_output_bits)
|
||||
{
|
||||
if (HashID >= HashID::__RHASH_MAX)
|
||||
{
|
||||
switch (HashID)
|
||||
{
|
||||
case HashID::SHAKE128:
|
||||
{
|
||||
if (ex_output_bits <= 0)
|
||||
return HashResult::Faliure(INVALID_HASHEX_ARGUMENT);
|
||||
CryptoPP::SHAKE128 shake128(ex_output_bits);
|
||||
int hash_size = CalculateHashExSize(HashID, ex_output_bits);
|
||||
unsigned char* recv = new unsigned char[ex_output_bits + 10];
|
||||
char* hashstr = new char[hash_size + 10];
|
||||
memset(recv, 0, (ex_output_bits + 10));
|
||||
memset(hashstr, 0, (hash_size + 10));
|
||||
shake128.CalculateDigest(recv, (const unsigned char*)Source.data(), Source.size());
|
||||
rhash_print_bytes(hashstr, recv, (hash_size / 2), (RHPR_HEX | RHPR_UPPERCASE));
|
||||
std::string str(hashstr);
|
||||
delete[] recv;
|
||||
recv = nullptr;
|
||||
delete[] hashstr;
|
||||
hashstr = nullptr;
|
||||
return HashResult::Success(RemoveWhiteChar(str));
|
||||
}
|
||||
case HashID::SHAKE256:
|
||||
{
|
||||
if (ex_output_bits <= 0)
|
||||
return HashResult::Faliure(INVALID_HASHEX_ARGUMENT);
|
||||
CryptoPP::SHAKE256 shake256(ex_output_bits);
|
||||
int hash_size = CalculateHashExSize(HashID, ex_output_bits);
|
||||
unsigned char* recv = new unsigned char[ex_output_bits + 10];
|
||||
char* hashstr = new char[hash_size + 10];
|
||||
memset(recv, 0, (ex_output_bits + 10));
|
||||
memset(hashstr, 0, (hash_size + 10));
|
||||
shake256.CalculateDigest(recv, (const unsigned char*)Source.data(), Source.size());
|
||||
rhash_print_bytes(hashstr, recv, (hash_size / 2), (RHPR_HEX | RHPR_UPPERCASE));
|
||||
std::string str(hashstr);
|
||||
delete[] recv;
|
||||
recv = nullptr;
|
||||
delete[] hashstr;
|
||||
hashstr = nullptr;
|
||||
return HashResult::Success(RemoveWhiteChar(str));
|
||||
}
|
||||
case HashID::BASE16:
|
||||
return HashResult::Success(basecodec::encodeBase16(Source));
|
||||
case HashID::BASE32:
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string ret = "";
|
||||
CryptoPP::Base32Encoder b32enc;
|
||||
b32enc.Detach(new CryptoPP::StringSink(ret));
|
||||
b32enc.Put(reinterpret_cast<CryptoPP::byte*>(&Source[0]), Source.size());
|
||||
if (ret == "")
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
else
|
||||
return HashResult::Success(RemoveWhiteChar(ret));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HashResult::Faliure(HASH_LIBRARY_ERROR);
|
||||
}
|
||||
}
|
||||
case HashID::BASE32_HEX:
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string ret = "";
|
||||
CryptoPP::Base32HexEncoder b32enc_hex;
|
||||
b32enc_hex.Detach(new CryptoPP::StringSink(ret));
|
||||
b32enc_hex.Put(reinterpret_cast<CryptoPP::byte*>(&Source[0]), Source.size());
|
||||
if (ret == "")
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
else
|
||||
return HashResult::Success(RemoveWhiteChar(ret));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HashResult::Faliure(HASH_LIBRARY_ERROR);
|
||||
}
|
||||
}
|
||||
case HashID::BASE36:
|
||||
{
|
||||
std::string out;
|
||||
if (basecodec::encodeBase36FromDecimalString(Source, out))
|
||||
return HashResult::Success(out);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
case HashID::BASE58:
|
||||
return HashResult::Success(basecodec::encodeBase58(Source));
|
||||
case HashID::BASE62:
|
||||
return HashResult::Success(basecodec::encodeBase62(Source));
|
||||
case HashID::BASE64:
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string ret = "";
|
||||
CryptoPP::Base64Encoder b64enc;
|
||||
b64enc.Detach(new CryptoPP::StringSink(ret));
|
||||
b64enc.Put(reinterpret_cast<CryptoPP::byte*>(&Source[0]), Source.size());
|
||||
if (ret == "")
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
else
|
||||
return HashResult::Success(RemoveWhiteChar(ret));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HashResult::Faliure(HASH_LIBRARY_ERROR);
|
||||
}
|
||||
}
|
||||
case HashID::BASE64_URL:
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string ret = "";
|
||||
CryptoPP::Base64URLEncoder b64enc_url;
|
||||
b64enc_url.Detach(new CryptoPP::StringSink(ret));
|
||||
b64enc_url.Put(reinterpret_cast<CryptoPP::byte*>(&Source[0]), Source.size());
|
||||
if (ret == "")
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
else
|
||||
return HashResult::Success(RemoveWhiteChar(ret));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HashResult::Faliure(HASH_LIBRARY_ERROR);
|
||||
}
|
||||
}
|
||||
case HashID::BASE85:
|
||||
return HashResult::Success(basecodec::encodeBase85(Source));
|
||||
case HashID::BASE91:
|
||||
{
|
||||
std::string out;
|
||||
if (basecodec::encodeBase91(Source, out))
|
||||
return HashResult::Success(out);
|
||||
else
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
}
|
||||
case HashID::URL:
|
||||
return HashResult::Success(urlcodec::url_encode(Source));
|
||||
default:
|
||||
return HashResult::Faliure(INVALID_HASH_FUNCTION);
|
||||
}
|
||||
return HashResult::Faliure(INVALID_HASH_FUNCTION);
|
||||
}
|
||||
else
|
||||
{
|
||||
int HashSize = CalculateHashSize(HashID);
|
||||
if (HashSize < 8)
|
||||
return HashResult::Faliure(INVALID_HASH_FUNCTION);
|
||||
char* src = new char[Source.length() + 1];
|
||||
unsigned char* dst = new unsigned char[(HashSize * 4) + 1];
|
||||
char* str = new char[HashSize + 1];
|
||||
strcpy(src, Source.c_str());
|
||||
memset(dst, 0, sizeof(dst));
|
||||
memset(str, 0, sizeof(str));
|
||||
rhash_library_init();
|
||||
int res = rhash_msg((int)HashID, src, strlen(src), dst);
|
||||
if (res < 0)
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
rhash_print_bytes(str, dst, rhash_get_digest_size((int)HashID), (PickOutputType(HashID) | RHPR_UPPERCASE));
|
||||
std::string ret = std::string(str);
|
||||
delete[] src;
|
||||
delete[] dst;
|
||||
delete[] str;
|
||||
return HashResult((ret.length() >= 8), RemoveWhiteChar(ret), HASH_LIBRARY_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
HashResult UOHash::HashFile(HashID HashID, std::string File, int ex_output_bits)
|
||||
{
|
||||
if (HashID >= HashID::__RHASH_MAX)
|
||||
return HashResult::Faliure(INVALID_HASH_FUNCTION);
|
||||
if (!FileExist(File))
|
||||
return HashResult::Faliure(FILE_DOES_NOT_EXIST);
|
||||
int HashSize = CalculateHashSize(HashID);
|
||||
if (HashSize < 8)
|
||||
return HashResult::Faliure(INVALID_HASH_FUNCTION);
|
||||
unsigned char* dst = new unsigned char[(HashSize * 4) + 1];
|
||||
char* str = new char[HashSize + 1];
|
||||
rhash_library_init();
|
||||
int res = rhash_file((int)HashID, File.c_str(), dst);
|
||||
if (res < 0)
|
||||
return HashResult::Faliure(HASH_CALCULATION_ERROR);
|
||||
rhash_print_bytes(str, dst, rhash_get_digest_size((int)HashID), (PickOutputType(HashID) | RHPR_UPPERCASE));
|
||||
std::string ret = std::string(str);
|
||||
delete[] dst;
|
||||
delete[] str;
|
||||
return HashResult((ret.length() >= 8), RemoveWhiteChar(ret), HASH_LIBRARY_ERROR);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user