bcrypt.h (3248B)
1 #ifndef BCRYPT_H_ 2 #define BCRYPT_H_ 3 /* 4 * bcrypt wrapper library 5 * 6 * Written by Remy. 7 * No copyright is claimed, and the software is hereby placed in the public 8 * domain. In case this attempt to disclaim copyright and place the software 9 * in the public domain is deemed null and void, then the software is 10 * Copyright (c) 2000-2014 Solar Designer and it is hereby released to the 11 * general public under the following terms: 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted. 15 * 16 * There's ABSOLUTELY NO WARRANTY, express or implied. 17 */ 18 19 #define BCRYPT_HASHSIZE (64) 20 21 /* 22 * This function expects a work factor between 4 and 31 and a char array to 23 * store the resulting generated salt. The char array should typically have 24 * BCRYPT_HASHSIZE bytes at least. If the provided work factor is not in the 25 * previous range, it will default to 12. 26 * 27 * The return value is nonzero if the salt could be correctly generated and 28 * zero otherwise. 29 * 30 */ 31 int bcryptGensaltSync(int workfactor, char salt[BCRYPT_HASHSIZE]); 32 33 /* 34 * This function expects a password to be hashed, a salt to hash the password 35 * with and a char array to leave the result. Both the salt and the hash 36 * parameters should have room for BCRYPT_HASHSIZE characters at least. 37 * 38 * It can also be used to verify a hashed password. In that case, provide the 39 * expected hash in the salt parameter and verify the output hash is the same 40 * as the input hash. However, to avoid timing attacks, it's better to use 41 * bcryptCheckSync when verifying a password. 42 * 43 * The return value is nonzero if the password could be hashed and zero 44 * otherwise. 45 */ 46 int bcryptHashSync(const char *passwd, const char salt[BCRYPT_HASHSIZE], 47 char hash[BCRYPT_HASHSIZE]); 48 49 /* 50 * This function expects a password and a hash to verify the password against. 51 * The internal implementation is tuned to avoid timing attacks. 52 * 53 * The return value will be greater than zero if the provided password matches 54 * the given hash and zero if the passwords don't match. 55 * 56 */ 57 int bcryptCheckSync(const char *passwd, const char hash[BCRYPT_HASHSIZE]); 58 59 /* 60 * Brief Example 61 * ------------- 62 * 63 * Hashing a password: 64 * 65 * char salt[BCRYPT_HASHSIZE]; 66 * char hash[BCRYPT_HASHSIZE]; 67 * int ret; 68 * 69 * ret = bcryptGensaltSync(12, salt); 70 * assert(ret); 71 * ret = bcryptHashSync("thepassword", salt, hash); 72 * assert(ret); 73 * 74 * 75 * Verifying a password: 76 * 77 * int ret; 78 * 79 * ret = bcryptCheckSync("thepassword", "expectedhash"); 80 * assert(ret != -1); 81 * 82 * if (ret) { 83 * printf("The password matches\n"); 84 * } else { 85 * printf("The password does NOT match\n"); 86 * } 87 * 88 */ 89 90 typedef void (*bcryptHashCallbackt)(i64 err, char hash[BCRYPT_HASHSIZE], void *callbackEnv); 91 92 int bcryptHash(const char *passwd, u8 saltRounds, bcryptHashCallbackt callback, void *callbackEnv); 93 94 typedef void (*bcryptCheckCallbackt)(i64 err, int result, void *callbackEnv); 95 96 int bcryptCheck(const char *passwd, char hash[BCRYPT_HASHSIZE], bcryptCheckCallbackt callback, void *callbackEnv); 97 98 99 #endif 100 101 #define isBcryptCompiledWithCurrentLisheepyVersion checkLibsheepyVersionBcrypt(LIBSHEEPY_VERSION) 102 bool checkLibsheepyVersionBcrypt(const char *currentLibsheepyVersion);