char *ftsi_generate_bc_salt(char *prefix, int work_factor) { if (work_factor < 4 || work_factor > 31) { errno = EINVAL; return NULL; } char rand[BC_RANDSIZE]; FILE *f = fopen("/dev/urandom", "r"); if (f == NULL) return NULL; size_t r = fread(rand, sizeof(char), BC_RANDSIZE, f); if (r < BC_RANDSIZE) return NULL; if (fclose(f) != 0) return NULL; char *salt = calloc(BC_HASHSIZE + 1, sizeof(char)); if (prefix == NULL) prefix = "$2a$"; char *s = crypt_gensalt_rn( prefix, work_factor, rand, BC_RANDSIZE, salt, BC_HASHSIZE); if (s == NULL) { free(salt); return NULL; } return salt; }
static PyObject * _py_crypt_gensalt_rn(PyObject *self, PyObject *args) { char *rc; const char *prefix; const int count; const char *salt; const Py_ssize_t salt_len; char output[30]; memset(output, 0, sizeof(output)); if (!PyArg_ParseTuple(args, "sis#", &prefix, &count, &salt, &salt_len)) { return NULL; } Py_BEGIN_ALLOW_THREADS; /* prefix, count, input, size, output, output_size */ rc = crypt_gensalt_rn(prefix, count, salt, salt_len, output, sizeof(output)); Py_END_ALLOW_THREADS; if (rc == NULL) { Py_RETURN_NONE; } output[sizeof(output) - 1] = '\0'; return Py_BuildValue("s", output); }
int bcrypt_gensalt(int factor, char salt[BCRYPT_HASHSIZE]) { int fd; char input[RANDBYTES]; int workf; char *aux; fd = open("/dev/urandom", O_RDONLY); if (fd == -1) return 1; if (try_read(fd, input, RANDBYTES) != 0) { if (try_close(fd) != 0) return 4; return 2; } if (try_close(fd) != 0) return 3; /* Generate salt. */ workf = (factor < 4 || factor > 31)?12:factor; aux = crypt_gensalt_rn("$2a$", workf, input, RANDBYTES, salt, BCRYPT_HASHSIZE); return (aux == NULL)?5:0; }
int bcrypt_gensalt(int factor, char salt[BCRYPT_HASHSIZE]) { char input[RANDBYTES]; int workf; char *aux; #ifdef WIN32 BCryptGenRandom(NULL, input, RANDBYTES, BCRYPT_USE_SYSTEM_PREFERRED_RNG); #else int fd; fd = open("/dev/urandom", O_RDONLY); if (fd == -1) return 1; if (try_read(fd, input, RANDBYTES) != 0) { if (try_close(fd) != 0) return 4; return 2; } if (try_close(fd) != 0) return 3; #endif /* Generate salt. */ workf = (factor < 4 || factor > 31)?12:factor; aux = crypt_gensalt_rn("$2a$", workf, input, RANDBYTES, salt, BCRYPT_HASHSIZE); return (aux == NULL)?5:0; }
std::string BCryptHashFunction::compute(const std::string& msg, const std::string& salt) const { //#ifdef __APPLE__ char setting[32]; char c_salt[16]; std::strncpy(c_salt, salt.c_str(), 16); if (salt.length() < 16) std::memset(c_salt + salt.length(), 'A', 16 - salt.length()); if (!crypt_gensalt_rn("$2y$", count_, c_salt, 16, setting, 32)) { std::perror("crypt_gen_salt_rn"); throw std::runtime_error("bcrypt() gensalt internal error"); } else { char result[64]; std::cout<<"setting "<<setting<<std::endl; if (!crypt_rn(msg.c_str(), setting, result, 64)) { std::perror("crypt_rn"); throw std::runtime_error("bcrypt() internal error"); } return result; } //#else // std::string md5Salt = "$1$" + salt; // return crypt(msg.c_str(), md5Salt.c_str()); //#endif }
std::string generate_salt() { char salt[32], entrophy[16]; for (unsigned int x = 0; x < sizeof(entrophy); x++) entrophy[x] = ServerInstance->GenRandomInt(0xff); if (!crypt_gensalt_rn("$2a$", WorkFactor, entrophy, sizeof(entrophy), salt, sizeof(salt))) return ""; return salt; }
/* * This function expects a work factor between 4 and 31 and a char array to * store the resulting generated salt. The char array should typically have * BCRYPT_HASHSIZE bytes at least. If the provided work factor is not in the * previous range, it will default to 12. * * The return value is zero if the salt could be correctly generated and * nonzero otherwise. * */ int bcrypt_gensalt(const char* prefix, int factor, char salt[BCRYPT_HASHSIZE]) { #define RANDBYTES (16) //char input[RANDBYTES]; uint16_t input; int workf; char *aux; std::mt19937 rng; rng.seed(std::random_device()()); std::uniform_int_distribution<uint16_t> dist; input = dist(rng); /* Generate salt. */ workf = (factor < 4 || factor > 31)?12:factor; aux = crypt_gensalt_rn(prefix, workf, reinterpret_cast<const char*>(&input), RANDBYTES, salt, BCRYPT_HASHSIZE); return (aux == nullptr)?5:0; #undef RANDBYTES }
std::string BCryptHashFunction::compute(const std::string& msg, const std::string& salt) const { char setting[32]; char c_salt[16]; std::strncpy(c_salt, salt.c_str(), 16); if (salt.length() < 16) std::memset(c_salt + salt.length(), 'A', 16 - salt.length()); if (!crypt_gensalt_rn("$2y$", count_, c_salt, 16, setting, 32)) { std::perror("crypt_gen_salt_rn"); throw std::runtime_error("bcrypt() gensalt internal error"); } else { char result[64]; if (!crypt_rn(msg.c_str(), setting, result, 64)) { std::perror("crypt_rn"); throw std::runtime_error("bcrypt() internal error"); } return result; } }
static char *do_crypt(const char *password, char *output, int output_len) { char salt[16]; char tmp[64]; char *setting; FILE *fp = fopen("/dev/urandom","r"); if (!fp) return NULL; if (fread(salt, sizeof(salt), 1, fp) != 1) { fclose(fp); return NULL; } fclose(fp); memset(tmp, 0, sizeof(tmp)); if (!(setting = crypt_gensalt_rn("$2y$", 0, salt, sizeof(salt), tmp, sizeof(tmp)))) return NULL; memset(output, 0, output_len); return crypt_rn(password, setting, output, output_len); }
char *bcrypt_hash(UDF_INIT *initid, UDF_ARGS *args, char *res, unsigned long *len, char *is_null, char *err) { int ret, fd; char *aux; char randb[RANDBYTES]; char pass[PASS_MAXLEN+1]; char salt[BCRYPT_HASHSIZE]; long long workfactor; /* password */ if (!args->args[0]) { *is_null = 1; return 0; } else { if ((ret = my_str_to_c_str(pass, sizeof(pass), args->args[0], args->lengths[0])) != 0) { *is_null = 1; return 0; } } /* work factor */ if (args->args[1]) { workfactor = *(long long*) args->args[1]; } else { workfactor = WORKFACTOR_DEFAULT; } if (workfactor < WORKFACTOR_MIN) { workfactor = WORKFACTOR_MIN; } else if (workfactor > WORKFACTOR_MAX) { workfactor = WORKFACTOR_MAX; } /* start salt generation */ if ((fd = open("/dev/urandom", O_RDONLY)) < 0) { *is_null = 1; return 0; } if (try_read(fd, randb, RANDBYTES) != 0) { try_close(fd); *is_null = 1; return 0; } if (try_close(fd) != 0) { *is_null = 1; return 0; } if ((aux = crypt_gensalt_rn("$2b$", workfactor, randb, RANDBYTES, salt, BCRYPT_HASHSIZE)) == NULL) { *is_null = 1; return 0; } /* end salt generation */ /* compute password hash */ if ((aux = crypt_rn(pass, salt, res, BCRYPT_HASHSIZE)) == NULL) { *is_null = 1; return 0; } *len = strlen(res); return res; }