Sodium::Digest::Blake2b Add #hexfinal(dst).

Better specs.
master
Didactic Drunk 2020-06-28 16:30:17 -07:00
parent 55b7acbbb9
commit f1a225b03b
4 changed files with 54 additions and 1 deletions

View File

@ -89,8 +89,45 @@ describe Sodium::Digest::Blake2b do
expect_raises ArgumentError do
Sodium::Digest::Blake2b.new personal: Bytes.new(128)
end
d = Sodium::Digest::Blake2b.new
expect_raises ArgumentError do
d.hexfinal Bytes.new(1)
end
expect_raises ArgumentError do
d.hexfinal Bytes.new(256)
end
end
pending "dups" do
it "can't final twice or update after final" do
d = Sodium::Digest::Blake2b.new
d.hexfinal
expect_raises Digest::FinalizedError do
d.hexfinal
end
expect_raises Digest::FinalizedError do
d.update Bytes.new(0)
end
end
it "dups" do
data = "foo".to_slice
d1 = Sodium::Digest::Blake2b.new
d2 = d1.dup
d3 = d2.dup
d1.update data
h1 = d1.hexfinal
d2.update data
h2 = d2.hexfinal
d3.update data
h3 = d3.hexfinal
h1.should eq h2
h1.should eq h3
end
end

View File

@ -1,3 +1,4 @@
require "log"
require "spec"
# require "../src/sodium"

View File

@ -19,6 +19,8 @@ module Sodium::Digest
class Blake2b < ::Digest::Base
include Wipe
Log = ::Log.for self
# 32
KEY_SIZE = LibSodium.crypto_generichash_blake2b_keybytes.to_i
# 16
@ -84,6 +86,18 @@ module Sodium::Digest
def hexfinal : String
final.hexstring
end
def hexfinal(dst : Bytes) : Nil
dsize = digest_size
unless dst.bytesize == dsize * 2
raise ArgumentError.new("Incorrect dst size: #{dst.bytesize}, expected: #{dsize * 2}")
end
sary = uninitialized StaticArray(UInt8, 64)
tmp = sary.to_slice[0, dsize]
final tmp
tmp.hexstring dst
end
{% end %}
# Compatibility with Crystal <= 0.32

View File

@ -1,3 +1,4 @@
require "log"
require "random/secure"
require "./error"