2019-06-28 13:32:16 +02:00
|
|
|
require "../../spec_helper"
|
2019-07-01 15:24:26 +02:00
|
|
|
require "../../../src/sodium/crypto_box/secret_key"
|
2019-06-28 13:32:16 +02:00
|
|
|
|
2019-06-29 21:44:47 +02:00
|
|
|
private def new_key_bytes
|
2019-07-04 02:56:02 +02:00
|
|
|
Sodium::CryptoBox::SecretKey.new.to_slice
|
2019-06-29 21:44:47 +02:00
|
|
|
end
|
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
describe Sodium::CryptoBox::SecretKey do
|
2019-06-29 21:44:47 +02:00
|
|
|
it "loads keys" do
|
|
|
|
key1 = Sodium::CryptoBox::SecretKey.new
|
2019-07-04 02:56:02 +02:00
|
|
|
key2 = Sodium::CryptoBox::SecretKey.new key1.to_slice, key1.public_key.to_slice
|
|
|
|
key1.to_slice.should eq key2.to_slice
|
|
|
|
key1.public_key.to_slice.should eq key2.public_key.to_slice
|
2019-06-30 02:21:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "recomputes the public_key" do
|
|
|
|
key1 = Sodium::CryptoBox::SecretKey.new
|
2019-07-04 02:56:02 +02:00
|
|
|
key2 = Sodium::CryptoBox::SecretKey.new key1.to_slice
|
|
|
|
key1.to_slice.should eq key2.to_slice
|
|
|
|
key1.public_key.to_slice.should eq key2.public_key.to_slice
|
2019-06-29 21:44:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "seed keys" do
|
|
|
|
seed = Bytes.new Sodium::CryptoBox::SecretKey::SEED_SIZE
|
|
|
|
key1 = Sodium::CryptoBox::SecretKey.new seed: seed
|
|
|
|
key2 = Sodium::CryptoBox::SecretKey.new seed: seed
|
2019-07-04 02:56:02 +02:00
|
|
|
key1.to_slice.should eq key2.to_slice
|
|
|
|
key1.public_key.to_slice.should eq key2.public_key.to_slice
|
2019-06-29 21:44:47 +02:00
|
|
|
end
|
|
|
|
|
2019-06-30 03:19:01 +02:00
|
|
|
it "authenticated easy encrypt/decrypt" do
|
2019-06-28 13:32:16 +02:00
|
|
|
data = "Hello World!"
|
|
|
|
|
|
|
|
# Alice is the sender
|
2019-06-29 01:17:09 +02:00
|
|
|
alice = Sodium::CryptoBox::SecretKey.new
|
2019-06-28 13:32:16 +02:00
|
|
|
|
|
|
|
# Bob is the recipient
|
2019-06-29 01:17:09 +02:00
|
|
|
bob = Sodium::CryptoBox::SecretKey.new
|
2019-06-28 13:32:16 +02:00
|
|
|
|
|
|
|
# Encrypt a message for Bob using his public key, signing it with Alice's
|
|
|
|
# secret key
|
2019-06-29 21:44:47 +02:00
|
|
|
box = alice.box bob.public_key
|
2019-07-01 19:37:45 +02:00
|
|
|
encrypted, nonce = box.encrypt_easy data
|
2019-06-28 13:32:16 +02:00
|
|
|
|
|
|
|
# Decrypt the message using Bob's secret key, and verify its signature against
|
|
|
|
# Alice's public key
|
2019-06-29 21:44:47 +02:00
|
|
|
bob.box alice.public_key do |box|
|
|
|
|
decrypted = box.decrypt_easy encrypted, nonce: nonce
|
2019-06-28 13:32:16 +02:00
|
|
|
|
|
|
|
String.new(decrypted).should eq(data)
|
|
|
|
end
|
|
|
|
end
|
2019-06-29 21:44:47 +02:00
|
|
|
|
2019-06-30 03:19:01 +02:00
|
|
|
it "unauthenticated seal encrypt/decrypt" do
|
|
|
|
data = "foo bar"
|
|
|
|
|
|
|
|
# Bob is the recipient
|
|
|
|
bob = Sodium::CryptoBox::SecretKey.new
|
|
|
|
|
|
|
|
# Encrypt a message for Bob using his public key. No signature.
|
|
|
|
encrypted = bob.public_key.encrypt data
|
|
|
|
|
|
|
|
# Decrypt the message using Bob's secret key.
|
|
|
|
decrypted = bob.decrypt encrypted
|
|
|
|
|
|
|
|
String.new(decrypted).should eq(data)
|
|
|
|
end
|
2019-06-28 13:32:16 +02:00
|
|
|
end
|