file_rock 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/file_rock.rb +170 -0
  3. data/lib/filerock/enc.rb +10 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 96739dd646e1a5305255fb2df2ebcdd0f8ea3292
4
+ data.tar.gz: 5de7cab8cb66919248a39eed82c015f7b50e8315
5
+ SHA512:
6
+ metadata.gz: 8cc7dc0a450d38a32ab1890334e7ec1df78d32afd3fc2ff8dfb48d8487a54475328547f3d9f2ebb98050c1ce533ed5fb17bff62f52bf126464c35da40f0b614d
7
+ data.tar.gz: 85fe208a2b054ce800b060d682eb24c670b5a52747c2b8eaa8588c6a89fe0c9297b0ebb471f72ecb97ade867b3c2f87c33d1228d3e729a225ca18899f66a21e9
data/lib/file_rock.rb ADDED
@@ -0,0 +1,170 @@
1
+ # file_rock.rb
2
+ #
3
+ # Copyright (C) June 26, 2014
4
+ #
5
+ # Author: Abbas Taghiloei AKA MR.0x41 <ox41_a@yahoo.com>
6
+ #
7
+ # == License
8
+ #
9
+ # The MIT License (MIT)
10
+ #
11
+ # Copyright (c) [2014] [file_rock v 0.0.1 Ruby Gem]
12
+ #
13
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ # of this software and associated documentation files (the "Software"), to deal
15
+ # in the Software without restriction, including without limitation the rights
16
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ # copies of the Software, and to permit persons to whom the Software is
18
+ # furnished to do so, subject to the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be included in all
21
+ # copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ # SOFTWARE.
30
+ #
31
+
32
+ raise "Please, use ruby 2.0.0 or later." if RUBY_VERSION < "2.0.0"
33
+
34
+ require 'digest'
35
+ require 'filerock/enc'
36
+
37
+ class FileRock
38
+ def initialize(sett = {})
39
+ @sett = {'space' => "ON", 'size' => 23}
40
+ @sett = @sett.merge(sett)
41
+ if(@sett["file"] == nil)
42
+ raise 'No file address input. Please send the file address as an input.'
43
+ end
44
+ end
45
+ def header
46
+ i = @sett['size']
47
+ head = Enc.tohex(IO.binread(@sett["file"]))[0..i]
48
+ if(@sett["space"] =~ /ON/i)
49
+ head = head.gsub(/(.{2})(?=.)/, '\1 \2')
50
+ end
51
+ return head
52
+ end
53
+ def footer
54
+ i = @sett["size"]
55
+ foot = Enc.tohex(IO.binread(@sett["file"])).reverse![0..i].reverse!
56
+ if(@sett["space"] =~ /ON/i)
57
+ foot = foot.gsub(/(.{2})(?=.)/, '\1 \2')
58
+ end
59
+ return foot
60
+ end
61
+ def create_checksum_MD5(blocksize = nil)
62
+ if(blocksize.is_a? String)
63
+ raise "Wrong method argument, You should send an integrer."
64
+ end
65
+ if(blocksize.nil?)
66
+ checksum_MD5 = Digest::MD5.hexdigest(File.read(@sett["file"]))
67
+ return checksum_MD5
68
+ else
69
+ open(@sett["file"],"rb"){|file| checksum_MD5 = Digest::MD5.hexdigest(file.read(blocksize))}
70
+ return checksum_MD5
71
+ end
72
+ end
73
+ def create_checksum_SHA1(blocksize = nil)
74
+ if(blocksize.is_a? String)
75
+ raise "Wrong method argument, You should send an integrer."
76
+ end
77
+ if(blocksize.nil?)
78
+ checksum_SHA1 = Digest::SHA1.hexdigest(File.read(@sett["file"]))
79
+ return checksum_SHA1
80
+ else
81
+ open(@sett["file"],"rb"){|file| checksum_SHA1 = Digest::SHA1.hexdigest(file.read(blocksize))}
82
+ return checksum_SHA1
83
+ end
84
+ end
85
+ def create_checksum_SHA512(blocksize = nil)
86
+ if(blocksize.is_a? String)
87
+ raise "Wrong method argument, You should send an integrer."
88
+ end
89
+ if(blocksize.nil?)
90
+ checksum_SHA512 = Digest::SHA512.hexdigest(File.read(@sett["file"]))
91
+ return checksum_SHA512
92
+ else
93
+ open(@sett["file"],"rb"){|file| checksum_SHA512 = Digest::SHA512.hexdigest(file.read(blocksize))}
94
+ return checksum_SHA512
95
+ end
96
+ end
97
+ def check_checksum(checksum, blocksize = nil)
98
+ if(checksum.length == 32)
99
+ if(blocksize.nil?)
100
+ checksum_MD5 = Digest::MD5.hexdigest(File.read(@sett["file"]))
101
+ else
102
+ open(@sett["file"],"rb"){|file| checksum_MD5 = Digest::MD5.hexdigest(file.read(blocksize))}
103
+ end
104
+ if(checksum == checksum_MD5)
105
+ return true
106
+ else
107
+ return false
108
+ end
109
+ elsif(checksum.length == 40)
110
+ if(blocksize.nil?)
111
+ checksum_SHA1 = Digest::SHA1.hexdigest(File.read(@sett["file"]))
112
+ else
113
+ open(@sett["file"],"rb"){|file| checksum_SHA1 = Digest::SHA1.hexdigest(file.read(blocksize))}
114
+ end
115
+ if(checksum == checksum_SHA1)
116
+ return true
117
+ else
118
+ return false
119
+ end
120
+ elsif(checksum.length == 128)
121
+ if(blocksize.nil?)
122
+ checksum_SHA512 = Digest::SHA512.hexdigest(File.read(@sett["file"]))
123
+ else
124
+ open(@sett["file"],"rb"){|file| checksum_SHA512 = Digest::SHA512.hexdigest(file.read(blocksize))}
125
+ end
126
+ if(checksum == checksum_SHA512)
127
+ return true
128
+ else
129
+ return false
130
+ end
131
+ else
132
+ raise "Unknown Checksum Algorithm."
133
+ end
134
+ end
135
+ def type?
136
+ header = ["jP", "ftyp3gp", "ftypisom","ftypqt", "ftyp3gp5", "IIXPR","MMXPR", "MSISAMDatabase", "StandardACEDB","StandardJetDB", ".dss","APPR", "SKF", "dt2ddtd",
137
+ "org.bitcoin.pr", ".DOC", ".NeroISO",".WKS", "DiskDescripto", "MicrosoftDeveloperStudio","SCCA", "#!AMR","#?RADIANCE.", "%PDF", "%bitmap",".REC", "8BPS", "KEYB",
138
+ "MZ", "ElfFile", "GIF89a", "GIF87a", "MSFT", "NESM", "REGEDIT", "REVNUM:,", "SMARTDRW", "dswfile", "moov", "PAGEDU64"]
139
+ type = ["JPEG-2000", "3GPP", "MPEG-4","QuickTime movie file", "MPEG-4 video files", "Quark Express document","Quark Express document", "Microsoft Money file",
140
+ "Microsoft Access 2007 file", "Microsoft Access file", "Digital Speech Standard", "Approach index file","SkinCrafter skin file", "DesignTools 2D Design file",
141
+ "MultiBit Bitcoin wallet file","DeskMate Document file", "Nero CD Compilation", "DeskMate Worksheet","Windows prefetch file", "VMware 4 Virtual Disk description file",
142
+ "Microsoft Developer Studio project file", "Adaptive Multi-Rate ACELP", "Radiance High Dynamic Range image file","Adobe Portable Document Format and Forms Document file",
143
+ "Fuzzy bitmap (FBM) file", "RealPlayer video file (V11 and later)","Photoshop image file", "Keyboard driver file", "Windows/DOS executable file","Windows Vista event log",
144
+ "Graphics interchange format file", "Graphics interchange format file","OLE, SPSS, or Visual C++ type library file", "NES Sound file",
145
+ "Windows NT Registry and Registry Undo files", "Antenna data file", "SmartDraw Drawing file", "Microsoft Visual Studio workspace file", "QuickTime movie file",
146
+ "Windows 64-bit memory dump"]
147
+ hexhead = ["FFFFFFFFFFFFFFFFFFFF000002", "1F8B08","1F9D", "1FA0", "FFD8FFE0","5F27A889", "34CDB2A1", "4D444D5093A7", "504B0304140008000800", "A1B2C3D4", "A1B2CD34",
148
+ "D4C3B2A1"]
149
+ hextype = ["Alcohol 120 CD image", "GZIP archive file", "Compressed tape archive file using standard","Compressed tape archive file using LZH", "JPEG/JFIF graphics file",
150
+ "Jar archive","Extended tcpdump (libpcap) capture file (Linux/Unix)", "Windows heap dump file", "Java archive",
151
+ "tcpdump (libpcap) capture file (Linux/Unix)", "Extended tcpdump (libpcap) capture file (Linux/Unix)", "WinDump (winpcap) capture file (Windows)"]
152
+ i = 0
153
+ typetestheadassci = IO.binread(@sett["file"])[0..42]
154
+ while(i < header.size)
155
+ if(typetestheadassci =~ /#{header[i]}/i)
156
+ return type[i]
157
+ end
158
+ i += 1
159
+ end
160
+ i = 0
161
+ typetestheadhex = Enc.tohex(IO.binread(@sett["file"]))[0..42]
162
+ while(i < header.size)
163
+ if(typetestheadhex =~ /#{hexhead[i]}/i)
164
+ return hextype[i]
165
+ end
166
+ i += 1
167
+ end
168
+ return nil
169
+ end
170
+ end
@@ -0,0 +1,10 @@
1
+ class Enc
2
+ def self.tohex(bit)
3
+ bit = bit.each_byte.map { |b| b.to_s(16) }.join
4
+ return bit
5
+ end
6
+ def self.toascii(bit)
7
+ bit = bit.scan(/../).map { |x| x.hex.chr }.join
8
+ return bit
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_rock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - A-Taghiloei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Gem to get files Header, Footer, Type(via headers), create file checksum,
14
+ check file checksum and ... .
15
+ email: ox41_a@yahoo.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/file_rock.rb
21
+ - lib/filerock/enc.rb
22
+ homepage: http://www.opensec.ir
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: 2.0.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.0
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: A Gem to get files Header, Footer, and other info.
46
+ test_files: []