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)


98
99
100
# File 'lib/mihari/data_type.rb', line 98

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

.type(data) ⇒ String?

Returns:

  • (String, nil)


93
94
95
# File 'lib/mihari/data_type.rb', line 93

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

Instance Method Details

#detailed_typeString?

Returns:

  • (String, nil)


64
65
66
67
68
69
# File 'lib/mihari/data_type.rb', line 64

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)


35
36
37
38
39
40
# File 'lib/mihari/data_type.rb', line 35

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
31
32
# File 'lib/mihari/data_type.rb', line 28

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

#mail?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/mihari/data_type.rb', line 51

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

#md5?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/mihari/data_type.rb', line 72

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

#sha1?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/mihari/data_type.rb', line 77

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

#sha256?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/mihari/data_type.rb', line 82

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

#sha512?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/mihari/data_type.rb', line 87

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

#typeString?

Returns:

  • (String, nil)


56
57
58
59
60
61
# File 'lib/mihari/data_type.rb', line 56

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)


43
44
45
46
47
48
# File 'lib/mihari/data_type.rb', line 43

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