<commit_before>/**
 * Node Native Module for Lib Sodium
 *
 * @Author <NAME>
 * @email paixaop at gmail dot com
 * @License MIT
 */
#include "node_sodium.h"

/* int crypto_pwhash_argon2i(unsigned char * const out,
                          unsigned long long outlen,
                          const char * const passwd,
                          unsigned long long passwdlen,
                          const unsigned char * const salt,
                          unsigned long long opslimit, size_t memlimit,
                          int alg)
*/
Napi::Value bind_crypto_pwhash_argon2i(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    ARGS(6,"arguments must be: output buffer, password buffer, salt buffer, oLimit, memLimit, algorithm");

    ARG_TO_UCHAR_BUFFER(out);
    ARG_TO_BUFFER_TYPE(passwd, char);
    ARG_TO_UCHAR_BUFFER_LEN(salt, crypto_pwhash_argon2i_SALTBYTES);
    ARG_TO_NUMBER(oppLimit);
    ARG_TO_NUMBER(memLimit);
    ARG_TO_NUMBER(alg);

    if (crypto_pwhash_argon2i(out, out_size, passwd, passwd_size, salt, oppLimit, memLimit, alg) == 0) {
        return Napi::Boolean::New(env, true);
    }

    return env.Null();
}

/* int crypto_pwhash_argon2i_str(char out[crypto_pwhash_argon2i_STRBYTES],
                              const char * const passwd,
                              unsigned long long passwdlen,
                              unsigned long long opslimit, size_t memlimit)
*/
Napi::Value bind_crypto_pwhash_argon2i_str(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    ARGS(3,"arguments must be: password buffer, oLimit, memLimit");

    ARG_TO_BUFFER_TYPE(passwd, char);
    ARG_TO_NUMBER(oppLimit);
    ARG_TO_NUMBER(memLimit);

    NEW_BUFFER_AND_PTR(buf, crypto_pwhash_argon2i_STRBYTES);
    buf_ptr++; 
    char *out_ptr = (char *) buf.Data();

    if (crypto_pwhash_argon2i_str(out_ptr, passwd, passwd_size, oppLimit, memLimit) == 0) {
        return Napi::String::New(env, out_ptr);
    }

    return Napi::Boolean::New(env, false);
}

/* int crypto_pwhash_argon2i_str_verify(const char str[crypto_pwhash_argon2i_STRBYTES],
                                     const char * const passwd,
                                     unsigned long long passwdlen)
*/
Napi::Value bind_crypto_pwhash_argon2i_str_verify(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    ARGS(2,"arguments must be: pwhash string, password");

    ARG_TO_STRING(hash);
    ARG_TO_BUFFER_TYPE(passwd, char);

    const char *hash_ptr = hash.Utf8Value().c_str();
    if (crypto_pwhash_argon2i_str_verify(hash_ptr, passwd, passwd_size) == 0) {
        return Napi::Boolean::New(env, true);
    }

    return Napi::Boolean::New(env, false);
}

/*
    int crypto_pwhash(unsigned char * const out, unsigned long long outlen,
                  const char * const passwd, unsigned long long passwdlen,
                  const unsigned char * const salt,
                  unsigned long long opslimit, size_t memlimit, int alg)

    Buffer out
    Buffer password
    Buffer salt
    Number oppsLimit
    Number memLimit
    Number algorithm

*/
Napi::Value bind_crypto_pwhash(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    ARGS(6,"arguments must be: output buffer, password buffer, salt buffer, oLimit, memLimit, algorithm");

    ARG_TO_BUFFER_TYPE(out, unsigned char);
    ARG_TO_BUFFER_TYPE(passwd, char);
    ARG_TO_UCHAR_BUFFER_LEN(salt, crypto_pwhash_SALTBYTES);
    ARG_TO_NUMBER(oppLimit);<commit_msg>fix hash str and verify to return buffers<commit_after>/**
 * Node Native Module for Lib Sodium
 *
 * @Author <NAME>
 * @email <EMAIL> at g<EMAIL> dot com
 * @License MIT
 */
#include "node_sodium.h"

/* int crypto_pwhash_argon2i(unsigned char * const out,
                          unsigned long long outlen,
                          const char * const passwd,
                          unsigned long long passwdlen,
                          const unsigned char * const salt,
                          unsigned long long opslimit, size_t memlimit,
                          int alg)
*/
Napi::Value bind_crypto_pwhash_argon2i(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    ARGS(6,"arguments must be: output buffer, password buffer, salt buffer, oLimit, memLimit, algorithm");

    ARG_TO_UCHAR_BUFFER(out);
    ARG_TO_BUFFER_TYPE(passwd, char);
    ARG_TO_UCHAR_BUFFER_LEN(salt, crypto_pwhash_argon2i_SALTBYTES);
    ARG_TO_NUMBER(oppLimit);
    ARG_TO_NUMBER(memLimit);
    ARG_TO_NUMBER(alg);

    if (crypto_pwhash_argon2i(out, out_size, passwd, passwd_size, salt, oppLimit, memLimit, alg) == 0) {
        return Napi::Boolean::New(env, true);
    }

    return env.Null();
}

/* int crypto_pwhash_argon2i_str(char out[crypto_pwhash_argon2i_STRBYTES],
                              const char * const passwd,
                              unsigned long long passwdlen,
                              unsigned long long opslimit, size_t memlimit)
*/
Napi::Value bind_crypto_pwhash_argon2i_str(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    ARGS(3,"arguments must be: password buffer, oLimit, memLimit");

    ARG_TO_BUFFER_TYPE(passwd, char);
    ARG_TO_NUMBER(oppLimit);
    ARG_TO_NUMBER(memLimit);

    NEW_BUFFER_AND_PTR(buf, crypto_pwhash_argon2i_STRBYTES);
    buf_ptr++; 
    char *out_ptr = (char *) buf.Data();

    if (crypto_pwhash_argon2i_str(out_ptr, passwd, passwd_size, oppLimit, memLimit) == 0) {
        return buf;
    }

    return Napi::Boolean::New(env, false);
}

/* int crypto_pwhash_argon2i_str_verify(const char str[crypto_pwhash_argon2i_STRBYTES],
                                     const char * const passwd,
                                     unsigned long long passwdlen)
*/
Napi::Value bind_crypto_pwhash_argon2i_str_verify(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    ARGS(2,"arguments must be: pwhash string, password");

    ARG_TO_BUFFER_TYPE(hash, char);
    ARG_TO_BUFFER_TYPE(passwd, char);

    if (crypto_pwhash_argon2i_str_verify(hash, passwd, passwd_size) == 0) {
        return Napi::Boolean::New(env, true);
    }

    return Napi::Boolean::New(env, false);
}

/*
    int crypto_pwhash(unsigned char * const out, unsigned long long outlen,
                  const char * const passwd, unsigned long long passwdlen,
                  const unsigned char * const salt,
                  unsigned long long opslimit, size_t memlimit, int alg)

    Buffer out
    Buffer password
    Buffer salt
    Number oppsLimit
    Number memLimit
    Number algorithm

*/
Napi::Value bind_crypto_pwhash(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    ARGS(6,"arguments must be: output buffer, password buffer, salt buffer, oLimit, memLimit, algorithm");

    ARG_TO_BUFFER_TYPE(out, unsigned char);
    ARG_TO_BUFFER_TYPE(passwd, char);
    ARG_TO_UCHAR_BUFFER_LEN(salt, crypto_pwhash_SALTBYTES);
    ARG_TO_NUMBER(oppLimit);
{'fraction_non_alphanumeric': 0.09613450208363271, 'fraction_numerical': 0.005891651099295876, 'mean_word_length': 2.79291553133515, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3353367', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2219, 'n_words': 518}