class Sodium::Password::Key::Create

Overview

Argon2 Password Hashing

Create a key for encryption with auto set parameters based on time.

Usage:

pwkc = Sodium::Password::Key::Create.new

# Take approximately 1 second to generate a key.
pwkc.tcost = 1.0

# Memory use will end up between `mem_min` and `mem_max`
pwkc.mem_min = 128 * 1024 * 1024 # 128M
pwkc.mem_max = 256 * 1024 * 1024 # 256M

kdf, params = pwkc.create_kdf pass
# Or
key, params = pwkc.create_key pass, 32

# Save params.[mode, ops, mem, salt, key_size] to derive the same key later.
# Or serialize `params.to_h`

Deriving a previously created key.

Usage:

pwkey = Sodium::Password::Key.from_params hash
# Or
pwkey = Sodium::Password::Key.new
pwkey.mode = Mode.parse serialized[:mode]
pwkey.ops = serialized[:ops]
pwkey.mem = serialized[:mem]
pwkey.salt = serialized[:salt]
key_size = serialized[:key_size]

kdf = pwhash.derive_kdf pass
# Or
key = pwkey.derive_key pass, key_size

Defined in:

sodium/password/create_key.cr

Instance Method Summary

Instance Method Detail

def create_kdf(pass, *, salt : Bytes | String | Nil = nil) : Tuple(Kdf, Params) #

[View source]
def create_key(pass : Bytes | String, key_size, *, salt : Bytes | String | Nil = nil) : Tuple(SecureBuffer, Params) #

Returns a consistent key based on [salt, pass, key_size, mode] in a SecureBuffer and Params.

Params are set to run in approximately #tcost seconds.

Make sure you store Params for later use with #derive_key.


[View source]
def mem_max : UInt64 #

Specified in bytes. Currently defaults to 256M. May increase between version.


[View source]
def mem_max=(mem_max : UInt64) #

Specified in bytes. Currently defaults to 256M. May increase between version.


[View source]
def mem_min : UInt64 #

Specified in bytes. Currently the libsodium default. May increase between version.


[View source]
def mem_min=(mem_min) #

Specified in bytes. Currently the libsodium default. May increase between version.


[View source]
def mode : Mode #

[View source]
def mode=(mode : Mode) #

[View source]
def tcost : Float64 #

Specified in seconds.


[View source]
def tcost=(tcost) #

Specified in seconds.


[View source]