Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

52 lines (39 sloc) 1.781 kb
# encoding: binary
require 'spec_helper'
shared_examples "box" do
let(:nonce) { hex2bytes(Crypto::TestVectors[:box_nonce]) }
let(:invalid_nonce) { nonce[0,12] } # too short!
let(:invalid_nonce_long) { nonce + nonce } # too long!
let(:message) { hex2bytes(Crypto::TestVectors[:box_message]) }
let(:ciphertext) { hex2bytes(Crypto::TestVectors[:box_ciphertext]) }
let (:nonce_error_regex) { /Nonce.*(Expected #{box.nonce_bytes})/ }
let(:corrupt_ciphertext) { ciphertext[80] = " " } # picked at random by fair diceroll
context "box" do
it "encrypts a message" do
box.box(nonce, message).should eq ciphertext
end
it "raises on a short nonce" do
expect { box.box(invalid_nonce, message) }.to raise_error(Crypto::LengthError, nonce_error_regex)
end
it "raises on a long nonce" do
expect { box.box(invalid_nonce_long, message) }.to raise_error(Crypto::LengthError, nonce_error_regex)
end
end
context "open" do
it "decrypts a message" do
box.open(nonce, ciphertext).should eq message
end
it "raises on a truncated message to decrypt" do
expect { box.open(nonce, ciphertext[0, 64]) }.to raise_error(Crypto::CryptoError, /Decryption failed. Ciphertext failed verification./)
end
it "raises on a corrupt ciphertext" do
expect { box.open(nonce, corrupt_ciphertext) }.to raise_error(Crypto::CryptoError, /Decryption failed. Ciphertext failed verification./)
end
it "raises on a short nonce" do
expect { box.open(invalid_nonce, message) }.to raise_error(Crypto::LengthError, nonce_error_regex)
end
it "raises on a long nonce" do
expect { box.open(invalid_nonce_long, message) }.to raise_error(Crypto::LengthError, nonce_error_regex)
end
end
end
Jump to Line
Something went wrong with that request. Please try again.