md5test.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/md5.h>
  14. #include <gmssl/hex.h>
  15. static char *teststr[] = {
  16. "",
  17. "a",
  18. "abc",
  19. "message digest",
  20. "abcdefghijklmnopqrstuvwxyz",
  21. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  22. "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
  23. };
  24. static char *dgsthex[] = {
  25. "d41d8cd98f00b204e9800998ecf8427e",
  26. "0cc175b9c0f1b6a831c399e269772661",
  27. "900150983cd24fb0d6963f7d28e17f72",
  28. "f96b697d7cb7938d525a2f31aaf161d0",
  29. "c3fcd3d76192e4007dfb496cca67e13b",
  30. "d174ab98d277d9f5a5611c2c9f419d9f",
  31. "57edf4a22be3c955ac49da2e2107b67a",
  32. };
  33. int main(int argc, char **argv)
  34. {
  35. int err = 0;
  36. uint8_t dgst[16];
  37. uint8_t dgstbuf[16];
  38. size_t dgstbuflen;
  39. size_t i;
  40. for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
  41. hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen);
  42. md5_digest((uint8_t *)teststr[i], strlen(teststr[i]), dgst);
  43. if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
  44. int n;
  45. printf("error calculating MD5 on %s\n", teststr[i]);
  46. printf(" digest(corret) = ");
  47. for (n = 0; n < sizeof(dgst); n++) {
  48. printf("%02X", dgst[n]);
  49. }
  50. printf("\n");
  51. printf(" digest(error) = %s\n", dgsthex[i]);
  52. err++;
  53. } else {
  54. printf("md5 test %zu ok\n", i+1);
  55. }
  56. }
  57. return err;
  58. }