zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++获取文件哈希值(hash)和获取torrent(bt种子)磁力链接哈希值

C++文件BT链接哈希 获取 HASH 种子
2023-06-13 09:15:12 时间

复制代码代码如下:


//CHash.h:headerfile


#pragmaonce
#include"sha1.h"

#define       SIZE_OF_BUFFER        16000

classCHash
{
//Construction
public:
   CStringSHA1Hash(CStringstrHashFile);
};

复制代码代码如下:


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

CStringCHash::SHA1Hash(CStringstrHashFile)
{
   USES_CONVERSION;
   FILE*fileToHash=NULL;
   unsignedlonglenRead=0;
   unsignedcharfileBuf[SIZE_OF_BUFFER];
   sha1_ctxm_sha1;
   unsignedchar*tempOut=newunsignedchar[256];
   CString   tempHash;
   CStringoutHash;

   sha1_begin(&m_sha1);

   fileToHash=fopen(T2A(strHashFile),"rb");
   do
   {
       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(inti=0;i<20;i++)
   {
       chartmp[3];
       _itoa(tempOut[i],tmp,16);
       if(strlen(tmp)==1)
       {
           tmp[1]=tmp[0];
           tmp[0]="0";
           tmp[2]="\0";
       }
       tempHash+=tmp;   

   }

   delete[]tempOut;

   outHash=tempHash;

   returnoutHash;
}