📄 securehash.cpp
字号:
// SecureHash.cpp : Defines the entry point for the DLL application.
//
/* Implementation of NIST's Secure Hash Algorithm (FIPS 180)
* Lightly bummed for execution efficiency.
*
* Jim Gillogly 3 May 1993, Modified by Zhang Yi 2005
*
*
* Output: 40-hex-digit digest of each file specified (160 bits)
*
* Synopsis of the function calls:
*
* sha_file(char *filename, unsigned long *buffer)
* Filename is a file to be opened and processed.
* buffer is a user-supplied array of 5 or more longs.
* The 5-word buffer is filled with 160 bits of non-terminated hash.
* Returns 0 if successful, non-zero if bad file.
*
* void sha_stream(FILE *stream, unsigned long *buffer)
* Input is from already-opened stream, not file.
*
* void sha_memory(char *mem, long length, unsigned long *buffer)
* Input is a memory block "length" bytes long.
*
* Caveat:
* Not tested for case that requires the high word of the length,
* which would be files larger than 1/2 gig or so.
*
* Limitation:
* sha_memory (the memory block function) will deal with blocks no longer
* than 4 gigabytes; for longer samples, the stream version will
* probably be most convenient (e.g. perl moby_data.pl | sha).
*
* Bugs:
* The standard is defined for bit strings; I assume bytes.
*
* Copyright 1993, Dr. James J. Gillogly
* This code may be freely used in any application.
*/
#include "stdafx.h"
#include <stdio.h>
#include <memory.h>
#define TRUE 1
#define FALSE 0
#define SUCCESS 0
#define FAILURE -1
extern "C" __declspec(dllexport) void sha_memory(char* mem, unsigned long len, unsigned long* buffer );
extern "C" __declspec(dllexport) int sha_file(char* filename, unsigned long* buffer);
extern "C" __declspec(dllexport) void sha_stream(FILE* stream, unsigned long* buffer);
static void nist_guts(int, FILE*, char*, unsigned long, unsigned long*);
static void byteReverse(unsigned long*, int);
/**************************** implementation ********************************/
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
/* When run on a little-endian CPU we need to perform byte reversal on an
array of longwords.*/
static void byteReverse( unsigned long *buffer, int byteCount )
{
unsigned long value;
int count;
byteCount /= sizeof( unsigned long );
for( count = 0; count < byteCount; count++ )
{
value = ( buffer[ count ] << 16 ) | ( buffer[ count ] >> 16 );
buffer[ count ] = ( ( value & 0xFF00FF00L ) >> 8 ) | ( ( value & 0x00FF00FFL ) << 8 );
}
}
union longbyte
{
unsigned long W[80]; /* Process 16 32-bit words at a time */
char B[320]; /* But read them as bytes for counting */
};
int sha_file(char* filename, unsigned long* buffer)
{
FILE *infile;
if ((infile = fopen(filename, "rb")) == NULL)
{
int i;
for (i = 0; i < 5; i++)
buffer[i] = 0xdeadbeef;
return FAILURE;
}
(void) sha_stream(infile, buffer);
fclose(infile);
return SUCCESS;
}
void sha_memory(char* mem, unsigned long length, unsigned long* buffer )
{
nist_guts(FALSE, (FILE *) NULL, mem, length, buffer);
}
void sha_stream(FILE* stream, unsigned long* buffer)
{
nist_guts(TRUE, stream, (char *) NULL, 0l, buffer);
}
#define f0(x,y,z) (z ^ (x & (y ^ z))) /* Magic functions */
#define f1(x,y,z) (x ^ y ^ z)
#define f2(x,y,z) ((x & y) | (z & (x | y)))
#define f3(x,y,z) (x ^ y ^ z)
#define K0 0x5a827999 /* Magic constants */
#define K1 0x6ed9eba1
#define K2 0x8f1bbcdc
#define K3 0xca62c1d6
#define S(n, X) ((X << n) | (X >> (32 - n))) /* Barrel roll */
#define r0(f, K) \
temp = S(5, A) + f(B, C, D) + E + *p0++ + K; \
E = D; \
D = C; \
C = S(30, B); \
B = A; \
A = temp
#define r1(f, K) \
temp = *p1++ ^ *p2++ ^ *p3++ ^ *p4++; \
temp = S(5, A) + f(B, C, D) + E + (*p0++ = S(1,temp)) + K; \
E = D; \
D = C; \
C = S(30, B); \
B = A; \
A = temp
static void nist_guts( int file_flag, FILE* stream, char* mem, unsigned long length, unsigned long* buf)
{
int i, nread, nbits;
union longbyte d;
unsigned long hi_length, lo_length;
int padded;
char *s;
register unsigned long *p0, *p1, *p2, *p3, *p4;
unsigned long A, B, C, D, E, temp;
unsigned long h0, h1, h2, h3, h4;
h0 = 0x67452301; /* Accumulators */
h1 = 0xefcdab89;
h2 = 0x98badcfe;
h3 = 0x10325476;
h4 = 0xc3d2e1f0;
padded = FALSE;
s = mem;
for (hi_length = lo_length = 0; ;) /* Process 16 longs at a time */
{
if (file_flag)
{
nread = fread(d.B, 1, 64, stream); /* Read as 64 bytes */
}
else
{
if (length < 64) nread = length;
else nread = 64;
length -= nread;
memcpy(d.B, s, nread);
s += nread;
}
if (nread < 64) /* Partial block? */
{
nbits = nread << 3; /* Length: bits */
if ((lo_length += nbits) < nbits)
hi_length++; /* 64-bit integer */
if (nread < 64 && ! padded) /* Append a single bit */
{
d.B[nread++] = 0x80; /* Using up next byte */
padded = TRUE; /* Single bit once */
}
for (i = nread; i < 64; i++) /* Pad with nulls */
d.B[i] = 0;
if (nread <= 56) /* Room for length in this block */
{
d.W[14] = hi_length;
d.W[15] = lo_length;
byteReverse(d.W, 56 );
}
else byteReverse(d.W, 64 );
}
else /* Full block -- get efficient */
{
if ((lo_length += 512) < 512)
hi_length++; /* 64-bit integer */
byteReverse(d.W, 64 );
}
p0 = d.W;
A = h0; B = h1; C = h2; D = h3; E = h4;
r0(f0,K0); r0(f0,K0); r0(f0,K0); r0(f0,K0); r0(f0,K0);
r0(f0,K0); r0(f0,K0); r0(f0,K0); r0(f0,K0); r0(f0,K0);
r0(f0,K0); r0(f0,K0); r0(f0,K0); r0(f0,K0); r0(f0,K0);
r0(f0,K0);
p1 = &d.W[13]; p2 = &d.W[8]; p3 = &d.W[2]; p4 = &d.W[0];
r1(f0,K0); r1(f0,K0); r1(f0,K0); r1(f0,K0);
r1(f1,K1); r1(f1,K1); r1(f1,K1); r1(f1,K1); r1(f1,K1);
r1(f1,K1); r1(f1,K1); r1(f1,K1); r1(f1,K1); r1(f1,K1);
r1(f1,K1); r1(f1,K1); r1(f1,K1); r1(f1,K1); r1(f1,K1);
r1(f1,K1); r1(f1,K1); r1(f1,K1); r1(f1,K1); r1(f1,K1);
r1(f2,K2); r1(f2,K2); r1(f2,K2); r1(f2,K2); r1(f2,K2);
r1(f2,K2); r1(f2,K2); r1(f2,K2); r1(f2,K2); r1(f2,K2);
r1(f2,K2); r1(f2,K2); r1(f2,K2); r1(f2,K2); r1(f2,K2);
r1(f2,K2); r1(f2,K2); r1(f2,K2); r1(f2,K2); r1(f2,K2);
r1(f3,K3); r1(f3,K3); r1(f3,K3); r1(f3,K3); r1(f3,K3);
r1(f3,K3); r1(f3,K3); r1(f3,K3); r1(f3,K3); r1(f3,K3);
r1(f3,K3); r1(f3,K3); r1(f3,K3); r1(f3,K3); r1(f3,K3);
r1(f3,K3); r1(f3,K3); r1(f3,K3); r1(f3,K3); r1(f3,K3);
h0 += A; h1 += B; h2 += C; h3 += D; h4 += E;
if (nread <= 56) break; /* If it's greater, length in next block */
}
buf[0] = h0; buf[1] = h1; buf[2] = h2; buf[3] = h3; buf[4] = h4;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -