test_hostkey_hash.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "session_fixture.h"
  2. #include "libssh2_config.h"
  3. #include <libssh2.h>
  4. #include <stdio.h>
  5. const char *EXPECTED_HOSTKEY =
  6. "AAAAB3NzaC1yc2EAAAABIwAAAQEArrr/JuJmaZligyfS8vcNur+mWR2ddDQtVdhHzdKU"
  7. "UoR6/Om6cvxpe61H1YZO1xCpLUBXmkki4HoNtYOpPB2W4V+8U4BDeVBD5crypEOE1+7B"
  8. "Am99fnEDxYIOZq2/jTP0yQmzCpWYS3COyFmkOL7sfX1wQMeW5zQT2WKcxC6FSWbhDqrB"
  9. "eNEGi687hJJoJ7YXgY/IdiYW5NcOuqRSWljjGS3dAJsHHWk4nJbhjEDXbPaeduMAwQU9"
  10. "i6ELfP3r+q6wdu0P4jWaoo3De1aYxnToV/ldXykpipON4NPamsb6Ph2qlJQKypq7J4iQ"
  11. "gkIIbCU1A31+4ExvcIVoxLQw/aTSbw==";
  12. const char *EXPECTED_MD5_HASH_DIGEST = "0C0ED1A5BB10275F76924CE187CE5C5E";
  13. const char *EXPECTED_SHA1_HASH_DIGEST =
  14. "F3CD59E2913F4422B80F7B0A82B2B89EAE449387";
  15. const int MD5_HASH_SIZE = 16;
  16. const int SHA1_HASH_SIZE = 20;
  17. static void calculate_digest(const char *hash, size_t hash_len, char *buffer,
  18. size_t buffer_len)
  19. {
  20. size_t i;
  21. char *p = buffer;
  22. char *end = buffer + buffer_len;
  23. for (i = 0; i < hash_len && p < end; ++i) {
  24. p += snprintf(p, end - p, "%02X", (unsigned char)hash[i]);
  25. }
  26. }
  27. int test(LIBSSH2_SESSION *session)
  28. {
  29. char buf[BUFSIZ];
  30. const char *md5_hash;
  31. const char *sha1_hash;
  32. md5_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
  33. if (md5_hash == NULL) {
  34. print_last_session_error(
  35. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_MD5)");
  36. return 1;
  37. }
  38. calculate_digest(md5_hash, MD5_HASH_SIZE, buf, BUFSIZ);
  39. if (strcmp(buf, EXPECTED_MD5_HASH_DIGEST) != 0) {
  40. fprintf(stderr, "MD5 hash not as expected - digest %s != %s\n", buf,
  41. EXPECTED_MD5_HASH_DIGEST);
  42. return 1;
  43. }
  44. sha1_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  45. if (sha1_hash == NULL) {
  46. print_last_session_error(
  47. "libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA1)");
  48. return 1;
  49. }
  50. calculate_digest(sha1_hash, SHA1_HASH_SIZE, buf, BUFSIZ);
  51. if (strcmp(buf, EXPECTED_SHA1_HASH_DIGEST) != 0) {
  52. fprintf(stderr, "SHA1 hash not as expected - digest %s != %s\n", buf,
  53. EXPECTED_SHA1_HASH_DIGEST);
  54. return 1;
  55. }
  56. return 0;
  57. }