Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/core_ext/file.rb,
lib/ronin/support/crypto/core_ext/file.rb,
lib/ronin/support/archive/core_ext/file.rb,
lib/ronin/support/compression/core_ext/file.rb,
lib/ronin/support/binary/unhexdump/core_ext/file.rb

Overview

Copyright (c) 2006-2023 Hal Brodigan (postmodern.mod3 at gmail.com)

ronin-support is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

ronin-support is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with ronin-support. If not, see https://www.gnu.org/licenses/.

Class Method Summary collapse

Class Method Details

.aes128_decrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String

Decrypts the file using AES-128.

Examples:

File.aes256_decrypt('encrypted.bin', password: 's3cr3t')
# => "..."

Parameters:

  • path (String)

    The path to the file.

  • 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 AES decrypted data to. Defaults to an empty ASCII 8bit encoded String.

  • kwargs (Hash{Symbol => Object})

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

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :md5

    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.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

Since:

  • 1.0.0



517
518
519
520
521
522
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 517

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

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

.aes128_encrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String

Encrypts the file using AES-128.

Examples:

File.aes128_encrypt('file.txt', password: 's3cr3t')
# => "..."

Parameters:

  • path (String)

    The path to the file.

  • 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 AES encrypted data to. Defaults to an empty ASCII 8bit encoded String.

  • kwargs (Hash{Symbol => Object})

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

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :md5

    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.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

Since:

  • 1.0.0



464
465
466
467
468
469
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 464

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

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

.aes256_decrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String

Decrypts the file using AES-256.

Examples:

File.aes256_decrypt('encrypted.bin', password: 's3cr3t')
# => "..."

Parameters:

  • path (String)

    The path to the file.

  • 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 AES decrypted data to. Defaults to an empty ASCII 8bit encoded String.

  • kwargs (Hash{Symbol => Object})

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

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :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.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

Since:

  • 1.0.0



623
624
625
626
627
628
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 623

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

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

.aes256_encrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String

Encrypts the file using AES-256.

Examples:

File.aes256_encrypt('file.txt', password: 's3cr3t')
# => "..."

Parameters:

  • path (String)

    The path to the file.

  • 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 AES encrypted data to. Defaults to an empty ASCII 8bit encoded String.

  • kwargs (Hash{Symbol => Object})

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

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :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.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

Since:

  • 1.0.0



570
571
572
573
574
575
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 570

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

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

.aes_decrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String

Decrypts the file using AES.

Examples:

File.aes_decrypt('encrypted.bin', key_size: 256, password: 's3cr3t')
# => "..."

Parameters:

  • path (String)

    The path to the file.

  • 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 AES decrypted data to. Defaults to an empty ASCII 8bit encoded String.

  • kwargs (Hash{Symbol => Object})

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

Options Hash (**kwargs):

  • :key_size (Integer)

    The desired key size in bits.

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :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.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

Since:

  • 1.0.0



411
412
413
414
415
416
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 411

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

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

.aes_encrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String

Encrypts the file using AES.

Examples:

File.aes_encrypt('file.txt', key_size: 256, password: 's3cr3t')
# => "..."

Parameters:

  • path (String)

    The path to the file.

  • 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 AES encrypted data to. Defaults to an empty ASCII 8bit encoded String.

  • kwargs (Hash{Symbol => Object})

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

Options Hash (**kwargs):

  • :key_size (Integer)

    The desired key size in bits.

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :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.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the the key: or password: keyword argument must be given.

Since:

  • 1.0.0



355
356
357
358
359
360
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 355

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

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

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

Decrypts the file.

Examples:

File.decrypt('encrypted.bin', '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 decrypted 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: :sha1

    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 decrypted data.

See Also:

Since:

  • 0.6.0



298
299
300
301
302
303
304
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 298

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

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

.each_line(path) {|line| ... } ⇒ Enumerator

Reads each line from the file.

Examples:

File.each_line('passwords.txt') do |line|
  # ...
end

Parameters:

  • path (String)

    The path of the file.

Yields:

  • (line)

    The given block will be passed each line.

Yield Parameters:

  • line (String)

    A line from the file, with the trailing newline characters removed.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 0.3.0



45
46
47
48
49
# File 'lib/ronin/support/core_ext/file.rb', line 45

def self.each_line(path)
  return enum_for(__method__,path) unless block_given?

  foreach(path) { |line| yield line.chomp }
end

.each_row(path, separator = /\s+/) {|row| ... } ⇒ Enumerator

Reads each row from the file.

Examples:

File.each_row('db_dump.txt', '|') do |row|
  # ...
end

Parameters:

  • path (String)

    The path of the file.

  • separator (Regexp, String) (defaults to: /\s+/)

    The pattern to split the line by.

Yields:

  • (row)

    The given block will be passed each row.

Yield Parameters:

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 0.3.0



78
79
80
81
82
# File 'lib/ronin/support/core_ext/file.rb', line 78

def self.each_row(path,separator=/\s+/)
  return enum_for(__method__,path,separator) unless block_given?

  each_line(path) { |line| yield line.split(separator) }
end

.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: :sha1

    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

.escape_path(path) ⇒ String

Escapes a path.

Parameters:

  • path (String)

    Unescaped path.

Returns:

  • (String)

    The escaped path.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ronin/support/core_ext/file.rb', line 95

def self.escape_path(path)
  path = path.to_s

  # remove any \0 characters first
  path.tr!("\0",'')

  # remove any home-dir expansions
  path.gsub!('~',"\\~")

  path = expand_path(File.join('/',path))

  # remove the leading slash
  return path[1..]
end

.gunzip(path) {|gz| ... } ⇒ Ronin::Support::Compression::Gzip::Reader

Opens the gzipped file for reading.

Examples:

File.gunzip('wordlist.gz') do |gz|
  gz.each_line do |line|
    # ...
  end
end

Parameters:

  • path (String)

    The path to the file to read.

Yields:

  • (gz)

    If a block is given, it will be passed the gzip reader object.

Yield Parameters:

Returns:

Since:

  • 1.0.0



50
51
52
# File 'lib/ronin/support/compression/core_ext/file.rb', line 50

def self.gunzip(path,&block)
  Ronin::Support::Compression::Gzip::Reader.open(path,&block)
end

.gzip(path) {|gz| ... } ⇒ Ronin::Support::Compression::Gzip::Writer

Opens the gzip file for writing.

Examples:

File.gunzip('file.gz') do |gz|
  # gzip header info
  gz.mtime = Time.now
  gz.orig_name = 'file.txt'

  gz.write('Hello World!')
end

Parameters:

  • path (String)

    The path to the file to write to.

Yields:

  • (gz)

    If a block is given, it will be passed the gzip writer object.

Yield Parameters:

Returns:

Since:

  • 1.0.0



82
83
84
# File 'lib/ronin/support/compression/core_ext/file.rb', line 82

def self.gzip(path,&block)
  Ronin::Support::Compression::Gzip::Writer.open(path,&block)
end

.hmac(path, key:, digest: :sha1) ⇒ String

Calculates the HMAC for a file.

Parameters:

  • path (String)

    The path to the file.

  • key (String)

    The secret key for the HMAC.

  • digest (Symbol) (defaults to: :sha1)

    The digest algorithm for the HMAC.

Returns:

  • (String)

    The hex-encoded HMAC for the String.

See Also:

  • Ronin::Support::Crypt.hmac

Since:

  • 0.6.0



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 172

def self.hmac(path, key: , digest: :sha1)
  hmac = Ronin::Support::Crypto.hmac(key: key, digest: digest)

  File.open(path,'rb') do |file|
    until file.eof?
      hmac.update(file.read(16384))
    end
  end

  return hmac.hexdigest
end

.md5(path) ⇒ String

Calculates the MD5 checksum of a file.

Examples:

File.md5('data.txt')
# => "5d41402abc4b2a76b9719d911017c592"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The MD5 checksum of the file.



40
41
42
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 40

def self.md5(path)
  Digest::MD5.file(path).hexdigest
end

.rmd160(path) ⇒ String

Note:

JRuby and TruffleRuby do not yet support RMD160.

Calculates the RMD160 checksum for the File.

Examples:

File.rmd160('data.txt')
# => "108f07b8382412612c048d07d13f814118445acd"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The RMD160 checksum of the File.

Since:

  • 0.6.0



147
148
149
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 147

def self.rmd160(path)
  Digest::RMD160.file(path).hexdigest
end

.rsa_decrypt(path, **kwargs) ⇒ String

Decrypts the file using the given RSA key.

Optional padding mode. nil and false will disable padding.

Parameters:

Options Hash (**kwargs):

  • :key (String, nil)

    The PEM or DER encoded RSA key string.

  • :key_file (String, nil)

    The path to the PEM or DER encoded RSA key file.

  • :key_password (String, nil)

    The optional password to decrypt the encrypted RSA key.

  • :padding (:pkcs1_oaep, :pkcs1, :sslv23, nil, false) — default: :pkcs1

Returns:

  • (String)

    The decrypted data.

Raises:

  • (ArgumentError)

    Either the key: or key_file: keyword argument must be given.

Since:

  • 1.0.0



688
689
690
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 688

def self.rsa_decrypt(path,**kwargs)
  Ronin::Support::Crypto.rsa_decrypt(File.binread(path),**kwargs)
end

.rsa_encrypt(path, **kwargs) ⇒ String

Encrypts the file using the given RSA key.

Optional padding mode. nil and false will disable padding.

Parameters:

Options Hash (**kwargs):

  • :key (String, nil)

    The PEM or DER encoded RSA key string.

  • :key_file (String, nil)

    The path to the PEM or DER encoded RSA key file.

  • :key_password (String, nil)

    The optional password to decrypt the encrypted RSA key.

  • :padding (:pkcs1_oaep, :pkcs1, :sslv23, nil, false) — default: :pkcs1

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

    Either the key: or key_file: keyword argument must be given.

Since:

  • 1.0.0



657
658
659
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 657

def self.rsa_encrypt(path,**kwargs)
  Ronin::Support::Crypto.rsa_encrypt(File.binread(path),**kwargs)
end

.sha1(path) ⇒ String

Calculates the SHA1 checksum of a file.

Examples:

File.sha1('data.txt')
# => "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The SHA1 checksum of the file.



59
60
61
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 59

def self.sha1(path)
  Digest::SHA1.file(path).hexdigest
end

.sha128(path) ⇒ Object

See Also:



68
69
70
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 68

def self.sha128(path)
  File.sha1(path)
end

.sha2(path) ⇒ Object

See Also:



96
97
98
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 96

def self.sha2(path)
  File.sha256(path)
end

.sha256(path) ⇒ String

Calculates the SHA256 checksum of a file.

Examples:

File.sha256('data.txt')
# => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The SHA256 checksum of the file.



87
88
89
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 87

def self.sha256(path)
  Digest::SHA256.file(path).hexdigest
end

.sha5(path) ⇒ Object

See Also:



124
125
126
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 124

def self.sha5(path)
  File.sha512(path)
end

.sha512(path) ⇒ String

Calculates the SHA512 checksum of a file.

Examples:

File.sha512('data.txt')
# => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The SHA512 checksum of the file.



115
116
117
# File 'lib/ronin/support/crypto/core_ext/file.rb', line 115

def self.sha512(path)
  Digest::SHA512.file(path).hexdigest
end

.tar(path) {|tar| ... } ⇒ Ronin::Support::Archive::Tar::Writer

Opens the tar archive file for writing.

Examples:

File.tar('output.tar') do |tar|
  tar.mkdir('foo')
  tar.add_file('foo/bar.txt','Hello World!')
end

Parameters:

  • path (String)

    The path to the file to write to.

Yields:

  • (tar)

    If a block is given, it will be passed the tar writer object.

Yield Parameters:

Returns:

Since:

  • 1.0.0



90
91
92
# File 'lib/ronin/support/archive/core_ext/file.rb', line 90

def self.tar(path,&block)
  Ronin::Support::Archive::Tar::Writer.open(path,&block)
end

.unhexdump(path, **kwargs) ⇒ String

Converts a hexdump file to it's original binary data.

Examples:

Unhexdump a hexdump created by GNU hexdump -C:

File.unhexdump('hexdump.txt')
# => "hello\n"

Unhexdump a hexdump created by GNU hexdump:

File.unhexdump('hexdump.txt', type: :uint16_le)
# => "hello\n"

Unhexdump a hexdump created by od:

File.unhexdump('od.txt', format: :od)
# => "hello\n"

Parameters:

Options Hash (**kwargs):

  • :format (:od, :hexdump) — default: :hexdump

    The expected format of the hexdump. Must be either :od or :hexdump.

  • :type (Symbol) — default: :byte

    Denotes the encoding used for the bytes within the hexdump. Must be one of the following:

    • :byte
    • :char
    • :uint8
    • :uint16
    • :uint32
    • :uint64
    • :int8
    • :int16
    • :int32
    • :int64
    • :uchar
    • :ushort
    • :uint
    • :ulong
    • :ulong_long
    • :short
    • :int
    • :long
    • :long_long
    • :float
    • :double
    • :float_le
    • :double_le
    • :float_be
    • :double_be
    • :uint16_le
    • :uint32_le
    • :uint64_le
    • :int16_le
    • :int32_le
    • :int64_le
    • :uint16_be
    • :uint32_be
    • :uint64_be
    • :int16_be
    • :int32_be
    • :int64_be
    • :ushort_le
    • :uint_le
    • :ulong_le
    • :ulong_long_le
    • :short_le
    • :int_le
    • :long_le
    • :long_long_le
    • :ushort_be
    • :uint_be
    • :ulong_be
    • :ulong_long_be
    • :short_be
    • :int_be
    • :long_be
    • :long_long_be
  • :address_base (2, 8, 10, 16, nil)

    The numerical base that the offset addresses are encoded in.

  • base (2, 8, 10, 16, nil)

    The numerical base that the hexdumped numbers are encoded in.

  • named_chars (Boolean)

    Indicates to parse od-style named characters (ex: nul, del, etc).

Returns:

  • (String)

    The raw-data from the hexdump.

Raises:

  • (ArgumentError)

    Unsupported type: value, or the format: was not :hexdump or :od.



125
126
127
128
# File 'lib/ronin/support/binary/unhexdump/core_ext/file.rb', line 125

def self.unhexdump(path,**kwargs)
  parser = Ronin::Support::Binary::Unhexdump::Parser.new(**kwargs)
  parser.unhexdump(new(path))
end

.untar(path) {|tar| ... } ⇒ Ronin::Support::Archive::Tar::Reader

Opens the tar archive file for reading.

Examples:

Enumerating over each entry in the tar archive:

File.untar('file.tar') do |tar|
  tar.each do |entry|
    puts entry.full_name
    puts '-' * 80
    puts entry.read
    puts '-' * 80
  end
end

Reads a specific file from the tar archive:

File.untar('file.tar') do |tar|
  data = tar.read('foo.txt')
  # ...
end

Parameters:

  • path (String)

    The path to the file to read.

Yields:

  • (tar)

    If a block is given, it will be passed the tar reader object.

Yield Parameters:

Returns:

Since:

  • 1.0.0



61
62
63
# File 'lib/ronin/support/archive/core_ext/file.rb', line 61

def self.untar(path,&block)
  Ronin::Support::Archive::Tar::Reader.open(path,&block)
end

.unzip(path) {|zip| ... } ⇒ Ronin::Support::Archive::Zip::Reader

Opens the zip archive file for reading.

Examples:

Enumerating over each file in a zip archive:

File.unzip('file.zip') do |zip|
  zip.each do |entry|
    puts entry.name
    puts '-' * 80
    puts entry.read
    puts '-' * 80
  end
end

Reads a specific file from a zip archive:

File.unzip('file.zip') do |zip|
  data = zip.read('foo.txt')
  # ...
end

Parameters:

  • path (String)

    The path to the file to read.

Yields:

  • (zip)

    If a block is given, it will be passed the zip reader object.

Yield Parameters:

Returns:

Since:

  • 1.0.0



129
130
131
# File 'lib/ronin/support/archive/core_ext/file.rb', line 129

def self.unzip(path,&block)
  Ronin::Support::Archive::Zip::Reader.open(path,&block)
end

.zip(path) {|zip| ... } ⇒ Ronin::Support::Archive::Zip::Writer

Opens the zip archive file for writing.

Examples:

File.zip('output.zip') do |zip|
  zip.add_file('foo/bar.txt','Hello World!')
end

Parameters:

  • path (String)

    The path to the file to write to.

Yields:

  • (zip)

    If a block is given, it will be passed the zip writer object.

Yield Parameters:

Returns:

Since:

  • 1.0.0



157
158
159
# File 'lib/ronin/support/archive/core_ext/file.rb', line 157

def self.zip(path,&block)
  Ronin::Support::Archive::Zip::Writer.open(path,&block)
end