bcrypt.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef BCRYPT_H_
  2. #define BCRYPT_H_
  3. /*
  4. * bcrypt wrapper library
  5. *
  6. * Written in 2011, 2013, 2014, 2015 by Ricardo Garcia <r@rg3.name>
  7. *
  8. * To the extent possible under law, the author(s) have dedicated all copyright
  9. * and related and neighboring rights to this software to the public domain
  10. * worldwide. This software is distributed without any warranty.
  11. *
  12. * You should have received a copy of the CC0 Public Domain Dedication along
  13. * with this software. If not, see
  14. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  15. */
  16. #define BCRYPT_HASHSIZE (64)
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*
  21. * This function expects a work factor between 4 and 31 and a char array to
  22. * store the resulting generated salt. The char array should typically have
  23. * BCRYPT_HASHSIZE bytes at least. If the provided work factor is not in the
  24. * previous range, it will default to 12.
  25. *
  26. * The return value is zero if the salt could be correctly generated and
  27. * nonzero otherwise.
  28. *
  29. */
  30. int bcrypt_gensalt(int workfactor, char salt[BCRYPT_HASHSIZE]);
  31. /*
  32. * This function expects a password to be hashed, a salt to hash the password
  33. * with and a char array to leave the result. Both the salt and the hash
  34. * parameters should have room for BCRYPT_HASHSIZE characters at least.
  35. *
  36. * It can also be used to verify a hashed password. In that case, provide the
  37. * expected hash in the salt parameter and verify the output hash is the same
  38. * as the input hash. However, to avoid timing attacks, it's better to use
  39. * bcrypt_checkpw when verifying a password.
  40. *
  41. * The return value is zero if the password could be hashed and nonzero
  42. * otherwise.
  43. */
  44. int bcrypt_hashpw(const char *passwd, const char salt[BCRYPT_HASHSIZE],
  45. char hash[BCRYPT_HASHSIZE]);
  46. /*
  47. * This function expects a password and a hash to verify the password against.
  48. * The internal implementation is tuned to avoid timing attacks.
  49. *
  50. * The return value will be -1 in case of errors, zero if the provided password
  51. * matches the given hash and greater than zero if no errors are found and the
  52. * passwords don't match.
  53. *
  54. */
  55. int bcrypt_checkpw(const char *passwd, const char hash[BCRYPT_HASHSIZE]);
  56. /*
  57. * Brief Example
  58. * -------------
  59. *
  60. * Hashing a password:
  61. *
  62. * char salt[BCRYPT_HASHSIZE];
  63. * char hash[BCRYPT_HASHSIZE];
  64. * int ret;
  65. *
  66. * ret = bcrypt_gensalt(12, salt);
  67. * assert(ret == 0);
  68. * ret = bcrypt_hashpw("thepassword", salt, hash);
  69. * assert(ret == 0);
  70. *
  71. *
  72. * Verifying a password:
  73. *
  74. * int ret;
  75. *
  76. * ret = bcrypt_checkpw("thepassword", "expectedhash");
  77. * assert(ret != -1);
  78. *
  79. * if (ret == 0) {
  80. * printf("The password matches\n");
  81. * } else {
  82. * printf("The password does NOT match\n");
  83. * }
  84. *
  85. */
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif