Class: BlockCipherKit::AES256CFBScheme

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

Constant Summary collapse

IV_LENGTH =
16

Instance Method Summary collapse

Methods inherited from BaseScheme

#decrypt_range, #read_copy_stream_via_cipher, #write_copy_stream_via_cipher

Constructor Details

#initialize(encryption_key, iv_generator: SecureRandom) ⇒ AES256CFBScheme

Returns a new instance of AES256CFBScheme.

Raises:

  • (ArgumentError)


4
5
6
7
8
# File 'lib/block_cipher_kit/aes_256_cfb_scheme.rb', line 4

def initialize(encryption_key, iv_generator: SecureRandom)
  raise ArgumentError, "#{required_encryption_key_length} bytes of key material needed, at the minimum" unless encryption_key.bytesize >= required_encryption_key_length
  @iv_generator = iv_generator
  @key = BlockCipherKit::KeyMaterial.new(encryption_key.byteslice(0, 32))
end

Instance Method Details

#required_encryption_key_lengthObject



10
11
12
# File 'lib/block_cipher_kit/aes_256_cfb_scheme.rb', line 10

def required_encryption_key_length
  32
end

#streaming_decrypt(from_ciphertext_io:, into_plaintext_io: nil, &blk) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/block_cipher_kit/aes_256_cfb_scheme.rb', line 14

def streaming_decrypt(from_ciphertext_io:, into_plaintext_io: nil, &blk)
  cipher = OpenSSL::Cipher.new("aes-256-cfb")
  cipher.decrypt
  cipher.iv = from_ciphertext_io.read(IV_LENGTH)
  cipher.key = @key
  read_copy_stream_via_cipher(source_io: from_ciphertext_io, cipher: cipher, destination_io: into_plaintext_io, &blk)
end

#streaming_decrypt_range(from_ciphertext_io:, range:, into_plaintext_io: nil, &blk) ⇒ Object



32
33
34
35
36
# File 'lib/block_cipher_kit/aes_256_cfb_scheme.rb', line 32

def streaming_decrypt_range(from_ciphertext_io:, range:, into_plaintext_io: nil, &blk)
  writable = BlockCipherKit::BlockWritable.new(into_plaintext_io, &blk)
  lens = BlockCipherKit::IOLens.new(writable, range)
  streaming_decrypt(from_ciphertext_io: from_ciphertext_io, into_plaintext_io: lens)
end

#streaming_encrypt(into_ciphertext_io:, from_plaintext_io: nil, &blk) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/block_cipher_kit/aes_256_cfb_scheme.rb', line 22

def streaming_encrypt(into_ciphertext_io:, from_plaintext_io: nil, &blk)
  iv = @iv_generator.bytes(16)
  cipher = OpenSSL::Cipher.new("aes-256-cfb")
  cipher.encrypt
  cipher.iv = iv
  cipher.key = @key
  into_ciphertext_io.write(iv)
  write_copy_stream_via_cipher(source_io: from_plaintext_io, cipher: cipher, destination_io: into_ciphertext_io, &blk)
end