From 3c345f7be8cd24a236aaa52b89255afa230866d0 Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Mon, 16 Sep 2019 02:35:38 -0700 Subject: [PATCH] Fix examples. --- examples/constants.cr | 6 +++--- examples/pwhash_selector.cr | 29 +++++++++++++++-------------- src/sodium/password/key.cr | 5 +++++ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/examples/constants.cr b/examples/constants.cr index 826dafa..9c4e0b4 100644 --- a/examples/constants.cr +++ b/examples/constants.cr @@ -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 "" diff --git a/examples/pwhash_selector.cr b/examples/pwhash_selector.cr index 649cbf1..69ee48b 100644 --- a/examples/pwhash_selector.cr +++ b/examples/pwhash_selector.cr @@ -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"] diff --git a/src/sodium/password/key.cr b/src/sodium/password/key.cr index bca8445..8017c13 100644 --- a/src/sodium/password/key.cr +++ b/src/sodium/password/key.cr @@ -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