| 1 |
# encoding: binary |
| 2 |
# frozen_string_literal: true |
| 3 |
|
| 4 |
RSpec.describe RbNaCl::VerifyKey do |
| 5 |
let(:signing_key) { vector :sign_private } |
| 6 |
let(:verify_key) { vector :sign_public } |
| 7 |
|
| 8 |
let(:message) { vector :sign_message } |
| 9 |
let(:signature) { vector :sign_signature } |
| 10 |
let(:curve25519_public_key) { vector :sign_curve25519_public } |
| 11 |
|
| 12 |
let(:bad_signature) do |
| 13 |
sig = signature.dup |
| 14 |
sig[0] = (sig[0].ord + 1).chr |
| 15 |
sig |
| 16 |
end |
| 17 |
|
| 18 |
subject { RbNaCl::SigningKey.new(signing_key).verify_key } |
| 19 |
|
| 20 |
it "verifies correct signatures" do |
| 21 |
expect(subject.verify(signature, message)).to eq true |
| 22 |
end |
| 23 |
|
| 24 |
it "verifies correct signatures, full version" do |
| 25 |
expect(subject.verify_attached(signature + message)).to eq true |
| 26 |
end |
| 27 |
|
| 28 |
it "raises when asked to verify a bad signature" do |
| 29 |
expect { subject.verify(bad_signature, message) }.to raise_exception RbNaCl::BadSignatureError |
| 30 |
end |
| 31 |
|
| 32 |
it "raises when asked to verify a bad signature, full version" do |
| 33 |
expect { subject.verify_attached(bad_signature + message) }.to raise_exception RbNaCl::BadSignatureError |
| 34 |
end |
| 35 |
|
| 36 |
it "raises when asked to verify a short signature" do |
| 37 |
expect { subject.verify(bad_signature[0, 63], message) }.to raise_exception RbNaCl::LengthError |
| 38 |
end |
| 39 |
|
| 40 |
it "raises when asked to verify a nil signed message" do |
| 41 |
expect { subject.verify_attached(nil) }.to raise_exception RbNaCl::LengthError |
| 42 |
end |
| 43 |
|
| 44 |
it "raises when asked to verify too short signed message" do |
| 45 |
expect { subject.verify_attached(signature) }.to raise_exception RbNaCl::LengthError |
| 46 |
end |
| 47 |
|
| 48 |
it "serializes to bytes" do |
| 49 |
expect(subject.to_bytes).to eq verify_key |
| 50 |
end |
| 51 |
|
| 52 |
it "initializes from bytes" do |
| 53 |
expect(described_class.new(verify_key).to_s).to eq verify_key |
| 54 |
end |
| 55 |
|
| 56 |
it "can be converted to curve25519 public key" do |
| 57 |
pk = subject.to_curve25519_public_key |
| 58 |
expect(pk).to be_a_kind_of RbNaCl::PublicKey |
| 59 |
expect(pk.to_s).to eq curve25519_public_key |
| 60 |
end |
| 61 |
|
| 62 |
include_examples "key equality" do |
| 63 |
let(:key_bytes) { verify_key } |
| 64 |
let(:key) { described_class.new(verify_key) } |
| 65 |
let(:other_key) { described_class.new("B" * 32) } |
| 66 |
end |
| 67 |
|
| 68 |
include_examples "serializable" |
| 69 |
end |