Module: JsonWebToken::Util

Defined in:
lib/json_web_token/util.rb

Overview

Utility methods

Class Method Summary collapse

Class Method Details

.constant_time_compare?(a, b) ⇒ Boolean

Returns a predicate that compares two strings for equality in constant-time to avoid timing attacks.

Examples:

Util.constant_time_compare?("a", "A")
# => false

Parameters:

  • a (String)
  • b (String)

Returns:

  • (Boolean)

    a predicate that compares two strings for equality in constant-time to avoid timing attacks

See Also:



16
17
18
19
# File 'lib/json_web_token/util.rb', line 16

def constant_time_compare?(a, b)
  return false if a.nil? || b.nil? || a.empty? || b.empty?
  secure_compare(a, b)
end

.symbolize_keys(hsh) ⇒ Hash

Returns a new hash with all keys converted to symbols, as long as they respond to to_sym.

Examples:

Util.symbolize_keys({'a' =>  0, 'b' => '2', c: '3'})
# => {a: 0, b: '2', c: '3'}

Parameters:

  • hsh (Hash)

Returns:

  • (Hash)

    a new hash with all keys converted to symbols, as long as they respond to to_sym

See Also:

  • rails activesupport/lib/active_support/core_ext/hash/keys.rb


28
29
30
# File 'lib/json_web_token/util.rb', line 28

def symbolize_keys(hsh)
  transform_keys(hsh) { |key| key.to_sym rescue key }
end