/
abstract.cr
37 lines (31 loc) · 952 Bytes
/
abstract.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require "../password"
abstract class Sodium::Password::Abstract
property ops = OPSLIMIT_INTERACTIVE
# Specified in bytes.
property mem = MEMLIMIT_INTERACTIVE
# Returns a random salt for use with #derive_key
def random_salt
Random::Secure.random_bytes SALT_SIZE
end
def self.from_params(hash)
pw = self.new
pw.ops = hash["ops"].as(UInt64)
pw.mem = hash["mem"].as(UInt64)
if pw.responds_to?(:mode=) && (mode = hash["mode"]?)
pw.mode = Mode.parse mode.as(String)
end
if pw.responds_to?(:salt=) && (salt = hash["salt"]?)
pw.salt = salt.as(Bytes)
end
if pw.responds_to?(:key_size=) && (key_size = hash["key_size"]?)
pw.key_size = key_size.as(Int32)
end
if pw.responds_to?(:tcost=) && (tcost = hash["tcost"]?)
pw.tcost = tcost.as(Float64)
end
if pw.responds_to?(:verify=) && (verify = hash["verify"]?)
pw.verify = verify.as(Bytes)
end
pw
end
end