-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecksum.rb
48 lines (36 loc) · 1.13 KB
/
checksum.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'digest/md5'
module Checksum
def self.checksum(path)
if File.directory?(path)
return Checksum.checksumDir(path)
end
result = `md5sum \"#{path}\"`
result.split(' ')[0] unless result.start_with?('md5sum:')
end
def self.checksumDir(path)
contents = Dir.entries(path).select { |d| !d.eql?('.') && !d.eql?('..') }
cathash = ""
contents.each do |file|
cathash << Checksum.checksum(File.join(path, file))
end
Digest::MD5.hexdigest(cathash)
end
class SshChecksum
@ssh = nil
def initialize(ssh)
@ssh = ssh
end
def checksum(path)
@ssh.isDirectory(path) ? checksumDir(path) : @ssh.checksum(path)
end
private
def checksumDir(path)
contents = @ssh.getDirectoryContents(path)
cathash = ""
contents.each do |file|
cathash << checksum(File.join(path, file))
end
Digest::MD5.hexdigest(cathash)
end
end
end