File: sealed_box.rb

package info (click to toggle)
ruby-rbnacl 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 700 kB
  • sloc: ruby: 2,884; makefile: 3
file content (29 lines) | stat: -rw-r--r-- 856 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# encoding: binary
# frozen_string_literal: true

RSpec.shared_examples "sealed_box" do
  let(:message)    { vector :box_message }
  let(:ciphertext) { vector :box_ciphertext }
  let(:corrupt_ciphertext) { ciphertext[80] = " " } # picked at random by fair diceroll

  context "box" do
    it "roundtrips" do
      ciphertext = box.box(message)
      expect(box.open(ciphertext)).to eq message
    end
  end

  context "open" do
    it "raises on a truncated message to decrypt" do
      expect do
        box.open(ciphertext[0, 64])
      end.to raise_error(RbNaCl::CryptoError, /Decryption failed. Ciphertext failed verification./)
    end

    it "raises on a corrupt ciphertext" do
      expect do
        box.open(corrupt_ciphertext)
      end.to raise_error(RbNaCl::CryptoError, /Decryption failed. Ciphertext failed verification./)
    end
  end
end