File: 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 (60 lines) | stat: -rw-r--r-- 1,825 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# encoding: binary
# frozen_string_literal: true

RSpec.shared_examples "box" do
  let(:nonce) { vector :box_nonce }
  let(:invalid_nonce) { nonce[0, 12] } # too short!
  let(:invalid_nonce_long) { nonce + nonce } # too long!
  let(:message)    { vector :box_message }
  let(:ciphertext) { vector :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
      expect(box.box(nonce, message)).to eq ciphertext
    end

    it "raises on a short nonce" do
      expect do
        box.box(invalid_nonce, message)
      end.to raise_error(RbNaCl::LengthError, nonce_error_regex)
    end

    it "raises on a long nonce" do
      expect do
        box.box(invalid_nonce_long, message)
      end.to raise_error(RbNaCl::LengthError, nonce_error_regex)
    end
  end

  context "open" do
    it "decrypts a message" do
      expect(box.open(nonce, ciphertext)).to eq message
    end

    it "raises on a truncated message to decrypt" do
      expect do
        box.open(nonce, 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(nonce, corrupt_ciphertext)
      end.to raise_error(RbNaCl::CryptoError, /Decryption failed. Ciphertext failed verification./)
    end

    it "raises on a short nonce" do
      expect do
        box.open(invalid_nonce, message)
      end.to raise_error(RbNaCl::LengthError, nonce_error_regex)
    end

    it "raises on a long nonce" do
      expect do
        box.open(invalid_nonce_long, message)
      end.to raise_error(RbNaCl::LengthError, nonce_error_regex)
    end
  end
end