20 #if defined(_WIN32) || defined(_WIN64) 21 #pragma comment(lib, "crypt32.lib") 25 if (!CryptAcquireContext(&c->hProv, NULL, NULL,
26 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
28 if (!CryptCreateHash(c->hProv, CALG_SHA1, 0, 0, &c->hHash))
30 CryptReleaseContext(c->hProv, 0);
39 if (!CryptHashData(c->hHash, data, (DWORD)len, 0))
48 if (CryptGetHashParam(c->hHash, HP_HASHVAL, md, &md_len, 0))
50 CryptDestroyHash(c->hHash);
51 CryptReleaseContext(c->hProv, 0);
56 #if defined(__linux__) || defined(__CYGWIN__) 58 #elif defined(__APPLE__) 59 # include <libkern/OSByteOrder.h> 60 # define htobe32(x) OSSwapHostToBigInt32(x) 61 # define be32toh(x) OSSwapBigToHostInt32(x) 62 #elif defined(__FreeBSD__) || defined(__NetBSD__) 63 # include <sys/endian.h> 66 static unsigned char pad[64] = {
67 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
82 ctx->
h[0] = 0x67452301;
83 ctx->
h[1] = 0xEFCDAB89;
84 ctx->
h[2] = 0x98BADCFE;
85 ctx->
h[3] = 0x10325476;
86 ctx->
h[4] = 0xC3D2E1F0;
94 #define ROTATE_LEFT32(a, n) (((a) << (n)) | ((a) >> (32 - (n)))) 102 for ( i = 0; i < 5; ++i )
108 for ( i = 0; i < 16; ++i )
109 w[i] = be32toh(w[i]);
111 for( i = 0; i < 80; ++i )
115 w[i & 0x0F] =
ROTATE_LEFT32( w[(i+13) & 0x0F] ^ w[(i+8) & 0x0F] ^ w[(i+2) & 0x0F] ^ w[i & 0x0F], 1 );
118 tmp =
ROTATE_LEFT32(blks[0], 5) + ((blks[1] & blks[2]) | (~(blks[1]) & blks[3])) + blks[4] + w[i & 0x0F] + 0x5A827999;
120 tmp =
ROTATE_LEFT32(blks[0], 5) + (blks[1]^blks[2]^blks[3]) + blks[4] + w[i & 0x0F] + 0x6ED9EBA1;
122 tmp =
ROTATE_LEFT32(blks[0], 5) + ((blks[1] & blks[2]) | (blks[1] & blks[3]) | (blks[2] & blks[3])) + blks[4] + w[i & 0x0F] + 0x8F1BBCDC;
124 tmp =
ROTATE_LEFT32(blks[0], 5) + (blks[1]^blks[2]^blks[3]) + blks[4] + w[i & 0x0F] + 0xCA62C1D6;
135 for ( i = 0; i < 5; ++i )
136 ctx->
h[i] += blks[i];
147 total = ctx->
total * 8;
149 if ( ctx->
size < 56 )
150 pad_amount = 56 - ctx->
size;
152 pad_amount = 64 + 56 - ctx->
size;
156 ctx->
w[14] = htobe32((uint32_t)(total >> 32));
157 ctx->
w[15] = htobe32((uint32_t)total);
161 for ( i = 0; i < 5; ++i )
162 ctx->
h[i] = htobe32(ctx->
h[i]);
177 unsigned int n = 64 - ctx->
size;
186 data = (uint8_t *)data + n;
189 if ( ctx->
size == 64 )
201 #if defined(SHA1_TEST) 205 #define TEST_EXPECT(i,x) if (!(x)) {fprintf( stderr, "failed test: %s (for i == %d)\n", #x, i ); ++fails;} 207 int main(
int argc,
char *argv[])
216 unsigned int fails = 0u;
217 struct _td test_data[] = {
218 {
"",
"da39a3ee5e6b4b0d3255bfef95601890afd80709" },
219 {
"this string",
"fda4e74bc7489a18b146abdf23346d166663dab8" },
225 while ( test_data[i].in != NULL )
227 int r[3] = { 1, 1, 1 };
233 r[1] =
SHA1_Update( &c, test_data[i].in, strlen(test_data[i].in));
236 snprintf( &out[j*2], 3u,
"%02x", sha_out[j] );
237 out[SHA1_DIGEST_LENGTH * 2] =
'\0';
238 TEST_EXPECT( i, r[0] == 1 && r[1] == 1 && r[2] == 1 && strncmp(out, test_data[i].out, strlen(test_data[i].out)) == 0 );
243 printf(
"%u test failed!\n", fails );
245 printf(
"all tests passed\n" );
int SHA1_Final(unsigned char *md, SHA_CTX *ctx)
static unsigned char pad[64]
int SHA1_Update(SHA_CTX *ctx, const void *data, size_t len)
#define ROTATE_LEFT32(a, n)
int SHA1_Init(SHA_CTX *ctx)
#define SHA1_DIGEST_LENGTH
static void SHA1_ProcessBlock(SHA_CTX *ctx)
int main(int argc, char **argv)