Class: CryptoToolchain::BlackBoxes::EcbOrCbcEncryptor

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

Constant Summary collapse

ENCRYPTION_ALGORITHMS =
%i(ebc cbc).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plaintext, algorithm: random_algorithm) ⇒ EcbOrCbcEncryptor

Returns a new instance of EcbOrCbcEncryptor.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb', line 7

def initialize(plaintext, algorithm: random_algorithm)
  raise ArgumentError.new("Unsupported algorithm #{algorithm}") unless ENCRYPTION_ALGORITHMS.include? algorithm
  @plaintext = plaintext
  @key = String.random_bytes(16)
  @algorithm = algorithm
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb', line 5

def key
  @key
end

#plaintextObject (readonly)

Returns the value of attribute plaintext.



5
6
7
# File 'lib/crypto_toolchain/black_boxes/ecb_or_cbc_encryptor.rb', line 5

def plaintext
  @plaintext
end

Instance Method Details

#encrypt(_algo = algorithm) ⇒ Object



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

def encrypt(_algo = algorithm)
  case _algo
  when :ecb
    encrypt_ecb
  when :cbc
    encrypt_cbc
  end
end