Fix examples.

master
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 "" puts ""
{% for name in %w(OPSLIMIT_MIN OPSLIMIT_INTERACTIVE OPSLIMIT_MODERATE OPSLIMIT_SENSITIVE OPSLIMIT_MAX) %} {% 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 %} {% end %}
puts "" puts ""
{% for name in %w(MEMLIMIT_MIN MEMLIMIT_INTERACTIVE MEMLIMIT_MAX) %} {% 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 %} {% end %}
puts "" puts ""
{% for name in %w(SALT_SIZE STR_SIZE) %} {% 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 %} {% end %}
puts "" puts ""

View File

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

View File

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