Sha256: 690365a07e33f7a39cd48a47f393973ff73df149d5a3514d0ec8ab64817acf37

Contents?: true

Size: 1.81 KB

Versions: 10

Compression:

Stored size: 1.81 KB

Contents

#include <ruby.h>
#include <stdint.h>

#include "argon2.h"

#define UNUSED(x) (void)(x)

VALUE mArgon2id, cArgon2idError;

static VALUE
rb_argon2id_hash_encoded(VALUE module, VALUE iterations, VALUE memory, VALUE threads, VALUE pwd, VALUE salt, VALUE hashlen)
{
  uint32_t t_cost, m_cost, parallelism;
  size_t encodedlen, outlen;
  char * encoded;
  int result;
  VALUE hash;

  UNUSED(module);

  t_cost = FIX2INT(iterations);
  m_cost = FIX2INT(memory);
  parallelism = FIX2INT(threads);
  outlen = FIX2INT(hashlen);

  encodedlen = argon2_encodedlen(t_cost, m_cost, parallelism, (uint32_t)RSTRING_LEN(salt), (uint32_t)outlen, Argon2_id);
  encoded = malloc(encodedlen + 1);
  if (!encoded) {
    rb_raise(rb_eNoMemError, "not enough memory to allocate for encoded password");
  }

  result = argon2id_hash_encoded(t_cost, m_cost, parallelism, StringValuePtr(pwd), RSTRING_LEN(pwd), StringValuePtr(salt), RSTRING_LEN(salt), outlen, encoded, encodedlen);

  if (result != ARGON2_OK) {
    free(encoded);
    rb_raise(cArgon2idError, "%s", argon2_error_message(result));
  }

  hash = rb_str_new(encoded, strlen(encoded));
  free(encoded);

  return hash;
}

static VALUE
rb_argon2id_verify(VALUE module, VALUE encoded, VALUE pwd) {
  int result;

  UNUSED(module);

  result = argon2id_verify(StringValuePtr(encoded), StringValuePtr(pwd), RSTRING_LEN(pwd));
  if (result == ARGON2_OK) {
    return Qtrue;
  }
  if (result == ARGON2_VERIFY_MISMATCH) {
    return Qfalse;
  }

  rb_raise(cArgon2idError, "%s", argon2_error_message(result));
}

void
Init_argon2id(void)
{
  mArgon2id = rb_define_module("Argon2id");
  cArgon2idError = rb_define_class_under(mArgon2id, "Error", rb_eStandardError);
  rb_define_singleton_method(mArgon2id, "hash_encoded", rb_argon2id_hash_encoded, 6);
  rb_define_singleton_method(mArgon2id, "verify", rb_argon2id_verify, 2);
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
argon2id-0.1.0 ext/argon2id/argon2id.c
argon2id-0.1.0-x86_64-linux ext/argon2id/argon2id.c
argon2id-0.1.0-x86_64-darwin ext/argon2id/argon2id.c
argon2id-0.1.0-x86-mingw32 ext/argon2id/argon2id.c
argon2id-0.1.0-x86-linux ext/argon2id/argon2id.c
argon2id-0.1.0-x64-mingw32 ext/argon2id/argon2id.c
argon2id-0.1.0-x64-mingw-ucrt ext/argon2id/argon2id.c
argon2id-0.1.0-arm64-darwin ext/argon2id/argon2id.c
argon2id-0.1.0-arm-linux ext/argon2id/argon2id.c
argon2id-0.1.0-aarch64-linux ext/argon2id/argon2id.c