⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csha1test.cpp

📁 可用于P2P分布式网络的哈希算法, 带有例子,直接复制出HASH类即可方便使用.
💻 CPP
字号:
#define WIN32_LEAN_AND_MEAN

#include <stdlib.h>
#include <string.h> // Needed for strlen(...)

#include "CSHA1Test.h" // Test driver header file

#include "CSHA1ClassSource/SHA1.h" // The CSHA1 class

int main(int argc, char *argv[])
{
	int ch = 0;

	// Print some information
	printf("\r\n [ Test driver for the CSHA1 class ]\r\n\r\n");

	while(ch != 3)
	{
		printf("\r\n 1) Hash a string\r\n");
		printf(" 2) Hash a file\r\n");
		printf(" 3) Exit\r\n");
		printf("\r\n Enter [1], [2] or [3]: ");
		
		// User's choice
		fflush(stdin);
		ch = getchar();

		// Process user's choice
		if(ch == '3') return(0);
		else if(ch == '1') HashString();
		else if(ch == '2') HashFile();
		else { printf("\r\n Input error: Enter 1, 2 or 3 plus [Enter]!\r\n"); }
	}

	// Print information and exit
	printf("\r\n Operation successfully completed.\r\n Press any key to exit.\r\n");
	fflush(stdin);
	getchar();
	fflush(stdin);
	return(0);
}

void HashString()
{
	char szString[1024];
	char szReport[1024];
	CSHA1 sha1;

	szString[0] = 0; szReport[0] = 0;

	printf("\r\n Enter the string to hash:\r\n ");

	fflush(stdin);
	gets(szString);

	sha1.Reset();
	sha1.Update((UINT_8 *)szString, strlen(szString));
	sha1.Final();

	sha1.ReportHash(szReport, CSHA1::REPORT_HEX);
	printf("\r\n String hashed to:\r\n ");
	printf(szReport);
	printf("\r\n");
}

void HashFile()
{
	char szFilename[1024];
	char szReport[1024];
	bool bSuccess = false;
	CSHA1 sha1;

	szFilename[0] = 0; szReport[0] = 0;

	printf("\r\n Filename: ");

	fflush(stdin);
	gets(szFilename);

	sha1.Reset();
	bSuccess = sha1.HashFile(szFilename);
	sha1.Final();

	sha1.ReportHash(szReport, CSHA1::REPORT_HEX);

	if(bSuccess == true)
	{
		printf("\r\n File contents hashed to:\r\n ");
		printf(szReport);
		printf("\r\n");
	}
	else
		printf("\r\n An error occured (does the file really exist?) !\r\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -