hash.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #define TEST_NAME "hash"
  2. #include "cmptest.h"
  3. static unsigned char x[] = "testing\n";
  4. static unsigned char x2[] =
  5. "The Conscience of a Hacker is a small essay written January 8, 1986 by a "
  6. "computer security hacker who went by the handle of The Mentor, who "
  7. "belonged to the 2nd generation of Legion of Doom.";
  8. static unsigned char h[crypto_hash_BYTES];
  9. int
  10. main(void)
  11. {
  12. size_t i;
  13. crypto_hash(h, x, sizeof x - 1U);
  14. for (i = 0; i < crypto_hash_BYTES; ++i) {
  15. printf("%02x", (unsigned int) h[i]);
  16. }
  17. printf("\n");
  18. crypto_hash(h, x2, sizeof x2 - 1U);
  19. for (i = 0; i < crypto_hash_BYTES; ++i) {
  20. printf("%02x", (unsigned int) h[i]);
  21. }
  22. printf("\n");
  23. crypto_hash_sha256(h, x, sizeof x - 1U);
  24. for (i = 0; i < crypto_hash_sha256_BYTES; ++i) {
  25. printf("%02x", (unsigned int) h[i]);
  26. }
  27. printf("\n");
  28. crypto_hash_sha256(h, x2, sizeof x2 - 1U);
  29. for (i = 0; i < crypto_hash_sha256_BYTES; ++i) {
  30. printf("%02x", (unsigned int) h[i]);
  31. }
  32. printf("\n");
  33. assert(crypto_hash_bytes() > 0U);
  34. assert(strcmp(crypto_hash_primitive(), "sha512") == 0);
  35. assert(crypto_hash_sha256_bytes() > 0U);
  36. assert(crypto_hash_sha512_bytes() >= crypto_hash_sha256_bytes());
  37. assert(crypto_hash_sha512_bytes() == crypto_hash_bytes());
  38. assert(crypto_hash_sha256_statebytes() == sizeof(crypto_hash_sha256_state));
  39. assert(crypto_hash_sha512_statebytes() == sizeof(crypto_hash_sha512_state));
  40. return 0;
  41. }