Class: File
- Inherits:
-
Object
- Object
- File
- 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-2024 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
-
.aes128_decrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String
Decrypts the file using AES-128.
-
.aes128_encrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String
Encrypts the file using AES-128.
-
.aes256_decrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String
Decrypts the file using AES-256.
-
.aes256_encrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String
Encrypts the file using AES-256.
-
.aes_decrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String
Decrypts the file using AES.
-
.aes_encrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String
Encrypts the file using AES.
-
.decrypt(path, cipher, block_size: 16384, output: nil, **kwargs) {|block| ... } ⇒ String
Decrypts the file.
-
.each_line(path) {|line| ... } ⇒ Enumerator
Reads each line from the file.
-
.each_row(path, separator = /\s+/) {|row| ... } ⇒ Enumerator
Reads each row from the file.
-
.encrypt(path, cipher, block_size: 16384, output: nil, **kwargs) {|block| ... } ⇒ String
Encrypts the file.
-
.escape_path(path) ⇒ String
Escapes a path.
-
.gunzip(path) {|gz| ... } ⇒ Ronin::Support::Compression::Gzip::Reader
Opens the gzipped file for reading.
-
.gzip(path) {|gz| ... } ⇒ Ronin::Support::Compression::Gzip::Writer
Opens the gzip file for writing.
-
.hmac(path, key:, digest: :sha1) ⇒ String
Calculates the HMAC for a file.
-
.md5(path) ⇒ String
Calculates the MD5 checksum of a file.
-
.rmd160(path) ⇒ String
Calculates the RMD160 checksum for the File.
-
.rsa_decrypt(path, **kwargs) ⇒ String
Decrypts the file using the given RSA key.
-
.rsa_encrypt(path, **kwargs) ⇒ String
Encrypts the file using the given RSA key.
-
.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.
-
.tar(path) {|tar| ... } ⇒ Ronin::Support::Archive::Tar::Writer
Opens the tar archive file for writing.
-
.unhexdump(path, **kwargs) ⇒ String
Converts a hexdump file to it's original binary data.
-
.untar(path) {|tar| ... } ⇒ Ronin::Support::Archive::Tar::Reader
Opens the tar archive file for reading.
-
.unzip(path) {|zip| ... } ⇒ Ronin::Support::Archive::Zip::Reader
Opens the zip archive file for reading.
-
.zip(path) {|zip| ... } ⇒ Ronin::Support::Archive::Zip::Writer
Opens the zip archive file for writing.
Class Method Details
.aes128_decrypt(path, block_size: 16384, output: nil, **kwargs, &block) ⇒ String
Decrypts the file using AES-128.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 = (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.
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.
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.
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.
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
JRuby and TruffleRuby do not yet support RMD160.
Calculates the RMD160 checksum for the File.
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.
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.
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.
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
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
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.
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
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.
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.
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.
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.
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 |