Modify sha1 method

This commit is contained in:
antao 2018-09-15 21:43:38 +08:00
parent 8444c58e64
commit 1f5ab069f9
7 changed files with 161 additions and 48 deletions

View File

@ -14,6 +14,7 @@
#pragma once
#include <drogon/config.h>
#include <drogon/utils/Utilities.h>
#include <drogon/HttpApiBinder.h>
#include <trantor/utils/NonCopyable.h>

View File

@ -34,11 +34,6 @@ namespace drogon{
char *data, size_t *ndata);
std::string getHttpFullDate(const trantor::Date &date);
#ifndef USE_OPENSSL
#define SHA_DIGEST_LENGTH 20
void SHA1(const unsigned char *str, size_t length, unsigned char const * sha1);
#endif
}

View File

@ -14,7 +14,6 @@
#include "HttpAppFrameworkImpl.h"
#include "ConfigLoader.h"
#include "HttpServer.h"
#include <drogon/utils/Utilities.h>
#include <drogon/DrClassMap.h>
#include <drogon/HttpRequest.h>
@ -22,7 +21,11 @@
#include <drogon/CacheMap.h>
#include <drogon/Session.h>
#include <trantor/utils/AsyncFileLogger.h>
#ifdef USE_OPENSSL
#include <openssl/sha.h>
#else
#include "Sha1.h"
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

View File

@ -21,7 +21,7 @@
#include <drogon/HttpAppFramework.h>
#include <drogon/HttpSimpleController.h>
#include <drogon/version.h>
#include <openssl/sha.h>
#include <string>
#include <vector>
#include <memory>

143
lib/src/Sha1.cc Normal file
View File

@ -0,0 +1,143 @@
#include "Sha1.h"
#include <string.h>
static unsigned int FromBigEndian(unsigned int v)
{
return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) |
((v & 0xff000000) >> 24);
}
static void WriteBigEndian64(unsigned char *p, unsigned int v)
{
unsigned char t;
memset(p, 0, 8);
memcpy(p, &v, 4);
int i=0;
for(i = 0; i < 4; i++)
{
t = p[i];
p[i] = p[7 - i];
p[7 - i] = t;
}
}
static unsigned int LeftRol(unsigned int v, int n)
{
return (v << n) | (v >> (32 - n));
}
unsigned char * SHA1(const unsigned char* data, size_t dataLen, unsigned char* md)
{
unsigned char* pbytes = (unsigned char*)data;
unsigned int nbyte = dataLen;
static unsigned int words[80];
unsigned int H[5] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0};
unsigned int a, b, c, d, e, f, k, temp, bitlen[2], word;
unsigned int i, j, index, p1, p2, maxlen;
unsigned char spec[4] = {0};
i = nbyte % 4;
p1 = nbyte - i;
spec[i] = 1 << 7;
while (i--)
{
spec[i] = pbytes[p1 + i];
}
maxlen = (nbyte + 1) % 64;
if(maxlen <= 56)
{
maxlen = (nbyte + 1) - maxlen + 64;
}
else
{
maxlen = (nbyte + 1) - maxlen + 128;
}
p2= maxlen - 8;
WriteBigEndian64((unsigned char*)bitlen, nbyte * 8);
for(j = 0; j < maxlen; j += 64)
{
a = H[0];
b = H[1];
c = H[2];
d = H[3];
e = H[4];
for(i = 0; i < 80; i++)
{
if(i < 16)
{
index = j + (i << 2);
if(index < p1)
{
word = *((unsigned int*)(pbytes + index));
}
else if(index == p1)
{
word = *(unsigned int*)spec;
}
else if(index < p2)
{
word = 0;
}
else
{
word = (index < maxlen - 4) ? bitlen[0] : bitlen[1];
}
words[i] = FromBigEndian(word);
}
else
{
words[i] = LeftRol(words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16], 1);
}
if(i < 20)
{
f = (b & c) | ((~b) & d);
k = 0x5A827999;
}
else if (i < 40)
{
f=b ^ c ^ d;
k = 0x6ED9EBA1;
}
else if (i < 60)
{
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
}
else
{
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
temp = LeftRol(a, 5) + f + e + k + words[i];
e = d;
d = c;
c = LeftRol(b, 30);
b = a;
a = temp;
}
H[0] += a;
H[1] += b;
H[2] += c;
H[3] += d;
H[4] += e;
}
int ct = 0;
for (i = 0; i < 5; i++)
{
unsigned char buf[4] = {0};
memcpy(buf, &(H[i]), 4);
for (int r = 3; r >= 0; r--)
{
md[ct] = buf[r];
ct++;
}
}
return md;
}

11
lib/src/Sha1.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <iostream>
#define SHA_DIGEST_LENGTH 20
unsigned char * SHA1(const unsigned char *pIn, size_t inLen, unsigned char *pOut);

View File

@ -312,44 +312,4 @@ namespace drogon{
return date.toCustomedFormattedString("%a, %d %b %Y %T GMT");
}
#ifndef USE_OPENSSL
#define SHA1_F(B,C,D,t) ((t<40)?((t<20)?((B&C)|((~B)&D)):(B^C^D)):((t<60)?((B&C)|(B&D)|(C&D)):(B^C^D)))
long SHA1_ROTL(long a,long b)
{
long SHA1_tmp=a;
return ((SHA1_tmp>>(32-b))&(0x7fffffff>>(31-b)))|(SHA1_tmp<<b);
}
void SHA1(const unsigned char *str, size_t length, unsigned char const * sha1)
{
char *pp, *ppend;
long l, i, K[80], W[80], TEMP, A, B, C, D, E, H0, H1, H2, H3, H4;
H0 = 0x67452301, H1 = 0xEFCDAB89, H2 = 0x98BADCFE, H3 = 0x10325476, H4 = 0xC3D2E1F0;
for (i = 0; i < 20; K[i++] = 0x5A827999);
for (i = 20; i < 40; K[i++] = 0x6ED9EBA1);
for (i = 40; i < 60; K[i++] = 0x8F1BBCDC);
for (i = 60; i < 80; K[i++] = 0xCA62C1D6);
l = length + ((length % 64 > 56) ? (128 - length % 64) : (64 - length % 64));
if (!(pp = (char*)malloc((unsigned long)l))) return 0;
for (i = 0; i < length; pp[i + 3 - 2 * (i % 4)] = str[i], i++);
for (pp[i + 3 - 2 * (i % 4)] = 128,i++; i < l; pp[i + 3 - 2 * (i % 4)] = 0,i++);
*((long*)(pp + l - 4)) = length << 3;
*((long*)(pp + l - 8)) = length >> 29;
for (ppend = pp + l; pp < ppend; pp += 64){
for (i = 0; i < 16; W[i] = ((long*)pp)[i], i++);
for (i = 16; i < 80; W[i] = SHA1_ROTL((W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]), 1), i++);
A = H0, B = H1, C = H2, D = H3, E = H4;
for (i = 0; i < 80; i++){
TEMP = SHA1_ROTL(A, 5) + SHA1_F(B, C, D, i) + E + W[i] + K[i];
E = D, D = C, C = SHA1_ROTL(B, 30), B = A, A = TEMP;
}
H0 += A, H1 += B, H2 += C, H3 += D, H4 += E;
}
free(pp - l);
sprintf(sha1, "%08X%08X%08X%08X%08X", H0, H1, H2, H3, H4);
}
#endif
}