Class: Argon2::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/argon2/engine.rb,
lib/argon2/ffi_engine.rb

Overview

The engine class shields users from the FFI interface. It is generally not advised to directly use this class.

Class Method Summary collapse

Class Method Details

.argon2_verify(pwd, hash, secret) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/argon2/ffi_engine.rb', line 103

def self.argon2_verify(pwd, hash, secret)
  secretlen = secret.nil? ? 0 : secret.bytesize
  passwordlen = pwd.nil? ? 0 : pwd.bytesize

  ret = Ext.wrap_argon2_verify(hash, pwd, passwordlen, secret, secretlen)
  return false if ERRORS[ret.abs] == 'ARGON2_DECODING_FAIL'
  raise ::Argon2::Errors::ExtError, ERRORS[ret.abs] unless ret.zero?

  true
end

.hash_argon2i(password, salt, t_cost, m_cost, out_len = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/argon2/ffi_engine.rb', line 54

def self.hash_argon2i(password, salt, t_cost, m_cost, out_len = nil)
  out_len = (out_len || Constants::OUT_LEN).to_i
  raise ::Argon2::Errors::InvalidOutputLength if out_len < 1

  result = ''
  FFI::MemoryPointer.new(:char, out_len) do |buffer|
    ret = Ext.argon2i_hash_raw(t_cost, 1 << m_cost, 1, password,
                               password.length, salt, salt.length,
                               buffer, out_len)
    raise ::Argon2::Errors::ExtError, ERRORS[ret.abs] unless ret.zero?

    result = buffer.read_string(out_len)
  end
  result.unpack('H*').join
end

.hash_argon2id(password, salt, t_cost, m_cost, p_cost, out_len = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/argon2/ffi_engine.rb', line 70

def self.hash_argon2id(password, salt, t_cost, m_cost, p_cost, out_len = nil)
  out_len = (out_len || Constants::OUT_LEN).to_i
  raise ::Argon2::Errors::InvalidOutputLength if out_len < 1

  result = ''
  FFI::MemoryPointer.new(:char, out_len) do |buffer|
    ret = Ext.argon2id_hash_raw(t_cost, 1 << m_cost, p_cost, password,
                                password.length, salt, salt.length,
                                buffer, out_len)
    raise ::Argon2::Errors::ExtError, ERRORS[ret.abs] unless ret.zero?

    result = buffer.read_string(out_len)
  end
  result.unpack('H*').join
end

.hash_argon2id_encode(password, salt, t_cost, m_cost, p_cost, secret) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/argon2/ffi_engine.rb', line 86

def self.hash_argon2id_encode(password, salt, t_cost, m_cost, p_cost, secret)
  result = ''
  secretlen = secret.nil? ? 0 : secret.bytesize
  passwordlen = password.nil? ? 0 : password.bytesize
  raise ::Argon2::Errors::InvalidSaltSize if salt.length != Constants::SALT_LEN

  FFI::MemoryPointer.new(:char, Constants::ENCODE_LEN) do |buffer|
    ret = Ext.argon2_wrap(buffer, password, passwordlen,
                          salt, salt.length, t_cost, (1 << m_cost),
                          p_cost, secret, secretlen)
    raise ::Argon2::Errors::ExtError, ERRORS[ret.abs] unless ret.zero?

    result = buffer.read_string(Constants::ENCODE_LEN)
  end
  result.delete "\0"
end

.saltgenObject

Generates a random, binary string for use as a salt.



14
15
16
# File 'lib/argon2/engine.rb', line 14

def self.saltgen
  SecureRandom.random_bytes(Argon2::Constants::SALT_LEN)
end