Class: BlockCipherKit::AES256CFBScheme
- Inherits:
-
BaseScheme
- Object
- BaseScheme
- BlockCipherKit::AES256CFBScheme
- Defined in:
- lib/block_cipher_kit/aes_256_cfb_scheme.rb
Constant Summary collapse
- IV_LENGTH =
16
Instance Method Summary collapse
-
#initialize(encryption_key, iv_generator: SecureRandom) ⇒ AES256CFBScheme
constructor
A new instance of AES256CFBScheme.
- #required_encryption_key_length ⇒ Object
- #streaming_decrypt(from_ciphertext_io:, into_plaintext_io: nil, &blk) ⇒ Object
- #streaming_decrypt_range(from_ciphertext_io:, range:, into_plaintext_io: nil, &blk) ⇒ Object
- #streaming_encrypt(into_ciphertext_io:, from_plaintext_io: nil, &blk) ⇒ Object
Methods inherited from BaseScheme
Constructor Details
#initialize(encryption_key, iv_generator: SecureRandom) ⇒ AES256CFBScheme
Returns a new instance of AES256CFBScheme.
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_length ⇒ Object
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 37 38 |
# 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) # There is potential, but I don't have time for this at the moment # https://crypto.stackexchange.com/a/87007 writable = BlockCipherKit::BlockWritable.new(into_plaintext_io, &blk) lens = BlockCipherKit::WriteWindowIO.new(writable, range.begin, range.end - range.begin + 1) 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 |