From da188875c4395a0b1509c0a06e8d070d0b6969f0 Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Sat, 18 Jul 2020 22:49:13 -0700 Subject: [PATCH] Generate Sodium::Sign::SecretKey#seed. --- spec/sodium/sign/secret_key_spec.cr | 2 +- src/sodium/sign/secret_key.cr | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/spec/sodium/sign/secret_key_spec.cr b/spec/sodium/sign/secret_key_spec.cr index 4af7cf6..563b3a3 100644 --- a/spec/sodium/sign/secret_key_spec.cr +++ b/spec/sodium/sign/secret_key_spec.cr @@ -49,7 +49,7 @@ describe Sodium::Sign::SecretKey do it "seed keys" do seed = Bytes.new Sodium::Sign::SecretKey::SEED_SIZE key1 = Sodium::Sign::SecretKey.new seed: seed - key2 = Sodium::Sign::SecretKey.new seed: seed + key2 = Sodium::Sign::SecretKey.new seed: Sodium::Sign::SecretKey.new(key1.to_slice).seed key1.to_slice.should eq key2.to_slice key1.public_key.to_slice.should eq key2.public_key.to_slice key1.seed.should eq seed diff --git a/src/sodium/sign/secret_key.cr b/src/sodium/sign/secret_key.cr index 0891218..be33f8d 100644 --- a/src/sodium/sign/secret_key.cr +++ b/src/sodium/sign/secret_key.cr @@ -23,8 +23,6 @@ module Sodium # Returns key delegate_to_slice to: @sbuf - @seed : SecureBuffer? - # Generates a new random secret/public key pair. def initialize @sbuf = SecureBuffer.new KEY_SIZE @@ -70,9 +68,12 @@ module Sodium end end - def seed - # BUG: Generate seed if not set. - @seed.not_nil!.to_slice + getter seed : SecureBuffer? do + SecureBuffer.new(SEED_SIZE).tap do |s| + if LibSodium.crypto_sign_ed25519_sk_to_seed(s.to_slice, self.to_slice) != 0 + raise Sodium::Error.new("crypto_sign_ed25519_sk_to_seed") + end + end.readonly end # Signs message and returns a detached signature.