Fix examples.

This commit is contained in:
Didactic Drunk 2019-09-16 02:35:38 -07:00
parent c470ef8865
commit 3c345f7be8
3 changed files with 23 additions and 17 deletions

View File

@ -23,17 +23,17 @@ puts ""
puts ""
{% for name in %w(OPSLIMIT_MIN OPSLIMIT_INTERACTIVE OPSLIMIT_MODERATE OPSLIMIT_SENSITIVE OPSLIMIT_MAX) %}
puts "Sodium::Pwhash::{{ name.id }} #{Sodium::Pwhash::{{ name.id }}}"
puts "Sodium::Password::{{ name.id }} #{Sodium::Pwhash::{{ name.id }}}"
{% end %}
puts ""
{% for name in %w(MEMLIMIT_MIN MEMLIMIT_INTERACTIVE MEMLIMIT_MAX) %}
puts "Sodium::Pwhash::{{ name.id }} #{Sodium::Pwhash::{{ name.id }}}"
puts "Sodium::Password::{{ name.id }} #{Sodium::Pwhash::{{ name.id }}}"
{% end %}
puts ""
{% for name in %w(SALT_SIZE STR_SIZE) %}
puts "Sodium::Pwhash::{{ name.id }} #{Sodium::Pwhash::{{ name.id }}}"
puts "Sodium::Password::{{ name.id }} #{Sodium::Pwhash::{{ name.id }}}"
{% end %}
puts ""

View File

@ -1,7 +1,7 @@
require "../src/sodium"
if ARGV.empty?
puts "Help select Pwhash ops/mem limits for your application."
puts "Help select Password ops/mem limits for your application."
puts "Usage: #{PROGRAM_NAME} time_min [time_max] [mem_max]"
puts "\ttime is in seconds"
puts "\tmem is in bytes"
@ -14,9 +14,10 @@ time_limit = if t = ARGV.shift?
else
time_min * 4
end
mem_limit = (ARGV.shift?.try &.to_i || (Sodium::Pwhash::MEMLIMIT_MAX)).to_u64
pwhash = Sodium::Pwhash.new
mem_limit = (ARGV.shift?.try &.to_i || (Sodium::Password::MEMLIMIT_MAX)).to_u64
pwhash = Sodium::Password::Key.new
pass = "1234"
salt = pwhash.random_salt!
# data = Array(Array({UInt64, UInt64, Float64})).new
header = [" "]
@ -35,20 +36,20 @@ def bytes_str(b)
"%5d#{suffix}" % b
end
pwhash.memlimit = Sodium::Pwhash::MEMLIMIT_MIN
pwhash.mem = Sodium::Password::MEMLIMIT_MIN
loop do
pwhash.opslimit = Sodium::Pwhash::OPSLIMIT_MIN
row = ["%5dK" % (pwhash.memlimit / 1024)]
pwhash.ops = Sodium::Password::OPSLIMIT_MIN
row = ["%5dK" % (pwhash.mem / 1024)]
data << row
loop do
# p pwhash
t = Time.measure { pwhash.create pass }.to_f
ostr = "%7d" % pwhash.opslimit
t = Time.measure { pwhash.derive_key pass, 32 }.to_f
ostr = "%7d" % pwhash.ops
header << ostr if data.size == 2
if t >= time_min
mstr = bytes_str pwhash.memlimit
# mstr = "%5dK" % (pwhash.memlimit / 1024)
mstr = bytes_str pwhash.mem
# mstr = "%5dK" % (pwhash.mem / 1024)
tstr = "%6.3fs" % t
row << tstr
s = String.build do |sb|
@ -65,14 +66,14 @@ loop do
end
break if t >= time_limit
pwhash.opslimit *= 4
pwhash.ops *= 4
end
row << "" # puts | on the end
puts ""
break if pwhash.memlimit >= mem_limit
break if pwhash.opslimit == Sodium::Pwhash::OPSLIMIT_MIN # Couldn't get past 1 iteration before going over time.
pwhash.memlimit *= 4
break if pwhash.mem >= mem_limit
break if pwhash.ops == Sodium::Password::OPSLIMIT_MIN # Couldn't get past 1 iteration before going over time.
pwhash.mem *= 4
end
# header << "Ops limit"
data << ["Memory"]

View File

@ -41,5 +41,10 @@ module Sodium::Password
def to_params(*, salt = nil, key_size = nil, tcost : Float64? = nil)
Params.new @mode, @ops, @mem, salt: salt, key_size: key_size, tcost: tcost
end
def random_salt!
raise "salt already set" if @salt
self.salt = random_salt
end
end
end