Method: File.encrypt

Defined in:
lib/ronin/support/crypto/core_ext/file.rb

.encrypt(path, cipher, block_size: 16384, output: nil, **kwargs) {|block| ... } ⇒ String

Encrypts the file.

Examples:

File.encrypt('file.txt', 'aes-256-cbc', password: 's3cr3t')
# => "..."

Parameters:

  • path (String)

    The path to the file.

  • cipher (String)

    The cipher to use.

  • block_size (Integer) (defaults to: 16384)

    Reads data from the file in chunks of the given block size.

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

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

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for Ronin::Support::Crypto.cipher.

Options Hash (**kwargs):

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Yields:

  • (block)

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

Yield Parameters:

  • block (String)

    An encrypted block from the file.

Returns:

  • (String)

    The encrypted data.

See Also:

Since:

  • 0.6.0



237
238
239
240
241
242
243
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 237

def self.encrypt(path,cipher, block_size: 16384, output: nil, **kwargs,&block)
  cipher = Ronin::Support::Crypto.cipher(cipher, direction: :encrypt,
                                                 **kwargs)
  file   = File.open(path,'rb')

  return cipher.stream(file, block_size: block_size, output: output,&block)
end