class Argon2::Engine
def self.hash_argon2i(password, salt, t_cost, m_cost)
The engine class shields users from the FFI interface.
def self.hash_argon2i(password, salt, t_cost, m_cost) result = '' FFI::MemoryPointer.new(:char, Constants::OUT_LEN) do |buffer| ret = Ext.hash_argon2i(buffer, Constants::OUT_LEN, password, password.length, salt, salt.length, t_cost, (1<<m_cost)) raise ArgonHashFail.new(ERRORS[ret]) unless ret == 0 result = buffer.read_string(Constants::OUT_LEN) end result.unpack('H*').join end
def self.hash_argon2i_encode(password, salt, t_cost, m_cost)
def self.hash_argon2i_encode(password, salt, t_cost, m_cost) result = '' if salt.length != Constants::SALT_LEN raise ArgonHashFail.new("Invalid salt size") end FFI::MemoryPointer.new(:char, 300) do |buffer| ret = Ext.argon2_wrap(buffer, password, salt, t_cost, (1<<m_cost), 1) raise ArgonHashFail.new(ERRORS[ret]) unless ret == 0 result = buffer.read_string(300) end result.gsub("\0", '') end
def self.saltgen
def self.saltgen SecureRandom.random_bytes(Argon2::Constants::SALT_LEN) end