sha1test.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <gmssl/sha1.h>
  14. #include <gmssl/hex.h>
  15. static char *teststr[] = {
  16. "abc",
  17. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  18. "a",
  19. "0123456701234567012345670123456701234567012345670123456701234567",
  20. };
  21. static size_t testcnt[] = {
  22. 1,
  23. 1,
  24. 1000000,
  25. 10,
  26. };
  27. static char *dgsthex[] = {
  28. "A9993E364706816ABA3E25717850C26C9CD0D89D",
  29. "84983E441C3BD26EBAAE4AA1F95129E5E54670F1",
  30. "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F",
  31. "DEA356A2CDDD90C7A7ECEDC5EBB563934F460452",
  32. };
  33. int main(void)
  34. {
  35. int err = 0;
  36. SHA1_CTX ctx;
  37. uint8_t dgst[20];
  38. uint8_t dgstbuf[20];
  39. size_t dgstlen;
  40. size_t i, j;
  41. for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
  42. hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstlen);
  43. sha1_init(&ctx);
  44. for (j = 0; j < testcnt[i]; j++) {
  45. sha1_update(&ctx, (uint8_t *)teststr[i], strlen(teststr[i]));
  46. }
  47. sha1_finish(&ctx, dgst);
  48. if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
  49. printf("sha1 test %zu failed\n", i+1);
  50. printf("%s\n", dgsthex[i]);
  51. for (j = 0; j < sizeof(dgst); j++) {
  52. printf("%02X", dgst[j]);
  53. }
  54. printf("\n");
  55. err++;
  56. } else {
  57. printf("sha1 test %zu ok\n", i+1);
  58. }
  59. }
  60. return err;
  61. }