From 8e939b951823a9efd625f5309cc4b1dc9c519b4c Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Fri, 28 Jun 2019 13:58:55 -0700 Subject: [PATCH] API change. Cox::Blake2b renamed to Cox::Digest::Blake2b --- README.md | 24 ++++++++++++------------ benchmarks/blake2b.cr | 6 +++--- spec/cox/blake2b_spec.cr | 28 ++++++++++++++-------------- src/cox/{ => digest}/blake2b.cr | 2 +- 4 files changed, 30 insertions(+), 30 deletions(-) rename src/cox/{ => digest}/blake2b.cr (99%) diff --git a/README.md b/README.md index 5c75f6b..0798f49 100644 --- a/README.md +++ b/README.md @@ -65,16 +65,16 @@ dependencies: | Class | | | --- | --- | | `CryptoBox` `Sign` `SecretBox` | I don't know much about crypto. | -| `Cox::CryptoBox::PrivateKey` | I want to encrypt + authenticate data using public key encryption. | -| `Cox::Sign::PrivateKey` | I want to sign or verify messages without encryption. | -| `Cox::SecretBox` | I have a shared key and want to encrypt + authenticate data. | -| AEAD | I have a shared key and want encrypt + authenticate streamed data. (not implemented yet) | -| `Cox::Blake2b` | I want to hash data fast and securely. | -| `Cox::SipHash` | I want to hash data really fast and less securely. (not implemented yet) | +| [`Cox::CryptoBox::SecretKey`](https://didactic-drunk.github.io/cox/Cox/CryptoBox/SecretKey.html) | I want to encrypt + authenticate data using public key encryption. | +| [`Cox::Sign::SecretKey`](https://didactic-drunk.github.io/cox/Cox/Sign/SecretKey.html) | I want to sign or verify messages without encryption. | +| [`Cox::SecretBox`](https://didactic-drunk.github.io/cox/Cox/SecretBox.html) | I have a shared key and want to encrypt + authenticate data. | +| AEAD | I have a shared key and want encrypt + authenticate streamed data. (Not implemented yet) | +| [`Cox::Digest::Blake2b`](https://didactic-drunk.github.io/cox/Cox/Digest::Blake2b.html) | I want to hash data fast and securely. | +| `Cox::Digest::SipHash` | I want to hash data really fast and less securely. (Not implemented yet) | | `Cox::Pwhash` | I want to hash a password and store it. | | `Cox::Pwhash` | I want to derive a key from a password. | | `Cox::Kdf` | I have a high quality master key and want to make subkeys. | -| `Cox::Cipher::Chalsa` | What goes with guacamole? | +| [`Cox::Cipher::Chalsa`](https://didactic-drunk.github.io/cox/Cox/Cipher/Chalsa.html) | What goes with guacamole? | | Everything else | I want to design my own crypto protocol and probably do it wrong. | @@ -142,14 +142,14 @@ message = key.decrypt_easy encrypted, nonce ### Blake2b ```crystal -key = Bytes.new Cox::Blake2B::KEY_SIZE -salt = Bytes.new Cox::Blake2B::SALT_SIZE -personal = Bytes.new Cox::Blake2B::PERSONAL_SIZE -out_size = 64 # bytes between Cox::Blake2B::OUT_SIZE_MIN and Cox::Blake2B::OUT_SIZE_MAX +key = Bytes.new Cox::Digest::Blake2B::KEY_SIZE +salt = Bytes.new Cox::Digest::Blake2B::SALT_SIZE +personal = Bytes.new Cox::Digest::Blake2B::PERSONAL_SIZE +out_size = 64 # bytes between Cox::Digest::Blake2B::OUT_SIZE_MIN and Cox::Digest::Blake2B::OUT_SIZE_MAX data = "data".to_slice # output_size, key, salt, and personal are optional. -digest = Cox::Blake2b.new out_size, key: key, salt: salt, personal: personal +digest = Cox::Digest::Blake2b.new out_size, key: key, salt: salt, personal: personal digest.update data output = d.hexdigest diff --git a/benchmarks/blake2b.cr b/benchmarks/blake2b.cr index 8b2eed6..566684f 100644 --- a/benchmarks/blake2b.cr +++ b/benchmarks/blake2b.cr @@ -13,19 +13,19 @@ puts "'crystal run --release benchmarks/blake2b.cr sha1 sha256'" Benchmark.ips(warmup: 0.5) do |bm| sizes.each_with_index do |size, i| bm.report "blake2b new obj per iter #{size}" do - d = Cox::Blake2b.new 64 + d = Cox::Digest::Blake2b.new 64 d.update bufs[i] d.digest end - d = Cox::Blake2b.new output_size + d = Cox::Digest::Blake2b.new output_size bm.report "blake2b reset per iter #{size}" do d.reset d.update bufs[i] d.digest end - d = Cox::Blake2b.new output_size + d = Cox::Digest::Blake2b.new output_size dst = Bytes.new d.digest_size bm.report "blake2b reset reusing buffer per iter #{size}" do d.reset diff --git a/spec/cox/blake2b_spec.cr b/spec/cox/blake2b_spec.cr index c17ac05..cff3f3c 100644 --- a/spec/cox/blake2b_spec.cr +++ b/spec/cox/blake2b_spec.cr @@ -23,10 +23,10 @@ test_vectors = [ }, ] -describe Cox::Blake2b do +describe Cox::Digest::Blake2b do it "libsodium comparisons" do libsodium_comparisons.each do |vec| - d = Cox::Blake2b.new vec[:out_size], key: vec[:key].try(&.hexbytes) + d = Cox::Digest::Blake2b.new vec[:out_size], key: vec[:key].try(&.hexbytes) d.update vec[:input].hexbytes d.hexdigest.should eq vec[:output] end @@ -34,31 +34,31 @@ describe Cox::Blake2b do it "test vectors" do test_vectors.each do |vec| - d = Cox::Blake2b.new 64, key: vec[:key].hexbytes + d = Cox::Digest::Blake2b.new 64, key: vec[:key].hexbytes d.update vec[:input].hexbytes d.hexdigest.should eq vec[:output] end end it "produces different output with different salt or personal params" do - key = Bytes.new Cox::Blake2b::KEY_SIZE - salt = Bytes.new Cox::Blake2b::SALT_SIZE - salt2 = Bytes.new Cox::Blake2b::SALT_SIZE + key = Bytes.new Cox::Digest::Blake2b::KEY_SIZE + salt = Bytes.new Cox::Digest::Blake2b::SALT_SIZE + salt2 = Bytes.new Cox::Digest::Blake2b::SALT_SIZE salt2 = salt.dup salt2[0] = 1 - personal = Bytes.new Cox::Blake2b::PERSONAL_SIZE + personal = Bytes.new Cox::Digest::Blake2b::PERSONAL_SIZE personal2 = personal.dup personal2[0] = 1 - d = Cox::Blake2b.new key: key, salt: salt, personal: personal + d = Cox::Digest::Blake2b.new key: key, salt: salt, personal: personal d.update "foo".to_slice output = d.hexdigest - d = Cox::Blake2b.new key: key, salt: salt2, personal: personal + d = Cox::Digest::Blake2b.new key: key, salt: salt2, personal: personal d.update "foo".to_slice saltout = d.hexdigest - d = Cox::Blake2b.new key: key, salt: salt, personal: personal2 + d = Cox::Digest::Blake2b.new key: key, salt: salt, personal: personal2 d.update "foo".to_slice personalout = d.hexdigest @@ -69,19 +69,19 @@ describe Cox::Blake2b do it "raises on invalid " do expect_raises ArgumentError do - Cox::Blake2b.new key: Bytes.new(128) + Cox::Digest::Blake2b.new key: Bytes.new(128) end expect_raises ArgumentError do - Cox::Blake2b.new salt: Bytes.new(1) + Cox::Digest::Blake2b.new salt: Bytes.new(1) end expect_raises ArgumentError do - Cox::Blake2b.new salt: Bytes.new(128) + Cox::Digest::Blake2b.new salt: Bytes.new(128) end expect_raises ArgumentError do - Cox::Blake2b.new personal: Bytes.new(128) + Cox::Digest::Blake2b.new personal: Bytes.new(128) end end end diff --git a/src/cox/blake2b.cr b/src/cox/digest/blake2b.cr similarity index 99% rename from src/cox/blake2b.cr rename to src/cox/digest/blake2b.cr index 3aa5bab..61af3c4 100644 --- a/src/cox/blake2b.cr +++ b/src/cox/digest/blake2b.cr @@ -1,6 +1,6 @@ require "openssl/digest/digest_base" -module Cox +module Cox::Digest class Blake2b # provides copying digest/hexdigest methods include OpenSSL::DigestBase