Class: Shadowserver::Whitelist

Inherits:
Object
  • Object
show all
Defined in:
lib/shadowserver/whitelist.rb

Constant Summary collapse

@@baseurl =
"http://bin-test.shadowserver.org/api"

Class Method Summary collapse

Class Method Details

.by_filename(filename) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/shadowserver/whitelist.rb', line 34

def Whitelist::by_filename(filename)
	if File.exists?(filename)
		hash = Digest::SHA1.hexdigest(File.open(filename).read)
	else
		raise "Whitelist::by_filename: Could not find file, #{filename}"
	end
	Whitelist::by_hash(hash)
end

.by_hash(hash) ⇒ Object



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
# File 'lib/shadowserver/whitelist.rb', line 9

def Whitelist::by_hash(hash)
	url = @@baseurl
	if hash.length == 32
		url += "?md5=#{hash.upcase}"
	elsif hash.length == 40
		url += "?sha1=#{hash.upcase}"
	else
		raise "The hash must be either 32 or 40 characters long"
	end
	url = URI.parse(url)
	request = Net::HTTP::Get.new(url.path+"?"+url.query)
	request.add_field("User-Agent", "Ruby/#{RUBY_VERSION} shadowserver rubygem (https://github.com/chrislee35/shadowserver)")
	http = Net::HTTP.new(url.host, url.port)
	if url.scheme == 'https'
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE
		http.verify_depth = 5
	end
	resp = http.request(request)
	if resp.body =~ /^[0-9A-F]{32,40} (.+)/
		JSON.parse($1)
	else
		nil
	end
end

.by_string(string) ⇒ Object



42
43
44
45
# File 'lib/shadowserver/whitelist.rb', line 42

def Whitelist::by_string(string)
	hash = Digest::SHA1.hexdigest(string)
	Whitelist::by_hash(hash)
end