Module: WebriskHash::Prefixes

Extended by:
Prefixes
Included in:
WebriskHash, Prefixes
Defined in:
lib/webrisk_hash/prefixes.rb

Instance Method Summary collapse

Instance Method Details

#get_prefix_map(url, size = 32 * 8) ⇒ Array<Array>

Get hash prefix map for a URL

Returns an array of [expression, hash_prefix] pairs for all suffix/prefix expressions of the URL

Examples:

get_prefix_map("http://a.b.c/1/2.html?param=1")
# => [
#      ["a.b.c/1/2.html?param=1", "\xAB\xCD..."],
#      ["a.b.c/1/2.html", "\x12\x34..."],
#      ...
#    ]

Parameters:

  • url (String)

    The URL to process

  • size (Integer) (defaults to: 32 * 8)

    Hash prefix size in bits (default: 256 bits = 32 bytes)

Returns:

  • (Array<Array>)

    Array of [expression, hash_prefix] pairs



24
25
26
27
28
29
# File 'lib/webrisk_hash/prefixes.rb', line 24

def get_prefix_map(url, size = 32 * 8)
  canonical = WebriskHash.canonicalize(url)
  return [] if canonical.nil?

  WebriskHash.suffix_postfix_expressions(canonical).to_a.map { |u| [u, WebriskHash::Hash.truncated_sha256_prefix(u, size)] }
end

#get_prefixes(url, size = 32 * 8) ⇒ Set<String>

Get hash prefixes for a URL

Returns a set of hash prefixes for all suffix/prefix expressions of the URL. This is the main method used to check URLs against Web Risk lists.

Examples:

Get 32-bit hash prefixes

get_prefixes("http://evil.com/malware", 32)
# => #<Set: {"\xAB\xCD\xEF\x12", "\x34\x56\x78\x90", ...}>

Get full 256-bit hashes (default)

get_prefixes("http://example.com/")
# => #<Set: {"\xAB\xCD...(32 bytes)", ...}>

Parameters:

  • url (String)

    The URL to process

  • size (Integer) (defaults to: 32 * 8)

    Hash prefix size in bits (default: 256 bits = 32 bytes)

Returns:

  • (Set<String>)

    Set of binary hash prefixes



49
50
51
52
53
54
55
56
# File 'lib/webrisk_hash/prefixes.rb', line 49

def get_prefixes(url, size = 32 * 8)
  canonical = WebriskHash.canonicalize(url)
  return Set.new if canonical.nil?

  Set.new(WebriskHash.suffix_postfix_expressions(canonical).to_a.map do |u|
    WebriskHash::Hash.truncated_sha256_prefix(u, size)
  end)
end