|
| 1 | +#include "sha1.h" |
| 2 | + |
| 3 | +#include <string.h> |
| 4 | +#include <openssl/sha.h> |
| 5 | + |
| 6 | +inline void encodeb64(const unsigned char* pch, char* buff) |
| 7 | +{ |
| 8 | + const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 9 | + int mode = 0, left = 0; |
| 10 | + const int len = 20; |
| 11 | + const unsigned char *pchEnd = pch + len; |
| 12 | + while (pch < pchEnd) { |
| 13 | + int enc = *(pch++); |
| 14 | + if (mode == 0) { |
| 15 | + *buff++ = pbase64[enc >> 2]; |
| 16 | + left = (enc & 3) << 4; |
| 17 | + mode = 1; |
| 18 | + } |
| 19 | + else if (mode == 1) { |
| 20 | + *buff++ = pbase64[left | (enc >> 4)]; |
| 21 | + left = (enc & 15) << 2; |
| 22 | + mode = 2; |
| 23 | + } |
| 24 | + else { |
| 25 | + *buff++ = pbase64[left | (enc >> 6)]; |
| 26 | + *buff++ = pbase64[enc & 63]; |
| 27 | + mode = 0; |
| 28 | + } |
| 29 | + } |
| 30 | + *buff = pbase64[left]; |
| 31 | + *(buff + 1) = 0; |
| 32 | +} |
| 33 | + |
| 34 | +void sha1_hash(const char* input, char* output, uint32_t len) |
| 35 | +{ |
| 36 | + char str[38] __attribute__((aligned(32))); // 26 + 11 + 1 |
| 37 | + uint32_t prehash[5] __attribute__((aligned(32))); |
| 38 | + uint32_t hash[5] __attribute__((aligned(32))) = { 0 }; |
| 39 | + int i = 0; |
| 40 | + SHA_CTX ctx; |
| 41 | + SHA1_Init(&ctx); |
| 42 | + SHA1_Update(&ctx, (void *)input, len); |
| 43 | + SHA1_Final((void *)prehash, &ctx); |
| 44 | + encodeb64((const unsigned char *)prehash, str); |
| 45 | + memcpy(&str[26], str, 11); |
| 46 | + str[37] = 0; |
| 47 | + for (i = 0; i < 26; i++) { |
| 48 | + SHA1_Init(&ctx); |
| 49 | + SHA1_Update(&ctx, (void *)&str[i], 12); |
| 50 | + SHA1_Final((void *)prehash, &ctx); |
| 51 | + hash[0] ^= prehash[0]; |
| 52 | + hash[1] ^= prehash[1]; |
| 53 | + hash[2] ^= prehash[2]; |
| 54 | + hash[3] ^= prehash[3]; |
| 55 | + hash[4] ^= prehash[4]; |
| 56 | + } |
| 57 | + memset(output, 0, 32 - 20); |
| 58 | + memcpy(&output[32 - 20], hash, 20); |
| 59 | +} |
0 commit comments