2019-06-28 13:32:16 +02:00
|
|
|
require "../../spec_helper"
|
|
|
|
|
2019-06-29 21:44:47 +02:00
|
|
|
private def new_key_bytes
|
|
|
|
Sodium::CryptoBox::SecretKey.new.bytes
|
|
|
|
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
|
|
|
|
key2 = Sodium::CryptoBox::SecretKey.new key1.bytes, key1.public_key.bytes
|
|
|
|
key1.bytes.should eq key2.bytes
|
|
|
|
key1.public_key.bytes.should eq key2.public_key.bytes
|
2019-06-30 02:21:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "recomputes the public_key" do
|
|
|
|
key1 = Sodium::CryptoBox::SecretKey.new
|
|
|
|
key2 = Sodium::CryptoBox::SecretKey.new key1.bytes
|
|
|
|
key1.bytes.should eq key2.bytes
|
|
|
|
key1.public_key.bytes.should eq key2.public_key.bytes
|
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
|
|
|
|
key1.bytes.should eq key2.bytes
|
|
|
|
key1.public_key.bytes.should eq key2.public_key.bytes
|
|
|
|
end
|
|
|
|
|
2019-06-28 13:32:16 +02:00
|
|
|
it "easy encrypt/decrypt" do
|
|
|
|
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
|
|
|
|
nonce, encrypted = 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
|
|
|
|
|
|
|
it "wipes keys" do
|
|
|
|
check_wiped new_key_bytes
|
|
|
|
end
|
2019-06-28 13:32:16 +02:00
|
|
|
end
|