rehash-util.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* rehash --- a decentralised hash<->hash store
  2. Copyright © 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
  3. This file is part of rehash.
  4. rehash is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at
  7. your option) any later version.
  8. rehash is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with rehash. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <gnunet/gnunet_config.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include "rehash_types.h"
  18. #include "rehash_util.h"
  19. #include "fs_api.h"
  20. static const int hash_lengths[] = {
  21. [REHASH_HASH_NONE] = 0,
  22. [REHASH_HASH_SHA256] = 32,
  23. [REHASH_HASH_SHA512] = 64,
  24. [REHASH_HASH_SHA3_256] = 32,
  25. [REHASH_HASH_SHA3_512] = 64,
  26. /* TODO check */
  27. [REHASH_HASH_BLAKE2S_256] = 32,
  28. /* */
  29. [REHASH_HASH_FS_REGULAR] = sizeof(struct FileIdentifier),
  30. };
  31. static const char hash_names[][12] = {
  32. [REHASH_HASH_SHA256] = "sha256",
  33. [REHASH_HASH_SHA512] = "sha512",
  34. [REHASH_HASH_SHA3_256] = "sha3-256",
  35. [REHASH_HASH_SHA3_512] = "sha3-512",
  36. [REHASH_HASH_BLAKE2S_256] = "blake2s-256",
  37. [REHASH_HASH_FS_REGULAR] = "fs-regular",
  38. };
  39. int
  40. REHASH_hashtype_strsize (enum GNUNET_REHASH_Hash_Type type)
  41. {
  42. int index;
  43. index = type > REHASH_HASH_LAST ? 0 : (int) type;
  44. return hash_lengths[index];
  45. }
  46. int
  47. REHASH_unparse_hashtype (enum GNUNET_REHASH_Hash_Type type, int size, char outbuf[])
  48. {
  49. if ((type == REHASH_HASH_NONE) || (type > GNUNET_REHASH_HASH_LAST))
  50. return 0;
  51. strncpy(outbuf, hash_names[type], size);
  52. return size;
  53. }
  54. enum REHASH_Hash_Type
  55. REHASH_parse_hashtype (const char *name)
  56. {
  57. enum REHASH_Hash_Type candidate;
  58. for (candidate = 1; candidate <= REHASH_HASH_LAST; candidate++)
  59. {
  60. if (0 == strcmp(name, hash_names[(int) candidate]))
  61. return candidate;
  62. }
  63. return REHASH_HASH_NONE;
  64. }