<commit_before>#include <node.h>

#include <stdint.h>
#include <cstring>
#include <string>

#include "../argon2/include/argon2.h"
#include "argon2_node.h"

namespace NodeArgon2 {

const auto ENCODED_LEN = 108u;
const auto HASH_LEN = 32u;

HashAsyncWorker::HashAsyncWorker(const std::string& plain,
        const std::string& salt, uint32_t time_cost, uint32_t memory_cost,
        uint32_t parallelism, Argon2_type type):
    Nan::AsyncWorker{nullptr}, plain{plain}, salt{salt}, time_cost{time_cost},
    memory_cost{memory_cost}, parallelism{parallelism}, type{type}, output{}
{ }

void HashAsyncWorker::Execute()
{
    char encoded[ENCODED_LEN];

    auto result = argon2_hash(time_cost, memory_cost, parallelism,
            plain.c_str(), plain.size(), salt.c_str(), salt.size(), nullptr,
            HASH_LEN, encoded, ENCODED_LEN, type);
    if (result != ARGON2_OK) {
        SetErrorMessage(argon2_error_message(result));
        return;
    }

    output = std::string{encoded};
}

void HashAsyncWorker::HandleOKCallback()
{
    using v8::Promise;

    Nan::HandleScope scope;

    auto promise = GetFromPersistent("resolver").As<Promise::Resolver>();
    promise->Resolve(Nan::Encode(output.c_str(), output.size()));
}

void HashAsyncWorker::HandleErrorCallback()
{
    using v8::Exception;
    using v8::Promise;
    using v8::String;

    Nan::HandleScope scope;

    auto promise = GetFromPersistent("resolver").As<Promise::Resolver>();
    auto reason = Nan::New<String>(ErrorMessage()).ToLocalChecked();
    promise->Reject(Exception::Error(reason));
}

NAN_METHOD(Hash) {
    using namespace node;
    using v8::Promise;

    Nan::HandleScope scope;

    if (info.Length() < 6) {
        /* LCOV_EXCL_START */
        Nan::ThrowTypeError("6 arguments expected");
        return;
        /* LCOV_EXCL_STOP */
    }

    Nan::Utf8String plain{info[0]->ToString()};
    auto raw_salt = info[1]->ToObject();
    auto time_cost = info[2]->Uint32Value();
    auto memory_cost = info[3]->Uint32Value();
    auto parallelism = info[4]->Uint32Value();
    argon2_type type = info[5]->BooleanValue() ? Argon2_d : Argon2_i;

    auto salt = std::string(Buffer::Data(raw_salt), Buffer::Length(raw_salt));

    auto worker = new HashAsyncWorker{*plain, salt, time_cost,
        1u << memory_cost, parallelism, type};

    auto resolver = Promise::Resolver::New(info.GetIsolate());
    worker->SaveToPersistent("resolver", resolver);

    Nan::AsyncQueueWorker(worker);
    info.GetReturnValue().Set(resolver->GetPromise());
}

NAN_METHOD(HashSync) {
    using namespace node;

    Nan::HandleScope scope;

    if (info.Length() < 6) {
        /* LCOV_EXCL_START */
        Nan::ThrowTypeError("6 arguments expected");
        info.GetReturnValue().Set(Nan::Undefined());
        return;
        /* LCOV_EXCL_STOP */
    }

    Nan::Utf8String plain{info[0]->ToString()};
    auto raw_salt = info[1]->ToObject();
    auto time_cost = info[2]->Uint32Value();
    auto memory_cost = info[3]->Uint32Value();
    auto parallelism = info[4]->Uint32Value();
    argon2_type type = info[5]->BooleanValue() ? Argon2_d : Argon2_i;

    char encoded[ENCODED_LEN];

    auto salt = std::string(Buffer::Data(raw_salt), Buffer::Length(raw_salt));

    auto result = argon2_hash(time_cost, 1 << memory_cost, parallelism, *plain,
            strlen(*plain), salt.c_str(), salt.size(), nullptr, HASH_LEN,
            encoded, ENCODED_LEN, type);

    if (result != ARGON2_OK) {
        Nan::ThrowError(argon2_error_message(result));
        info.GetReturnValue().Set(Nan::Undefined());
        return;
    }

    info.GetReturnValue().Set(Nan::Encode(encoded, strlen(encoded),
            Nan::BINARY));
}

VerifyAsyncWorker::VerifyAsyncWorker(const std::string& hash,
        const std::string& plain, argon2_type type):
    Nan::AsyncWorker{nullptr}, hash{hash}, plain{plain}, type{type}<commit_msg>Ignore code unexpected to run on lcov report<commit_after>#include <node.h>

#include <stdint.h>
#include <cstring>
#include <string>

#include "../argon2/include/argon2.h"
#include "argon2_node.h"

namespace NodeArgon2 {

const auto ENCODED_LEN = 108u;
const auto HASH_LEN = 32u;

HashAsyncWorker::HashAsyncWorker(const std::string& plain,
        const std::string& salt, uint32_t time_cost, uint32_t memory_cost,
        uint32_t parallelism, Argon2_type type):
    Nan::AsyncWorker{nullptr}, plain{plain}, salt{salt}, time_cost{time_cost},
    memory_cost{memory_cost}, parallelism{parallelism}, type{type}, output{}
{ }

void HashAsyncWorker::Execute()
{
    char encoded[ENCODED_LEN];

    auto result = argon2_hash(time_cost, memory_cost, parallelism,
            plain.c_str(), plain.size(), salt.c_str(), salt.size(), nullptr,
            HASH_LEN, encoded, ENCODED_LEN, type);
    if (result != ARGON2_OK) {
        /* LCOV_EXCL_START */
        SetErrorMessage(argon2_error_message(result));
        return;
        /* LCOV_EXCL_STOP */
    }

    output = std::string{encoded};
}

void HashAsyncWorker::HandleOKCallback()
{
    using v8::Promise;

    Nan::HandleScope scope;

    auto promise = GetFromPersistent("resolver").As<Promise::Resolver>();
    promise->Resolve(Nan::Encode(output.c_str(), output.size()));
}

/* LCOV_EXCL_START */
void HashAsyncWorker::HandleErrorCallback()
{
    using v8::Exception;
    using v8::Promise;
    using v8::String;

    Nan::HandleScope scope;

    auto promise = GetFromPersistent("resolver").As<Promise::Resolver>();
    auto reason = Nan::New<String>(ErrorMessage()).ToLocalChecked();
    promise->Reject(Exception::Error(reason));
}
/* LCOV_EXCL_STOP */

NAN_METHOD(Hash) {
    using namespace node;
    using v8::Promise;

    Nan::HandleScope scope;

    if (info.Length() < 6) {
        /* LCOV_EXCL_START */
        Nan::ThrowTypeError("6 arguments expected");
        return;
        /* LCOV_EXCL_STOP */
    }

    Nan::Utf8String plain{info[0]->ToString()};
    auto raw_salt = info[1]->ToObject();
    auto time_cost = info[2]->Uint32Value();
    auto memory_cost = info[3]->Uint32Value();
    auto parallelism = info[4]->Uint32Value();
    argon2_type type = info[5]->BooleanValue() ? Argon2_d : Argon2_i;

    auto salt = std::string(Buffer::Data(raw_salt), Buffer::Length(raw_salt));

    auto worker = new HashAsyncWorker{*plain, salt, time_cost,
        1u << memory_cost, parallelism, type};

    auto resolver = Promise::Resolver::New(info.GetIsolate());
    worker->SaveToPersistent("resolver", resolver);

    Nan::AsyncQueueWorker(worker);
    info.GetReturnValue().Set(resolver->GetPromise());
}

NAN_METHOD(HashSync) {
    using namespace node;

    Nan::HandleScope scope;

    if (info.Length() < 6) {
        /* LCOV_EXCL_START */
        Nan::ThrowTypeError("6 arguments expected");
        info.GetReturnValue().Set(Nan::Undefined());
        return;
        /* LCOV_EXCL_STOP */
    }

    Nan::Utf8String plain{info[0]->ToString()};
    auto raw_salt = info[1]->ToObject();
    auto time_cost = info[2]->Uint32Value();
    auto memory_cost = info[3]->Uint32Value();
    auto parallelism = info[4]->Uint32Value();
    argon2_type type = info[5]->BooleanValue() ? Argon2_d : Argon2_i;

    char encoded[ENCODED_LEN];

    auto salt = std::string(Buffer::Data(raw_salt), Buffer::Length(raw_salt));

    auto result = argon2_hash(time_cost, 1 << memory_cost, parallelism, *plain,
            strlen(*plain), salt.c_str(), salt.size(), nullptr, HASH_LEN,
            encoded, ENCODED_LEN, type);

    if (result != ARGON2_OK) {
        /* LCOV_EXCL_START */
        Nan::ThrowError(argon2_error_message(result));
        return;
        /* LCOV_EXCL_STOP */
    }

    info.GetReturnValue().Set(Nan::Encode(encoded, strlen(encoded),
            Nan::BINARY));
}

VerifyAsyncWorker::VerifyAsyncWorker(const std::string& hash,
        const std::string& plain, argon2_type type):
    Nan::AsyncWorker{nullptr}, hash{hash}, plain{plain}, type{type}
{'fraction_non_alphanumeric': 0.1495008214330848, 'fraction_numerical': 0.016681410337419437, 'mean_word_length': 3.8285539963392314, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5961448', 'n_tokens_mistral': 2691, 'n_tokens_neox': 2572, 'n_words': 473}