Class: Mihari::TypeChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/mihari/type_checker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TypeChecker

Returns a new instance of TypeChecker.



12
13
14
# File 'lib/mihari/type_checker.rb', line 12

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/mihari/type_checker.rb', line 10

def data
  @data
end

Class Method Details

.detailed_type(data) ⇒ String?

Returns:

  • (String, nil)


75
76
77
# File 'lib/mihari/type_checker.rb', line 75

def self.detailed_type(data)
  new(data).detailed_type
end

.type(data) ⇒ String?

Returns:

  • (String, nil)


70
71
72
# File 'lib/mihari/type_checker.rb', line 70

def self.type(data)
  new(data).type
end

Instance Method Details

#detailed_typeString?

Returns:

  • (String, nil)


60
61
62
63
64
65
66
67
# File 'lib/mihari/type_checker.rb', line 60

def detailed_type
  return "md5" if md5?
  return "sha1" if sha1?
  return "sha256" if sha256?
  return "sha512" if sha512?

  type
end

#domain?true, false

Returns:

  • (true, false)


30
31
32
33
34
35
# File 'lib/mihari/type_checker.rb', line 30

def domain?
  uri = Addressable::URI.parse("http://#{data}")
  uri.host == data && PublicSuffix.valid?(uri.host)
rescue Addressable::URI::InvalidURIError => _e
  false
end

#hash?true, false

Returns:

  • (true, false)


17
18
19
# File 'lib/mihari/type_checker.rb', line 17

def hash?
  md5? || sha1? || sha256? || sha512?
end

#ip?true, false

Returns:

  • (true, false)


22
23
24
25
26
27
# File 'lib/mihari/type_checker.rb', line 22

def ip?
  IPAddr.new data
  true
rescue IPAddr::InvalidAddressError => _e
  false
end

#mail?true, false

Returns:

  • (true, false)


46
47
48
# File 'lib/mihari/type_checker.rb', line 46

def mail?
  EmailAddress.valid? data, host_validation: :syntax
end

#typeString?

Returns:

  • (String, nil)


51
52
53
54
55
56
57
# File 'lib/mihari/type_checker.rb', line 51

def type
  return "hash" if hash?
  return "ip" if ip?
  return "domain" if domain?
  return "url" if url?
  return "mail" if mail?
end

#url?true, false

Returns:

  • (true, false)


38
39
40
41
42
43
# File 'lib/mihari/type_checker.rb', line 38

def url?
  uri = Addressable::URI.parse(data)
  uri.scheme && uri.host && uri.path && PublicSuffix.valid?(uri.host)
rescue Addressable::URI::InvalidURIError => _e
  false
end