• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdint.h>
15 #include <gmssl/sha1.h>
16 #include <gmssl/hex.h>
17 
18 
19 static char *teststr[] = {
20 	"abc",
21 	"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
22 	"a",
23 	"0123456701234567012345670123456701234567012345670123456701234567",
24 };
25 
26 static size_t testcnt[] = {
27 	1,
28 	1,
29 	1000000,
30 	10,
31 };
32 
33 static char *dgsthex[] = {
34 	"A9993E364706816ABA3E25717850C26C9CD0D89D",
35 	"84983E441C3BD26EBAAE4AA1F95129E5E54670F1",
36 	"34AA973CD4C4DAA4F61EEB2BDBAD27316534016F",
37 	"DEA356A2CDDD90C7A7ECEDC5EBB563934F460452",
38 };
39 
main(void)40 int main(void)
41 {
42 	int err = 0;
43 	SHA1_CTX ctx;
44 	uint8_t dgst[20];
45 	uint8_t dgstbuf[20];
46 	size_t dgstlen;
47 	size_t i, j;
48 
49 	for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
50 		hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstlen);
51 
52 		sha1_init(&ctx);
53 		for (j = 0; j < testcnt[i]; j++) {
54 			sha1_update(&ctx, (uint8_t *)teststr[i], strlen(teststr[i]));
55 		}
56 		sha1_finish(&ctx, dgst);
57 
58 		if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
59 			printf("sha1 test %lu failed\n", i+1);
60 			printf("%s\n", dgsthex[i]);
61 			for (j = 0; j < sizeof(dgst); j++) {
62 				printf("%02X", dgst[j]);
63 			}
64 			printf("\n");
65 			err++;
66 		} else {
67 			printf("sha1 test %lu ok\n", i+1);
68 		}
69 	}
70 
71 	return err;
72 }
73