mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-11-30 02:37:57 +08:00
Add SHA1 function when no openssl
This commit is contained in:
parent
d20caa2dbb
commit
8444c58e64
@ -14,6 +14,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <drogon/config.h>
|
||||
#include <trantor/utils/Date.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@ -33,6 +34,11 @@ 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
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -312,4 +312,44 @@ 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
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user