Class: Ronin::Support::Crypto::Cipher

Inherits:
OpenSSL::Cipher
  • Object
show all
Defined in:
lib/ronin/support/crypto/cipher.rb,
lib/ronin/support/crypto/cipher/aes.rb,
lib/ronin/support/crypto/cipher/aes128.rb,
lib/ronin/support/crypto/cipher/aes256.rb

Overview

Represents a cryptographic cipher.

Examples

Encrypt Data

aes256 = Crypto::Cipher.new('aes-256-cbc', direction: :encrypt,
                                           password: 'secret')
aes256.encrypt("message in a bottle")
# => "\x18\xC7\x00~\xA2\xA1\x80\x84c\x98,81mo\xBAZ\xDD\xF4\xF2\xEF\xA9\xDE\xB3\xD6!\xB9\xA8WT\x9D\xE0"

Decrypt Data

aes256 = Crypto::Cipher.new('aes-256-cbc', direction: :decrypt,
                                           password: 'secret')
aes256.decrypt("\x18\xC7\x00~\xA2\xA1\x80\x84c\x98,81mo\xBAZ\xDD\xF4\xF2\xEF\xA9\xDE\xB3\xD6!\xB9\xA8WT\x9D\xE0")
# => "message in a bottle"

Direct Known Subclasses

AES

Defined Under Namespace

Classes: AES, AES128, AES256

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, direction:, key: nil, hash: :sha256, password: nil, iv: nil, padding: nil) ⇒ Cipher

Initializes the cipher.

Parameters:

  • name (String)

    The name of the cipher.

  • direction (:encrypt, :decrypt)

    Specifies whether the cipher will be used to encrypt or decrypt data.

  • hash (Symbol, nil) (defaults to: :sha256)

    The algorithm to hash the :password.

  • key (String, nil) (defaults to: nil)

    The secret key to use.

  • password (String, nil) (defaults to: nil)

    The password for the cipher.

  • iv (String, nil) (defaults to: nil)

    The optional Initial Vector (IV).

  • padding (Integer, nil) (defaults to: nil)

    Sets the padding for the cipher.

Raises:

  • (ArgumentError)

    The key: or password: keyword arguments were not given.

Since:

  • 1.0.0



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ronin/support/crypto/cipher.rb', line 79

def initialize(name, direction: ,
                     key:       nil,
                     hash:      :sha256,
                     password:  nil,
                     iv:        nil,
                     padding:   nil)
  super(name)

  case direction
  when :encrypt then self.encrypt
  when :decrypt then self.decrypt
  end

  if password && hash
    self.key = OpenSSL::Digest.const_get(hash.upcase).digest(password)
  elsif key
    self.key = key
  else
    raise(ArgumentError,"the the key: or password: keyword argument must be given")
  end

  self.iv      = iv      if iv
  self.padding = padding if padding
end

Class Method Details

.supportedArray<String>

The list of supported ciphers.

Returns:

  • (Array<String>)

    The list of supported cipher names.

Since:

  • 1.0.0



110
111
112
# File 'lib/ronin/support/crypto/cipher.rb', line 110

def self.supported
  ciphers
end

Instance Method Details

#decrypt(data = nil) ⇒ String

Decrypts the given data.

Parameters:

  • data (String) (defaults to: nil)

    The data to decrypt.

Returns:

  • (String)

    The decrypted data.

Since:

  • 1.0.0



140
141
142
143
144
145
146
# File 'lib/ronin/support/crypto/cipher.rb', line 140

def decrypt(data=nil)
  if data
    update(data) + final
  else
    super()
  end
end

#encrypt(data = nil) ⇒ String

Encrypts the given data.

Parameters:

  • data (String) (defaults to: nil)

    The data to encrypt.

Returns:

  • (String)

    The encrypted data.

Since:

  • 1.0.0



123
124
125
126
127
128
129
# File 'lib/ronin/support/crypto/cipher.rb', line 123

def encrypt(data=nil)
  if data
    update(data) + final
  else
    super()
  end
end

#stream(io, block_size: 16384, output: nil) {|block| ... } ⇒ String

Pipes the IO stream through the cipher.

Parameters:

  • io (IO)

    The IO stream to encrypt or decrypt.

  • block_size (Integer) (defaults to: 16384)

    The block size to read the data with.

  • output (String, #<<, nil) (defaults to: nil)

    The optional output buffer to append processed data to. Defaults to an empty ASCII 8bit encoded String.

Yields:

  • (block)

    If a block is given, each processed block will be passed to it.

Yield Parameters:

  • block (String)

    A processed block from the file.

Returns:

  • (String)

    The processed data, if no block was given.

Since:

  • 1.0.0



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ronin/support/crypto/cipher.rb', line 170

def stream(io, block_size: 16384, output: nil)
  unless block_given?
    output ||= String.new(encoding: Encoding::ASCII_8BIT)
  end

  until io.eof?
    block = update(io.read(block_size))

    if block_given? then yield block
    else                 output << block
    end
  end

  if block_given?
    yield final
  else
    output << final
    return output
  end
end