Class: BlockCipherKit::BaseScheme

Inherits:
Object
  • Object
show all
Defined in:
lib/block_cipher_kit/base_scheme.rb

Instance Method Summary collapse

Instance Method Details

#decrypt_range(from_ciphertext_io:, range:) ⇒ String

Decrypts the desired range of the ciphered message, reading ciphertext out of ‘from_ciphertext_io`. Reading requires the `from_ciphertext_io` to be seekable - it must support `#pos`, `#read`and `#seek`. The decrypted plaintext gets returned as a single concatenated String.

Parameters:

  • from_ciphertext_io (RandomReadIO)

    Ciphertext will be read from that IO. The IO must support random access.

  • range (Range)

    range of bytes in plaintext offsets to decrypt. Endless ranges are supported.

Returns:

  • (String)

    the decrypted bytes located at the given offset range inside the plaintext



61
62
63
64
65
# File 'lib/block_cipher_kit/base_scheme.rb', line 61

def decrypt_range(from_ciphertext_io:, range:)
  buf = StringIO.new.binmode
  streaming_decrypt_range(from_ciphertext_io: from_ciphertext_io, range: range, into_plaintext_io: buf)
  buf.string
end

#inspectObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/block_cipher_kit/base_scheme.rb', line 67

def inspect
  # A reimplementation of #inspect based largely on
  # https://alchemists.io/articles/ruby_object_inspection
  pattern = +""
  values = []

  instance_variables.each do |name|
    pattern << "#{name}=%s "
    ivar_value = instance_variable_get(name)
    if ivar_value.is_a?(String) && key_material_instance_variable_names.include?(name)
      values.push("[SENSITIVE(#{ivar_value.bytesize * 8} bits)]")
    else
      values.push(ivar_value.inspect)
    end
  end

  format "#<%s:%#018x #{pattern.strip}>", self.class, object_id << 1, *values
end

#streaming_decrypt(from_ciphertext_io:, into_plaintext_io: nil) {|String| ... } ⇒ void

This method returns an undefined value.

Decrypts the entire ciphered message, reading ciphertext out of ‘from_ciphertext_io` until its `read` returns `nil` (until EOF is implicitly reached). The scheme will also read any data at the start of the IO that it requires for operation, and consume the IO until exhaustion.

Parameters:

  • from_ciphertext_io (StraightReadableIO)

    An IO-ish that responds to ‘read` with one argument, ciphertext will be read from that IO

  • into_plaintext_io (WritableIO) (defaults to: nil)

    An IO-ish that responds to ‘write` with one argument. If into_plaintext_io is not provided, the block passed to the method will receive String objects in binary encoding with chunks of decrypted ciphertext. The sizing of the chunks is defined by the cipher and the read size used by `IO.copy_stream`

Yields:

  • (String)

    the chunk of decrypted bytes



15
16
17
# File 'lib/block_cipher_kit/base_scheme.rb', line 15

def streaming_decrypt(from_ciphertext_io:, into_plaintext_io: nil, &blk)
  raise "Unimplemented"
end

#streaming_decrypt_range(from_ciphertext_io:, range:, into_plaintext_io: nil) {|String| ... } ⇒ void

This method returns an undefined value.

Decrypts the desired range of the ciphered message, reading ciphertext out of ‘from_ciphertext_io`. Reading requires the `from_ciphertext_io` to be seekable - it must support `#pos`, `#read`and `#seek`. The decrypted plaintext either gets written into `into_plaintext_io` if it is provided, or yielded to the passed block in String chunks.

Parameters:

  • from_ciphertext_io (RandomReadIO)

    Ciphertext will be read from that IO. The IO must support random access.

  • range (Range)

    range of bytes in plaintext offsets to decrypt. Endless ranges are supported.

  • into_plaintext_io (WritableIO) (defaults to: nil)

    An IO-ish that responds to ‘write` with one argument. If into_plaintext_io is not provided, the block passed to the method will receive String objects in binary encoding with chunks of decrypted ciphertext. The sizing of the chunks is defined by the cipher and the read size used by `IO.copy_stream`

Yields:

  • (String)

    the chunk of decrypted bytes



50
51
52
# File 'lib/block_cipher_kit/base_scheme.rb', line 50

def streaming_decrypt_range(from_ciphertext_io:, range:, into_plaintext_io: nil, &blk)
  raise "Unimplemented"
end

#streaming_encrypt(into_ciphertext_io:, from_plaintext_io: nil) {|#write| ... } ⇒ void

This method returns an undefined value.

Encrypts the entire ciphered message, reading plaintext either from the ‘from_plaintext_io` until its `read` returns `nil` (until EOF is implicitly reached) or from writes to the object it yields (for streaming writes).

The scheme will also write any leading data at the start of the output that should prefix the ciphertext (usually the IV) and any trailing data after the ciphertext (like a validation tag for cipher authentication) into the ‘into_ciphertext_io`.

Parameters:

  • from_plaintext_io (StraightReadableIO, nil) (defaults to: nil)

    An IO-ish that responds to ‘read` with one argument. If from_plaintext_io is not provided, the block passed to the method will receive an IO-ish object that responds to `#write` that plaintext can be written into.

  • into_ciphertext_io (WritableIO)

    An IO-ish that responds to ‘write` with one argument,

Yields:

  • (#write)

    IO-ish writable that accepts strings of plaintext into ‘#write`



33
34
35
# File 'lib/block_cipher_kit/base_scheme.rb', line 33

def streaming_encrypt(into_ciphertext_io:, from_plaintext_io: nil, &blk)
  raise "Unimplemented"
end