Skip to content

Commit

Permalink
- Added SHA3 function (Keccak-512)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mellnik committed Mar 27, 2014
1 parent 99fa447 commit 81a0f4c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hash.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define SHA256_LENGTH 64
#define SHA384_LENGTH 96
#define SHA512_LENGTH 128
#define SHA3_LENGTH 128
#define WHIRLPOOL_LENGTH 128
#define RIPEMD160_LENGTH 40
#define RIPEMD256_LENGTH 64
Expand All @@ -36,6 +37,7 @@
native sha256(const key[], hash[], len = sizeof(hash));
native sha384(const key[], hash[], len = sizeof(hash));
native sha512(const key[], hash[], len = sizeof(hash));
native sha3(const key[], hash[], len = sizeof(hash));
native whirlpool(const key[], hash[], len = sizeof(hash));
native ripemd160(const key[], hash[], len = sizeof(hash));
native ripemd256(const key[], hash[], len = sizeof(hash));
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ AMX_NATIVE_INFO hash_natives[] =
{"sha256", Native::sha256},
{"sha384", Native::sha384},
{"sha512", Native::sha512},
{"sha3", Native::sha3},
{"whirlpool", Native::whirlpool},
{"ripemd160", Native::ripemd160},
{"ripemd256", Native::ripemd256},
Expand Down
16 changes: 16 additions & 0 deletions src/natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ cell AMX_NATIVE_CALL Native::sha512(AMX *amx, cell *params)
return 1;
}

cell AMX_NATIVE_CALL Native::sha3(AMX *amx, cell *params)
{
PARAM_CHECK(3, "sha3");

char *str = NULL;
amx_StrParam(amx, params[1], str);

std::string hash;
Utility::sha3(str, hash);

cell *amx_Addr = NULL;
amx_GetAddr(amx, params[2], &amx_Addr);
amx_SetString(amx_Addr, hash.c_str(), 0, 0, params[3]);
return 1;
}

cell AMX_NATIVE_CALL Native::whirlpool(AMX *amx, cell *params)
{
PARAM_CHECK(3, "whirlpool");
Expand Down
1 change: 1 addition & 0 deletions src/natives.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Native
cell AMX_NATIVE_CALL sha256(AMX *amx, cell *params);
cell AMX_NATIVE_CALL sha384(AMX *amx, cell *params);
cell AMX_NATIVE_CALL sha512(AMX *amx, cell *params);
cell AMX_NATIVE_CALL sha3(AMX *amx, cell *params);
cell AMX_NATIVE_CALL whirlpool(AMX *amx, cell *params);
cell AMX_NATIVE_CALL ripemd160(AMX *amx, cell *params);
cell AMX_NATIVE_CALL ripemd256(AMX *amx, cell *params);
Expand Down
7 changes: 7 additions & 0 deletions src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <cryptopp/base64.h>
#include <cryptopp/hex.h>
#include <cryptopp/sha.h>
#include <cryptopp/sha3.h>
#include <cryptopp/osrng.h>
#include <cryptopp/ripemd.h>
#include <cryptopp/whrlpool.h>
Expand Down Expand Up @@ -66,6 +67,12 @@ void Utility::sha512(std::string input, std::string &output)
CryptoPP::StringSource(input, true, new CryptoPP::HashFilter(h_sha512, new CryptoPP::HexEncoder(new CryptoPP::StringSink(output))));
}

void Utility::sha3(std::string input, std::string &output)
{
CryptoPP::SHA3_512 h_sha3;
CryptoPP::StringSource(input, true, new CryptoPP::HashFilter(h_sha3, new CryptoPP::HexEncoder(new CryptoPP::StringSink(output))));
}

void Utility::whirlpool(std::string input, std::string &output)
{
CryptoPP::Whirlpool h_Whirlpool;
Expand Down
1 change: 1 addition & 0 deletions src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Utility
void sha256(std::string input, std::string &output);
void sha384(std::string input, std::string &output);
void sha512(std::string input, std::string &output);
void sha3(std::string input, std::string &output);
void whirlpool(std::string input, std::string &output);
void ripemd160(std::string input, std::string &output);
void ripemd256(std::string input, std::string &output);
Expand Down

0 comments on commit 81a0f4c

Please sign in to comment.