SHA1:
OpenSSL provides a SHA1 library that can be invoked directly after OpenSSL is installed.
MD5:
The function prototype is shown in the attachment, where static void Mdfile (filename) is a MD5 checksum of the file, and the static void Mdstring (instring) is a MD5 checksum of the string. Can be used directly, can also be encapsulated in the library after the call, it is worth noting that the source code in the function definition of the static removed.
Hash
The hash algorithm is shown in the attachment.
TCP/IP/UDP/ICMP中的checksum:
/*计算校验和*/
USHORT checksum(USHORT *buffer,int size)
{
unsigned long cksum=0;
while(size>1)
{
cksum+=*buffer++;
size -=sizeof(USHORT);
}
if(size)
{
cksum+=*(UCHAR*)buffer;
}
cksum =(cksum>>16)+(cksum & 0xffff);
cksum+=(cksum>>16);
return (USHORT)(~cksum);
}
CRC Checksum:
int FILE_CRC32 (const char *filename,unsigned int *CRC)
{
unsigned char buffer[max_buffer_size];
unsigned int VCRC = 0xFFFFFFFF;
unsigned int read = 0;
unsigned int filesize = 0;
FILE *FP = NULL;
struct stat fst;
if (stat (FILENAME,&FST))
{
printf ("Get file info failed\n");
return-1;
}
/* Unsigned long may denote the file size * *
if ((filesize = fst.st_size) = = 0)
return-1;
/* Open File */
if (fp = fopen (filename, "r") = = NULL)
{
printf ("Open the File failed\n");
}
while (filesize)
{
Read = filesize > max_buffer_size? Max_buffer_size:filesize;
if ((read = Fread (BUFFER,1,READ,FP)) = = 0) break;
* CRC * *
CRC32 (BUFFER,READ,&VCRC);
FileSize = read;
}
*CRC = ~VCRC;
Fclose (FP);
return 0;
}
void Crc32 (const unsigned char* byte,unsigned int length,unsigned int *VCRC)
{
unsigned int i = 0;
for (i = 0; i < length; i++)
*VCRC = ((*VCRC) >> 8) ^ Crc32table[byte[i] ^ ((*VCRC) & 0X000000FF)];
}
Article Source: http://qq164587043.blog.51cto.com/261469/159169