sodium.cr/spec/sodium/cipher/chalsa_spec.cr

47 lines
1.3 KiB
Crystal
Raw Normal View History

2019-06-27 22:52:09 +02:00
require "../../spec_helper"
require "../../../src/sodium/cipher/chalsa"
2019-06-27 22:52:09 +02:00
{% for name in %w(XSalsa20 Salsa20 XChaCha20 ChaCha20Ietf ChaCha20) %}
# TODO: verify against test vectors.
describe Sodium::Cipher::{{ name.id }} do
2019-06-27 22:52:09 +02:00
it "xors" do
data = Bytes.new(100)
2019-07-09 01:08:39 +02:00
cipher1 = Sodium::Cipher::{{ name.id }}.random
cipher2 = Sodium::Cipher::{{ name.id }}.new key: cipher1.key, nonce: cipher1.nonce
2019-07-09 01:08:39 +02:00
2019-06-27 22:52:09 +02:00
output = cipher1.update data
2019-07-09 01:08:39 +02:00
output.should_not eq data # Verify encryption did something.
2019-06-27 22:52:09 +02:00
cipher1.update(data).should_not eq output # Verify offset is incremented.
cipher1.final.should eq Bytes.new(0)
cipher2.update(output).should eq data
2019-07-09 01:08:39 +02:00
cipher2.final.should eq Bytes.new(0)
2019-06-27 22:52:09 +02:00
end
2022-05-23 03:32:11 +02:00
it "Random" do
cipher = Sodium::Cipher::{{ name.id }}.random
cipher.random_bytes(8).should_not eq(cipher.random_bytes(8))
r = 3.times.map { cipher.rand(65536) }.sum
r.should_not eq(0)
end
it "dups" do
cipher1 = Sodium::Cipher::{{ name.id }}.new Bytes.new(Sodium::Cipher::{{ name.id }}::KEY_SIZE)
cipher2 = cipher1.dup
key1 = cipher1.key
key2 = cipher2.key
2021-06-17 03:19:30 +02:00
key1.should eq key2
key2.readwrite do |ks|
ks[0] = 1_u8
end
key1.readonly do |ks|
ks[0].should eq 0_u8
end
key1.should_not eq key2
end
2019-06-27 22:52:09 +02:00
end
{% end %}