C + + Get file hash value (hash) and get torrent (bt Seed) Magnetic link hash Value _c language

Source: Internet
Author: User
Tags hash

Copy Code code as follows:

Chash.h:header file


#pragma once
#include "Sha1.h"

#define Size_of_buffer 16000

Class Chash
{
Construction
Public
CString Sha1hash (CString strhashfile);
};

Copy Code code as follows:

CHash.cpp:implementation file
//
#include "stdafx.h"
#include "CHash.h"
#include <atlconv.h>

CString Chash::sha1hash (CString strhashfile)
{
Uses_conversion;
FILE *filetohash = NULL;
unsigned long lenread = 0;
unsigned char filebuf[size_of_buffer];
Sha1_ctx M_SHA1;
unsigned char* tempout = new unsigned char[256];
CString Temphash;
CString Outhash;

Sha1_begin (&M_SHA1);

Filetohash = fopen (T2A (Strhashfile), "RB");
Todo
{
Lenread = Fread (filebuf, 1, Size_of_buffer, Filetohash);
if (lenread!= 0)
{
Sha1_hash (Filebuf, Lenread, &M_SHA1);
}
while (Lenread = = Size_of_buffer);

Fclose (Filetohash); Filetohash = NULL;

Sha1_end (Tempout, &M_SHA1);

for (int i = 0; i < i++)
{
Char tmp[3];
_itoa (Tempout[i], TMP, 16);
if (strlen (tmp) = = 1)
{
TMP[1] = tmp[0];
Tmp[0] = ' 0 ';
TMP[2] = ' the ';
}
Temphash + = tmp;

}

Delete[] Tempout;

Outhash = Temphash;

return outhash;
}

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.