short_hash.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <rtthread.h>
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <limits.h>
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "sodium.h"
  10. #define MAXLEN 64
  11. static unsigned char in[MAXLEN];
  12. static unsigned char out[crypto_shorthash_BYTES];
  13. static unsigned char k[crypto_shorthash_KEYBYTES];
  14. int short_hash(int argc, char* argv[])
  15. {
  16. size_t i;
  17. // Set Hash Key
  18. for (i = 0; i < crypto_shorthash_KEYBYTES; ++i) {
  19. k[i] = (unsigned char) i;
  20. }
  21. // Hash function
  22. if (argc == 2) {
  23. crypto_shorthash(out, (const unsigned char *) argv[1], strlen(argv[1]), k);
  24. printf("short_hash(\"%s\") = ", argv[1]);
  25. }
  26. else {
  27. printf("Usage: %s 01\n", argv[0]);
  28. in[0] = (unsigned char) '0';
  29. in[1] = (unsigned char) '1';
  30. crypto_shorthash(out, in, 2, k);
  31. printf("short_hash(\"%c%c\") = ", in[0], in[1]);
  32. }
  33. for (i = 0; i < crypto_shorthash_BYTES; ++i) {
  34. printf("%02x", (unsigned int) out[i]);
  35. }
  36. printf("\n");
  37. return 0;
  38. }
  39. MSH_CMD_EXPORT(short_hash, libsodium short hash)