static int process_encode_salt(ETERM *pid, ETERM *data) { int retval = 0; ETERM *pattern, *cslt, *lr; byte *csalt = NULL; long log_rounds = -1; int csaltlen = -1; char ret[64]; pattern = erl_format("{Csalt, LogRounds}"); if (erl_match(pattern, data)) { cslt = erl_var_content(pattern, "Csalt"); csaltlen = ERL_BIN_SIZE(cslt); csalt = ERL_BIN_PTR(cslt); lr = erl_var_content(pattern, "LogRounds"); log_rounds = ERL_INT_UVALUE(lr); if (16 != csaltlen) { retval = process_reply(pid, CMD_SALT, "Invalid salt length"); } else if (log_rounds < 4 || log_rounds > 31) { retval = process_reply(pid, CMD_SALT, "Invalid number of rounds"); } else { encode_salt(ret, (u_int8_t*)csalt, csaltlen, log_rounds); retval = process_reply(pid, CMD_SALT, ret); } erl_free_term(cslt); erl_free_term(lr); }; erl_free_term(pattern); return retval; }
/* Wrapper for R */ SEXP R_encode_salt(SEXP csalt_, SEXP log_rounds_){ if(TYPEOF(csalt_) != RAWSXP) error("Argument csalt must be raw vector of length 16"); if(!isInteger(log_rounds_)) error("Argument log_rounds must be integer"); char ret[64]; Rbyte *csalt = RAW(csalt_); int csaltlen = LENGTH(csalt_); int log_rounds = asInteger(log_rounds_); if (csaltlen != 16) error("Invalid salt length"); if (log_rounds < 4 || log_rounds > 31) error("Invalid number of rounds"); encode_salt(ret, csalt, csaltlen, log_rounds); return mkString(ret); }
static PyObject * bcrypt_encode_salt(PyObject *self, PyObject *args, PyObject *kw_args) { static char *keywords[] = { "csalt", "log_rounds", NULL }; char *csalt = NULL; int csaltlen = -1; long log_rounds = -1; char ret[64]; if (!PyArg_ParseTupleAndKeywords(args, kw_args, "s#l:encode_salt", keywords, &csalt, &csaltlen, &log_rounds)) return NULL; if (csaltlen != 16) { PyErr_SetString(PyExc_ValueError, "Invalid salt length"); return NULL; } if (log_rounds < 4 || log_rounds > 31) { PyErr_SetString(PyExc_ValueError, "Invalid number of rounds"); return NULL; } encode_salt(ret, csalt, csaltlen, log_rounds); return PyString_FromString(ret); }
static ERL_NIF_TERM erl_encode_salt(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifBinary csalt, bin; unsigned long log_rounds; if (!enif_inspect_binary(env, argv[0], &csalt) || 16 != csalt.size) { return enif_make_badarg(env); } if (!enif_get_ulong(env, argv[1], &log_rounds)) { enif_release_binary(env, &csalt); return enif_make_badarg(env); } if (!enif_alloc_binary(env, 64, &bin)) { enif_release_binary(env, &csalt); return enif_make_badarg(env); } encode_salt((char *)bin.data, (u_int8_t*)csalt.data, csalt.size, log_rounds); enif_release_binary(env, &csalt); return enif_make_string(env, (char *)bin.data, ERL_NIF_LATIN1); }