Class: File

Inherits:
Object show all
Defined in:
lib/ronin/extensions/file.rb,
lib/ronin/formatting/extensions/binary/file.rb,
lib/ronin/formatting/extensions/digest/file.rb

Overview

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

This file is part of Ronin Support.

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 http://www.gnu.org/licenses/.

Class Method Summary collapse

Class Method Details

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

Reads each line from the file.

Examples:

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

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.

Since:

  • 0.3.0


46
47
48
49
50
# File 'lib/ronin/extensions/file.rb', line 46

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

  File.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

Yields:

  • (row)

    The given block will be passed each row.

Yield Parameters:

Since:

  • 0.3.0


79
80
81
82
83
# File 'lib/ronin/extensions/file.rb', line 79

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

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

.escape_path(path) ⇒ String

Escapes a path.


124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ronin/extensions/file.rb', line 124

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

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

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

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

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

.md5(path) ⇒ String

Calculates the MD5 checksum of a file.

Examples:

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

41
42
43
# File 'lib/ronin/formatting/extensions/digest/file.rb', line 41

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

.sha1(path) ⇒ String

Calculates the SHA1 checksum of a file.

Examples:

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

60
61
62
# File 'lib/ronin/formatting/extensions/digest/file.rb', line 60

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

.sha128(path) ⇒ Object

See Also:


69
70
71
# File 'lib/ronin/formatting/extensions/digest/file.rb', line 69

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

.sha2(path) ⇒ Object

See Also:


97
98
99
# File 'lib/ronin/formatting/extensions/digest/file.rb', line 97

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

.sha256(path) ⇒ String

Calculates the SHA256 checksum of a file.

Examples:

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

88
89
90
# File 'lib/ronin/formatting/extensions/digest/file.rb', line 88

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

.sha5(path) ⇒ Object

See Also:


125
126
127
# File 'lib/ronin/formatting/extensions/digest/file.rb', line 125

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

.sha512(path) ⇒ String

Calculates the SHA512 checksum of a file.

Examples:

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

116
117
118
# File 'lib/ronin/formatting/extensions/digest/file.rb', line 116

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

.unhexdump(path, options = {}) ⇒ String

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

Options Hash (options):

  • :format (Symbol)

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

  • :encoding (Symbol)

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

    • :binary
    • :octal
    • :octal_bytes
    • :octal_shorts
    • :octal_ints
    • :octal_quads (Ruby 1.9 only)
    • :decimal
    • :decimal_bytes
    • :decimal_shorts
    • :decimal_ints
    • :decimal_quads (Ruby 1.9 only)
    • :hex
    • :hex_chars
    • :hex_bytes
    • :hex_shorts
    • :hex_ints
    • :hex_quads
    • :named_chars (Ruby 1.9 only)
    • :floats
    • :doubles
  • :endian (:little, :big, :network) — default: :little

    The endianness of the words.

  • :segment (Integer) — default: 16

    The length in bytes of each segment in the hexdump.


73
74
75
# File 'lib/ronin/formatting/extensions/binary/file.rb', line 73

def File.unhexdump(path,options={})
  Ronin::Binary::Hexdump::Parser.new(options).parse(File.new(path))
end

.write(path, data, offset = 0) ⇒ nil

Writes the given data to a specified path.

Examples:

File.write('dump.txt',data)

105
106
107
108
109
110
# File 'lib/ronin/extensions/file.rb', line 105

def File.write(path,data,offset=0)
  File.open(path,'w') do |file|
    file.seek(offset)
    file.write(data)
  end
end