📄 sha1ex.cpp
字号:
#include "stdafx.h"
#include "SHA1Ex.h"
SHA1Ex::SHA1Ex()
{
}
SHA1Ex::~SHA1Ex()
{
}
string SHA1Ex::ResultToHex(unsigned char digest[20], bool bUppercase)
{
string strRes = "";
char TmpBuf[5] = { 0 };
for(int i = 0 ; i < 20 ; i ++)
{
memset(TmpBuf, 0, 5);
if(bUppercase)
sprintf(TmpBuf, "%.2X", digest[i]);
else
sprintf(TmpBuf, "%.2x", digest[i]);
strRes += TmpBuf;
}
return strRes;
}
string SHA1Ex::SHA1String(unsigned char *pData, unsigned long DataLen, bool bUppercase)
{
SHA1::SHA1Context context = { 0 };
unsigned char Digest[20] = { 0 };
SHA1Reset (&context);
SHA1Input (&context, pData, DataLen);
SHA1Result (&context, Digest);
return ResultToHex(Digest, bUppercase);
}
string SHA1Ex::SHA1File(const char *FileName, bool bUppercase)
{
SHA1::SHA1Context context = { 0 };
unsigned char Digest[20] = { 0 };
FILE *file = 0;
int nFileBufSize = 10240;
unsigned char *buffer = new unsigned char[nFileBufSize + 1];
m_bTerminate = false;
try
{
if ((file = fopen (FileName, "rb")) == NULL)
{
m_strErr = "The file can't be opened!";
goto ERR;
}
else
{
int len = 0;
int ReadCount = 0;
unsigned long nFileSize = 0;
{
HANDLE hFile = CreateFile(FileName, NULL, FILE_SHARE_READ, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
if(hFile== NULL)
{
m_strErr = "Get file size failed!";
goto ERR;
}
nFileSize=::GetFileSize(hFile, NULL);
CloseHandle(hFile);
}
SHA1Reset(&context);
while (len = fread (buffer, 1, nFileBufSize, file))
{
ReadCount++;
if(m_bTerminate)
{
m_strErr = "User terminate calculate!";
goto ERR;
}
SHA1Input(&context, buffer, len);
memset(buffer, nFileBufSize + 1, 0);
}
SHA1Result(&context, Digest);
fclose (file);
delete[] buffer;
return ResultToHex(Digest, bUppercase);
}
}
catch(...)
{
m_strErr = "UnKnow error!";
goto ERR;
}
ERR:
if(file != 0)
fclose (file);
delete[] buffer;
return "";
}
void SHA1Ex::StopCalculate()
{
m_bTerminate = true;
}
string SHA1Ex::GetError()
{
return m_strErr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -