Module: JsonWebToken::Jwa

Defined in:
lib/json_web_token/jwa.rb

Overview

Choose a cryptographic algorithm to be used for a JSON Web Signature (JWS)

Constant Summary collapse

ALGORITHMS =
/(HS|RS|ES)(256|384|512)?/i
ALG_LENGTH =
5

Class Method Summary collapse

Class Method Details

.sign(algorithm, key, signing_input) ⇒ BinaryString

Returns a digital signature, or mac.

Examples:

key = 'gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C'
Jwa.sign('HS256', key, 'signing_input').bytes
# => [90, 34, 44, 252, 147, 130, 167, 173, 86, 191, 247, 93, 94, 12, 200, 30, 173, 115, 248, 89, 246, 222, 4, 213, 119, 74, 70, 20, 231, 194, 104, 103]

Parameters:

  • algorithm (String)

    ‘alg’ header parameter value for JWS

  • key (String | OpenSSL::PKey::RSA | OpenSSL::PKey::EC)

    secret key used to sign a digital signature, or mac

  • signing_input (String)

    input payload for a mac computation

Returns:

  • (BinaryString)

    a digital signature, or mac



24
25
26
27
# File 'lib/json_web_token/jwa.rb', line 24

def sign(algorithm, key, signing_input)
  alg_module, sha_bits = validated_alg(algorithm)
  alg_module.sign(sha_bits, key, signing_input)
end

.verify?(mac, algorithm, key, signing_input) ⇒ Boolean

Examples:

key = 'gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C'
Jwa.verify?(< binary_string >, 'HS256', key, 'signing_input')
# => true

Parameters:

  • mac (BinaryString)

    a digital signature, or mac

  • algorithm (String)

    ‘alg’ header parameter value for JWS

  • key (String | OpenSSL::PKey::RSA | OpenSSL::PKey::EC)

    key used to verify a digital signature, or mac

  • signing_input (String)

    input payload for a mac computation

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/json_web_token/jwa.rb', line 38

def verify?(mac, algorithm, key, signing_input)
  alg_module, sha_bits = validated_alg(algorithm)
  alg_module.verify?(mac, sha_bits, key, signing_input)
end