Class: CryptoToolchain::BlackBoxes::AesCtrEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_toolchain/black_boxes/aes_ctr_editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plaintext, key: Random.new.bytes(16), nonce: rand(0..0x0000FF)) ⇒ AesCtrEditor

Returns a new instance of AesCtrEditor.



4
5
6
7
8
9
# File 'lib/crypto_toolchain/black_boxes/aes_ctr_editor.rb', line 4

def initialize(plaintext, key: Random.new.bytes(16), nonce: rand(0..0x0000FF))
  @plaintext = plaintext
  @key = key
  @nonce = nonce
  @ciphertext = plaintext.encrypt_ctr(key: key, nonce: nonce)
end

Instance Attribute Details

#ciphertextObject (readonly)

Returns the value of attribute ciphertext.



22
23
24
# File 'lib/crypto_toolchain/black_boxes/aes_ctr_editor.rb', line 22

def ciphertext
  @ciphertext
end

#keyObject (readonly)

Returns the value of attribute key.



22
23
24
# File 'lib/crypto_toolchain/black_boxes/aes_ctr_editor.rb', line 22

def key
  @key
end

#nonceObject (readonly)

Returns the value of attribute nonce.



22
23
24
# File 'lib/crypto_toolchain/black_boxes/aes_ctr_editor.rb', line 22

def nonce
  @nonce
end

#plaintextObject (readonly)

Returns the value of attribute plaintext.



22
23
24
# File 'lib/crypto_toolchain/black_boxes/aes_ctr_editor.rb', line 22

def plaintext
  @plaintext
end

Instance Method Details

#edit(offset:, with:) ⇒ Object

Offset is in bytes Does not mutate @ciphetext or @plaintext



13
14
15
16
17
18
19
20
# File 'lib/crypto_toolchain/black_boxes/aes_ctr_editor.rb', line 13

def edit(offset: ,with: )
  previous = ciphertext[0...offset]
  after = ciphertext[(offset + with.bytesize)..-1]
  edited = with.encrypt_ctr(nonce: nonce,
                            key: key,
                            start_counter: offset)
  previous + edited + after
end