Some of the check functions I've used

Source: Internet
Author: User
Tags crc32 file info file size function definition function prototype hash openssl printf

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.