/* Given a secret and a salt, generates a salted hash (which you can then store safely). */ static VALUE bc_crypt(VALUE self, VALUE key, VALUE setting) { char * value; void * data; int size; VALUE out; data = NULL; size = 0xDEADBEEF; if(NIL_P(key) || NIL_P(setting)) return Qnil; value = crypt_ra( NIL_P(key) ? NULL : StringValuePtr(key), NIL_P(setting) ? NULL : StringValuePtr(setting), &data, &size); if(!value) return Qnil; out = rb_str_new(data, size - 1); free(data); return out; }
static bool test_one_setting (const char *label, const char *setting, struct crypt_data *cd, bool expected_to_succeed) { bool ok = true; const char *retval; int cdsize = (int) sizeof (struct crypt_data); #ifdef VERBOSE printf ("%s: testing %s (expect: %s)\n", label, setting, expected_to_succeed ? "succeed" : "fail"); #endif retval = crypt (phrase, setting); if (!check_crypt (label, "crypt", retval, setting, expected_to_succeed)) ok = false; retval = crypt_r (phrase, setting, cd); if (!check_crypt (label, "crypt_r", retval, setting, expected_to_succeed)) ok = false; retval = crypt_rn (phrase, setting, cd, cdsize); if (!check_crypt_rn (label, "crypt_rn", retval, cd->output, setting, expected_to_succeed)) ok = false; retval = crypt_ra (phrase, setting, (void **)&cd, &cdsize); if (!check_crypt_rn (label, "crypt_ra", retval, cd->output, setting, expected_to_succeed)) ok = false; return ok; }
std::string bcrypt_hash(const std::string& key,const std::string& salt) { BOOSTER_LOG(debug,__FUNCTION__); void* data = NULL; int size; if(key.empty() || salt.empty()) return ""; BOOSTER_LOG(debug,__FUNCTION__) << "generate hash"; //char* retval = crypt_ra(key.c_str(),salt.c_str(),&data,&size); std::string ret = crypt_ra(key.c_str(),salt.c_str(),&data,&size); //BOOSTER_LOG(debug,__FUNCTION__) << retval; //std::string ret(retval); free(data); if ( ret.empty() ) { BOOSTER_LOG(error,__FUNCTION__) << "Unable to generate a hash"; return ""; } BOOSTER_LOG(debug,__FUNCTION__) << "hash(" << ret << ")"; return ret; }