📄 md5test.c
字号:
/* $Id: md5test.c,v 1.9 2001/07/12 15:21:21 jm Exp $ * MD5 and session key generation tests * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-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 "auth.h"#include "md5_mac.h"/* calculate the MAC with own routine and by calling md5sum program and * print them for comparison */void test_md5(char *key, char *data){ unsigned char mac[16]; char command[200]; int i; md5_mac((unsigned char*)key, strlen(key), (unsigned char*)data, strlen(data), mac); for (i = 0; i < 16; i++) fprintf(stderr, "%02x", mac[i]); fprintf(stderr, " = "); sprintf(command, "md5sum --string=\"%s%s%s\"", key, data, key); system(command);}int test_hmac_md5(unsigned char *key, unsigned int key_len, unsigned char *data, unsigned int data_len, unsigned char *digest){ unsigned char mac[16]; hmac_md5(key, key_len, data, data_len, mac); if (memcmp(mac, digest, 16) != 0) { int i; fprintf(stderr, "test_hmac_md5 failed: expected "); for (i = 0; i < 16; i++) fprintf(stderr, "%02x", digest[i]); fprintf(stderr, ", got "); for (i = 0; i < 16; i++) fprintf(stderr, "%02x", mac[i]); fprintf(stderr, "\n"); return 1; } printf("HMAC-MD5 test matched with the expected digest\n"); return 0;}/* generate a session key with secret key generation key */void test_sk(char *key){ unsigned char sk[16]; int i, sk_len; sk_len = generate_session_key(AUTH_ALG_MD5, (unsigned char*)key, strlen(key), sk); if (sk_len == -1) { printf("session key generation failed!\n"); return; } printf("key[%s] => sk=", key); for (i = 0; i < sk_len; i++) printf("%02x", sk[i]); printf("\n");}int main(int argc, char *argv[]){ int ret = 0; unsigned char key[80], data[50]; unsigned char digest1[] = { 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d }; unsigned char digest2[] = { 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 }; unsigned char digest3[] = { 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 }; unsigned char digest4[] = { 0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a, 0x3a, 0xea, 0x3a, 0x75, 0x16, 0x47, 0x46, 0xff, 0xaa, 0x79 }; 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 digest5[] = { 0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00, 0xf9, 0xba, 0xb9, 0x95, 0x69, 0x0e, 0xfd, 0x4c }; unsigned char digest6[] = { 0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f, 0x0b, 0x62, 0xe6, 0xce, 0x61, 0xb9, 0xd0, 0xcd }; unsigned char digest7[] = { 0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, 0x1f, 0xb1, 0xf5, 0x62, 0xdb, 0x3a, 0xa5, 0x3e };#ifdef HAVE_PROG_GNUMD5SUM test_md5("avain", "data1234567890"); test_md5("", "data1234567890"); test_md5("avain", ""); test_md5("cn83nc38nciunecudy37cndhuincdhi7unhduwclsmjckmskl", "slkdcmjkln3j8nvdjnvd83nvud8nvjnvj8nvj8inv8eh");#else printf("Warning: Your md5sum program does not accept " "the \"--string\" option\n" "Some tests will be skipped\n\n");#endif /* HMAC-MD5 test vectors from RFC 2202 */ memset(key, 0x0b, sizeof(key)); if (test_hmac_md5(key, 16, "Hi There", 8, digest1)) ret = 1; if (test_hmac_md5("Jefe", 4, "what do ya want for nothing?", 28, digest2)) ret = 1; memset(key, 0xaa, sizeof(key)); memset(data, 0xdd, sizeof(data)); if (test_hmac_md5(key, 16, data, 50, digest3)) ret = 1; memset(data, 0xcd, sizeof(data)); if (test_hmac_md5(key4, 25, data, 50, digest4)) ret = 1; memset(key, 0x0c, sizeof(key)); if (test_hmac_md5(key, 16, "Test With Truncation", 20, digest5)) ret = 1; memset(key, 0xaa, sizeof(key)); if (test_hmac_md5(key, 80, "Test Using Larger Than Block-Size Key - " "Hash Key First", 54, digest6)) ret = 1; if (test_hmac_md5(key, 80, "Test Using Larger Than Block-Size Key and " "Larger Than One Block-Size Data", 73, digest7)) ret = 1; test_sk("avain"); test_sk("avain"); test_sk(""); test_sk(""); test_sk("ksjdkjwdhk3hkjehkj3hekjh3kjhekjh3kjhdkh3e8hn"); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -