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

📄 sha1test.c

📁 mobile ip 在linux下的一种实现
💻 C
字号:
/* $Id: sha1test.c,v 1.2 2001/07/12 15:21:21 jm Exp $ * SHA-1 tests * * Dynamic hierarchial IP tunnel * Copyright (C) 2001, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdlib.h>#include <stdio.h>#include <string.h>#include "sha1.h"int test_sha1(unsigned char *key, unsigned int key_len,	      unsigned char *data, unsigned int data_len,	      unsigned char *digest){	unsigned char mac[20];	sha1_mac(key, key_len, data, data_len, mac);	if (memcmp(mac, digest, 20) != 0) {		int i;		fprintf(stderr, "test_sha1 failed: expected ");		for (i = 0; i < 20; i++)			fprintf(stderr, "%02x", digest[i]);		fprintf(stderr, ", got ");		for (i = 0; i < 20; i++)			fprintf(stderr, "%02x", mac[i]);		fprintf(stderr, "\n");		return 1;	}	printf("SHA-1 test matched with the expected digest\n");	return 0;}int test_hmac_sha1(unsigned char *key, unsigned int key_len,		  unsigned char *data, unsigned int data_len,		  unsigned char *digest){	unsigned char mac[20];	hmac_sha1(key, key_len, data, data_len, mac);	if (memcmp(mac, digest, 20) != 0) {		int i;		fprintf(stderr, "test_hmac_sha1 failed: expected ");		for (i = 0; i < 20; i++)			fprintf(stderr, "%02x", digest[i]);		fprintf(stderr, ", got ");		for (i = 0; i < 20; i++)			fprintf(stderr, "%02x", mac[i]);		fprintf(stderr, "\n");		return 1;	}	printf("HMAC-SHA1 test matched with the expected digest\n");	return 0;}int main(int argc, char *argv[]){	int ret = 0;	char *bdata;	unsigned char digest1[] = { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81,				    0x6A, 0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50,				    0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D };	unsigned char digest2[] = { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2,				    0x6E, 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51,				    0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 };	unsigned char digest3[] = { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA,				    0xA4, 0xF6, 0x1E, 0xEB, 0x2B, 0xDB, 0xAD,				    0x27, 0x31, 0x65, 0x34, 0x01, 0x6F };	unsigned char key[80], data[50];	unsigned char hdig1[] = { 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72,				  0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37,				  0x8c, 0x8e, 0xf1, 0x46, 0xbe, 0x00 };	unsigned char hdig2[] = { 0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb, 0x2f,				  0xa2, 0xd2, 0x74, 0x16, 0xd5, 0xf1, 0x84,				  0xdf, 0x9c, 0x25, 0x9a, 0x7c, 0x79 };	unsigned char hdig3[] = { 0x12, 0x5d, 0x73, 0x42, 0xb9, 0xac, 0x11,				  0xcd, 0x91, 0xa3, 0x9a, 0xf4, 0x8a, 0xa1,				  0x7b, 0x4f, 0x63, 0xf1, 0x75, 0xd3 };	unsigned char hdig4[] = { 0x4c, 0x90, 0x07, 0xf4, 0x02, 0x62, 0x50,				  0xc6, 0xbc, 0x84, 0x14, 0xf9, 0xbf, 0x50,				  0xc8, 0x6c, 0x2d, 0x72, 0x35, 0xda };	unsigned char key4[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,				 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,				 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,				 0x16, 0x17, 0x18, 0x19 };	unsigned char hdig5[] = { 0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0,				  0x7f, 0xe7, 0xf2, 0x7b, 0xe1, 0xd5, 0x8b,				  0xb9, 0x32, 0x4a, 0x9a, 0x5a, 0x04 };	unsigned char hdig6[] = { 0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72, 0xd0,				  0x0e, 0x95, 0x70, 0x56, 0x37, 0xce, 0x8a,				  0x3b, 0x55, 0xed, 0x40, 0x21, 0x12 };	unsigned char hdig7[] = { 0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23, 0x7d,				  0x78, 0x6d, 0x6b, 0xba, 0xa7, 0x96, 0x5c,				  0x78, 0x08, 0xbb, 0xff, 0x1a, 0x91 };	if (test_sha1("", 0, "abc", 3, digest1))		ret = 1;	if (test_sha1("", 0, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmno"		      "mnopnopq", 56, digest2))		ret = 1;	bdata = (char *) malloc(1000000);	if (bdata == NULL) {		printf("Could not malloc enough memory for SHA-1 test 3\n");	} else {		memset(bdata, 'a', 1000000);		if (test_sha1("", 0, bdata, 1000000, digest3))			ret = 1;		free(bdata);		bdata = NULL;	}	/* HMAC-SHA1 test vectors from RFC 2202 */	memset(key, 0x0b, sizeof(key));	if (test_hmac_sha1(key, 20, "Hi There", 8, hdig1))		ret = 1;	if (test_hmac_sha1("Jefe", 4, "what do ya want for nothing?", 28,			  hdig2))		ret = 1;	memset(key, 0xaa, sizeof(key));	memset(data, 0xdd, sizeof(data));	if (test_hmac_sha1(key, 20, data, 50, hdig3))		ret = 1;	memset(data, 0xcd, sizeof(data));	if (test_hmac_sha1(key4, 25, data, 50, hdig4))		ret = 1;	memset(key, 0x0c, sizeof(key));	if (test_hmac_sha1(key, 20, "Test With Truncation", 20, hdig5))		ret = 1;	memset(key, 0xaa, sizeof(key));	if (test_hmac_sha1(key, 80, "Test Using Larger Than Block-Size Key - "			  "Hash Key First", 54, hdig6))		ret = 1;	if (test_hmac_sha1(key, 80, "Test Using Larger Than Block-Size Key and"			   " Larger Than One Block-Size Data", 73, hdig7))		ret = 1;	return ret;}

⌨️ 快捷键说明

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