Class: Argon2::Engine

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

Class Method Summary collapse

Class Method Details

.hash_argon2i(password, salt, t_cost, m_cost) ⇒ Object

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



24
25
26
27
28
29
30
31
32
# File 'lib/argon2/ffi_engine.rb', line 24

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

.hash_argon2i_encode(password, salt, t_cost, m_cost) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/argon2/ffi_engine.rb', line 34

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

.saltgenObject



5
6
7
# File 'lib/argon2/engine.rb', line 5

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