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-2011 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

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


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

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

  File.open(path) do |file|
    file.each_line { |line| yield line.chomp }
  end
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


81
82
83
84
85
# File 'lib/ronin/extensions/file.rb', line 81

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

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

.escape_path(path) ⇒ String

Escapes a path.

Parameters:

  • path (String)

    Unescaped path.

Returns:

  • (String)

    The escaped path.


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ronin/extensions/file.rb', line 118

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"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The MD5 checksum of the file.


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"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The SHA1 checksum of the file.


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"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The SHA256 checksum of the file.


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"

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (String)

    The SHA512 checksum of the file.


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.

Parameters:

  • path (Pathname, String)

    The path of the hexdump file.

  • options (Hash) (defaults to: {})

    Hexdump options.

Returns:

  • (String)

    The original binary data.

See Also:


40
41
42
# File 'lib/ronin/formatting/extensions/binary/file.rb', line 40

def File.unhexdump(path,options={})
  File.read(path).unhexdump(options)
end

.write(path, data) ⇒ nil

Writes the given data to a specified path.

Examples:

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

Parameters:

  • path (String)

    The path of the file to write to.

  • data (String)

    The data to write to the file.

Returns:

  • (nil)

103
104
105
# File 'lib/ronin/extensions/file.rb', line 103

def File.write(path,data)
  File.open(path,'w') { |file| file.write(data) }
end