Module: Argon2id

Defined in:
lib/argon2id.rb,
lib/argon2id/version.rb,
lib/argon2id/password.rb,
ext/argon2id/argon2id.c

Defined Under Namespace

Classes: Error, Password

Constant Summary collapse

DEFAULT_T_COST =

The default “time cost” of 2 iterations recommended by OWASP.

2
DEFAULT_M_COST =

The default “memory cost” of 19 mebibytes recommended by OWASP.

19_456
DEFAULT_PARALLELISM =

The default 1 thread and compute lane recommended by OWASP.

1
DEFAULT_SALT_LEN =

The default salt length of 16 bytes.

16
DEFAULT_OUTPUT_LEN =

The default desired hash length of 32 bytes.

32
VERSION =
"0.3.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.m_costObject

The default memory cost in kibibytes used by Argon2id::Password.create



40
41
42
# File 'lib/argon2id.rb', line 40

def m_cost
  @m_cost
end

.output_lenObject

The default desired length of the hash in bytes used by Argon2id::Password.create



49
50
51
# File 'lib/argon2id.rb', line 49

def output_len
  @output_len
end

.parallelismObject

The default number of threads and compute lanes used by Argon2id::Password.create



43
44
45
# File 'lib/argon2id.rb', line 43

def parallelism
  @parallelism
end

.salt_lenObject

The default salt size in bytes used by Argon2id::Password.create



46
47
48
# File 'lib/argon2id.rb', line 46

def salt_len
  @salt_len
end

.t_costObject

The default number of iterations used by Argon2id::Password.create



37
38
39
# File 'lib/argon2id.rb', line 37

def t_cost
  @t_cost
end

Class Method Details

.hash_encoded(t_cost, m_cost, parallelism, pwd, salt, output_len) ⇒ Object

Hashes a password with Argon2id, producing an encoded hash.

  • t_cost: number of iterations

  • m_cost: sets memory usage to m_cost kibibytes

  • parallelism: number of threads and compute lanes

  • pwd: the password

  • salt: the salt

  • output_len: desired length of the hash in bytes



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ext/argon2id/argon2id.c', line 21

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);
  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_cstr(encoded);
  free(encoded);

  return hash;
}

.verify(encoded, pwd) ⇒ Object

Verifies a password against an encoded string.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'ext/argon2id/argon2id.c', line 60

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

  UNUSED(module);

  result = argon2id_verify(StringValueCStr(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));
}