From 4da1b8723c1f6fa80345f75135437741cee5f889 Mon Sep 17 00:00:00 2001 From: GabrielTofvesson Date: Tue, 6 Mar 2018 04:13:31 +0100 Subject: [PATCH] Implemented SHA1 --- SHA1/SHA1.cpp | 107 ++++++++++++++++++++++++++++++++++++++ SHA1/SHA1.h | 30 +++++++++++ SHA1/SHA1.vcxproj | 4 ++ SHA1/SHA1.vcxproj.filters | 10 ++++ 4 files changed, 151 insertions(+) create mode 100644 SHA1/SHA1.cpp create mode 100644 SHA1/SHA1.h diff --git a/SHA1/SHA1.cpp b/SHA1/SHA1.cpp new file mode 100644 index 0000000..78d5dee --- /dev/null +++ b/SHA1/SHA1.cpp @@ -0,0 +1,107 @@ +#define SHA1_API +#include "SHA1.h" +#include + +namespace CryptoCPP { + namespace SHA1 { + SHA1_API char * digest(char * data, size_t data_size) + { + // Initialize buffers + unsigned int h0 = 0x67452301; + unsigned int h1 = 0xEFCDAB89; + unsigned int h2 = 0x98BADCFE; + unsigned int h3 = 0x10325476; + unsigned int h4 = 0xC3D2E1F0; + + // Pad message + size_t ml = data_size + 1; + size_t full_size = ml + ((960 - (ml * 8 % 512)) % 512) / 8 + 8; + char * msg = new char[full_size]; + memcpy(msg, data, data_size); + memset(msg + data_size, 0, full_size - data_size); + msg[data_size] = 0x80; + size_t len = data_size * 8; + for (int i = 0; i < 8; ++i) msg[full_size - 1 - i] = (len >> (i * 8)) & 255; + + unsigned int chunks = full_size / 64; + + // Perform hashing for each 512-bit block + for (size_t i = 0; i> (32 - by)); + } + SHA1_API void write_reverse_endian(char* to, unsigned int value, size_t offset) + { + for (size_t t = 0; t < 4; ++t) to[offset + 4 - t] = (value >> (t * 8)) & 255; + } + } +} \ No newline at end of file diff --git a/SHA1/SHA1.h b/SHA1/SHA1.h new file mode 100644 index 0000000..61ccf34 --- /dev/null +++ b/SHA1/SHA1.h @@ -0,0 +1,30 @@ +#pragma once + +#if defined(__MINGW32__) || defined(_WIN32) + +#if defined(SHA1_API) +#undef SHA1_API +#define SHA1_API __declspec(dllexport) +#else +#define SHA1_API __declspec(dllimport) +#endif + +#endif + +#ifndef SHA1_API +#if __GNUC__ >= 4 +#define SHA1_API __attribute__ ((visibility ("default"))) +#else +#define SHA1_API +#endif +#endif + +namespace CryptoCPP { + namespace SHA { + SHA1_API char * digest(char * data, size_t data_size); + SHA1_API unsigned int rot(unsigned int val, size_t by); + SHA1_API unsigned int func(unsigned int t, unsigned int b, unsigned int c, unsigned int d); + SHA1_API unsigned int K(unsigned int t); + SHA1_API void write_reverse_endian(char* to, unsigned int value, size_t offset); + } +} \ No newline at end of file diff --git a/SHA1/SHA1.vcxproj b/SHA1/SHA1.vcxproj index e7e53dc..051156e 100644 --- a/SHA1/SHA1.vcxproj +++ b/SHA1/SHA1.vcxproj @@ -116,6 +116,10 @@ + + + + diff --git a/SHA1/SHA1.vcxproj.filters b/SHA1/SHA1.vcxproj.filters index 6a1782f..f7cac3d 100644 --- a/SHA1/SHA1.vcxproj.filters +++ b/SHA1/SHA1.vcxproj.filters @@ -14,4 +14,14 @@ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + Header Files + + + + + Source Files + + \ No newline at end of file