Reverted digest

This commit is contained in:
WolverinDEV 2019-07-07 18:05:22 +02:00
parent cf40917d60
commit 22fd9382f0
2 changed files with 43 additions and 39 deletions

View File

@ -3,16 +3,12 @@
#include <tomcrypt.h> #include <tomcrypt.h>
#define DECLARE_DIGEST(name, _unused_, digestLength) \ #define DECLARE_DIGEST(name, _unused_, digestLength) \
std::string digest::tomcrypt::name(const std::string& input) { \ void digest::tomcrypt::name(const char* input, size_t length, uint8_t(& result)[digestLength]) { \
hash_state hash{}; \ hash_state hash{}; \
\ \
uint8_t buffer[digestLength]; \
\
name ##_init(&hash); \ name ##_init(&hash); \
name ##_process(&hash, (uint8_t*) input.data(), input.length()); \ name ##_process(&hash, (uint8_t*) input, length); \
name ##_done(&hash, buffer); \ name ##_done(&hash, result); \
\
return std::string((const char*) buffer, digestLength); \
} }
DECLARE_DIGEST(sha1, SHA1, SHA_DIGEST_LENGTH) DECLARE_DIGEST(sha1, SHA1, SHA_DIGEST_LENGTH)

View File

@ -5,22 +5,30 @@
#include <cstring> #include <cstring>
#ifdef NO_OPEN_SSL #ifdef NO_OPEN_SSL
#define SHA_DIGEST_LENGTH 20 #define SHA_DIGEST_LENGTH (20)
#define SHA256_DIGEST_LENGTH 32 #define SHA256_DIGEST_LENGTH (32)
#define SHA512_DIGEST_LENGTH 64 #define SHA512_DIGEST_LENGTH (64)
#define DECLARE_DIGEST(name, _unused_, digestLength) \ #define DECLARE_DIGEST(name, _unused_, digestLength) \
namespace tomcrypt { \ namespace tomcrypt { \
extern std::string name(const std::string&); \ extern void name(const char* input, size_t length, uint8_t(& result)[digestLength]); \
} \ } \
inline std::string name(const std::string& input) { \ inline std::string name(const std::string& input) { \
return tomcrypt::name(input); \ uint8_t result[digestLength]; \
tomcrypt::name(input.data(), input.length(), result); \
return std::string((const char*) result, (size_t) digestLength); \
} \ } \
\ \
inline std::string name(const char* input, int64_t length = -1) { \ inline std::string __ ##name(const char* input, int64_t length = -1) { \
if(length == -1) length = strlen(input); \ if(length == -1) length = strlen(input); \
return name(std::string{input, (size_t) length}); \ uint8_t result[digestLength]; \
tomcrypt::name(input, length, result); \
return std::string((const char*) result, (size_t) digestLength); \
} \ } \
\
inline void name(const char* input, size_t length, uint8_t(& result)[digestLength]) { \
tomcrypt::name(input, length, result); \
}
#else #else
#include <openssl/sha.h> #include <openssl/sha.h>
@ -29,12 +37,12 @@
inline std::string name(const std::string& input) { \ inline std::string name(const std::string& input) { \
u_char buffer[digestLength]; \ u_char buffer[digestLength]; \
method((u_char*) input.data(), input.length(), buffer); \ method((u_char*) input.data(), input.length(), buffer); \
return std::string((const char*) buffer, digestLength); \ return std::string((const char*) buffer, (size_t) digestLength); \
} \ } \
\ \
inline std::string name(const char* input, ssize_t length = -1) { \ inline std::string name(const char* input, ssize_t length = -1) { \
if(length == -1) length = strlen(input); \ if(length == -1) length = strlen(input); \
return name(std::string(input, length)); \ return name(std::string(input, (size_t) length)); \
} \ } \
\ \
inline void name(const char* input, size_t length, uint8_t(& result)[digestLength]) { \ inline void name(const char* input, size_t length, uint8_t(& result)[digestLength]) { \