Class: Utils::EcbOracle

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto-toolbox/utils/ecb_oracle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(static_key: nil, static_mode: nil, block_size: 128, static_prefix: nil, static_suffix: nil, append: false, prepend: false) ⇒ EcbOracle

Returns a new instance of EcbOracle.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/crypto-toolbox/utils/ecb_oracle.rb', line 5

def initialize(static_key: nil,static_mode: nil,block_size: 128,static_prefix: nil,static_suffix: nil,append: false, prepend: false)
  @key  = CryptBuffer(static_key)
  @mode = static_mode
  @iv   = nil
  @c    = nil
  @block_size = block_size
  
  @append  = append
  @prepend = prepend
  @suffix = static_suffix
  @prefix = static_prefix
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



3
4
5
# File 'lib/crypto-toolbox/utils/ecb_oracle.rb', line 3

def mode
  @mode
end

#prefixObject (readonly)

Returns the value of attribute prefix.



3
4
5
# File 'lib/crypto-toolbox/utils/ecb_oracle.rb', line 3

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



3
4
5
# File 'lib/crypto-toolbox/utils/ecb_oracle.rb', line 3

def suffix
  @suffix
end

Instance Method Details

#encipher(plaintext) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/crypto-toolbox/utils/ecb_oracle.rb', line 18

def encipher(plaintext)
  #support reproducable keys and mode
  @key      ||= CryptBuffer.random(16)
  @mode     ||= [:cbc,:ecb][SecureRandom.random_number(2)]
  message   = pad_message(plaintext) 
  method  = "encipher_#{@mode}".to_sym
  # we dispatch the method to avoid if-else dispatches
  # due to the difference of IV usage
  @c = send(method,message)
end