SHA256Match.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*====================================================================*
  2. *
  3. * signed SHA256Match (signed fd, const uint8_t digest[]);
  4. *
  5. * SHA256.h
  6. *
  7. * search a fingerprint file for the given digest; return true
  8. * on success and false on failure; digests in the fingerprint
  9. * file must be upper case because comparison is binary;
  10. *
  11. * Motley Tools by Charles Maier <cmaier@cmassoc.net>;
  12. * Copyright (c) 2001-2006 by Charles Maier Associates;
  13. * Licensed under the Internet Software Consortium License;
  14. *
  15. *--------------------------------------------------------------------*/
  16. #ifndef SHA256MATCH_SOURCE
  17. #define SHA256MATCH_SOURCE
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include "../tools/number.h"
  21. #include "../key/SHA256.h"
  22. signed SHA256Match (signed fd, const uint8_t digest [])
  23. {
  24. char string [SHA256_DIGEST_LENGTH << 1];
  25. char buffer [SHA256_DIGEST_LENGTH << 1];
  26. signed length = 0;
  27. while (length < (signed) (sizeof (string)))
  28. {
  29. string [length++] = DIGITS_HEX [(* digest >> 4) & 0x0F];
  30. string [length++] = DIGITS_HEX [(* digest >> 0) & 0x0F];
  31. digest++;
  32. }
  33. while (read (fd, buffer, sizeof (buffer)) == sizeof (buffer))
  34. {
  35. char c;
  36. if (! memcmp (string, buffer, sizeof (string)))
  37. {
  38. return (1);
  39. }
  40. while (read (fd, & c, sizeof (c)) == sizeof (c))
  41. {
  42. if (c == '\n')
  43. {
  44. break;
  45. }
  46. }
  47. }
  48. return (0);
  49. }
  50. #endif