Class: Mihari::DataType

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

Overview

(Artifact) Data Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ DataType

Returns a new instance of DataType.

Parameters:

  • data (String)

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/mihari/data_type.rb', line 16

def initialize(data)
  raise ArgumentError if data.is_a?(Hash)

  @data = data.to_s
end

Instance Attribute Details

#dataString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/mihari/data_type.rb', line 11

def data
  @data
end

Class Method Details

.detailed_type(data) ⇒ String?

Returns:

  • (String, nil)


96
97
98
# File 'lib/mihari/data_type.rb', line 96

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

.type(data) ⇒ String?

Returns:

  • (String, nil)


91
92
93
# File 'lib/mihari/data_type.rb', line 91

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

Instance Method Details

#detailed_typeString?

Returns:

  • (String, nil)


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

def detailed_type
  found = %i[md5? sha1? sha256? sha512?].find { |method| send(method) if respond_to?(method) }
  return found[...-1].to_s unless found.nil?

  type
end

#domain?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/mihari/data_type.rb', line 33

def domain?
  Try[Addressable::URI::InvalidURIError] do
    uri = Addressable::URI.parse("http://#{data}")
    uri.host == data && PublicSuffix.valid?(uri.host)
  end.recover { false }.value!
end

#hash?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/mihari/data_type.rb', line 23

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

#ip?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/mihari/data_type.rb', line 28

def ip?
  Try[IPAddr::InvalidAddressError] { IPAddr.new(data).to_s == data }.recover { false }.value!
end

#mail?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/mihari/data_type.rb', line 49

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

#md5?Boolean

Returns:

  • (Boolean)


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

def md5?
  data.match?(/^[A-Fa-f0-9]{32}$/)
end

#sha1?Boolean

Returns:

  • (Boolean)


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

def sha1?
  data.match?(/^[A-Fa-f0-9]{40}$/)
end

#sha256?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/mihari/data_type.rb', line 80

def sha256?
  data.match?(/^[A-Fa-f0-9]{64}$/)
end

#sha512?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/mihari/data_type.rb', line 85

def sha512?
  data.match?(/^[A-Fa-f0-9]{128}$/)
end

#typeString?

Returns:

  • (String, nil)


54
55
56
57
58
59
# File 'lib/mihari/data_type.rb', line 54

def type
  found = %i[hash? ip? domain? url? mail?].find { |method| send(method) if respond_to?(method) }
  return nil if found.nil?

  found[...-1].to_s
end

#url?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/mihari/data_type.rb', line 41

def url?
  Try[Addressable::URI::InvalidURIError] do
    uri = Addressable::URI.parse(data)
    uri.scheme && uri.host && uri.path && PublicSuffix.valid?(uri.host)
  end.recover { false }.value!
end