Class: File
- 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-2021 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 https://www.gnu.org/licenses/.
Class Method Summary collapse
-
.each_line(path) {|line| ... } ⇒ Enumerator
Reads each line from the file.
-
.each_row(path, separator = /\s+/) {|row| ... } ⇒ Enumerator
Reads each row from the file.
-
.escape_path(path) ⇒ String
Escapes a path.
-
.md5(path) ⇒ String
Calculates the MD5 checksum of a file.
-
.sha1(path) ⇒ String
Calculates the SHA1 checksum of a file.
-
.sha128(path) ⇒ Object
-
.sha2(path) ⇒ Object
-
.sha256(path) ⇒ String
Calculates the SHA256 checksum of a file.
-
.sha5(path) ⇒ Object
-
.sha512(path) ⇒ String
Calculates the SHA512 checksum of a file.
-
.unhexdump(path, options = {}) ⇒ String
Converts a hexdump file to it’s original binary data.
-
.write(path, data, offset = 0) ⇒ nil
Writes the given data to a specified path.
Class Method Details
.each_line(path) {|line| ... } ⇒ Enumerator
Reads each line from the file.
46 47 48 49 50 |
# File 'lib/ronin/extensions/file.rb', line 46 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.
79 80 81 82 83 |
# File 'lib/ronin/extensions/file.rb', line 79 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 |
.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 self.escape_path(path) path = path.to_s # remove any \0 characters first path.tr!("\0",'') # remove any home-dir expansions path.gsub!('~',"\\~") path = (File.join('/',path)) # remove the leading slash return path[1..-1] end |
.md5(path) ⇒ String
Calculates the MD5 checksum of a 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.
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
69 70 71 |
# File 'lib/ronin/formatting/extensions/digest/file.rb', line 69 def File.sha128(path) File.sha1(path) end |
.sha2(path) ⇒ Object
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.
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
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.
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.
73 74 75 |
# File 'lib/ronin/formatting/extensions/binary/file.rb', line 73 def File.unhexdump(path,={}) Ronin::Binary::Hexdump::Parser.new().parse(File.new(path)) end |
.write(path, data, offset = 0) ⇒ nil
Writes the given data to a specified path.
105 106 107 108 109 110 |
# File 'lib/ronin/extensions/file.rb', line 105 def self.write(path,data,offset=0) open(path,'w') do |file| file.seek(offset) file.write(data) end end |