Class: JWT::JWA::Rsa

Inherits:
Object
  • Object
show all
Includes:
SigningAlgorithm
Defined in:
lib/jwt/jwa/rsa.rb

Instance Attribute Summary

Attributes included from SigningAlgorithm

#alg

Instance Method Summary collapse

Methods included from SigningAlgorithm

#header, included, #raise_sign_error!, #raise_verify_error!, #valid_alg?

Constructor Details

#initialize(alg) ⇒ Rsa

Returns a new instance of Rsa.



8
9
10
11
# File 'lib/jwt/jwa/rsa.rb', line 8

def initialize(alg)
  @alg = alg
  @digest = OpenSSL::Digest.new(alg.sub('RS', 'SHA'))
end

Instance Method Details

#sign(data:, signing_key:) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/jwt/jwa/rsa.rb', line 13

def sign(data:, signing_key:)
  unless signing_key.is_a?(OpenSSL::PKey::RSA)
    raise_sign_error!("The given key is a #{signing_key.class}. It has to be an OpenSSL::PKey::RSA instance")
  end

  signing_key.sign(digest, data)
end

#verify(data:, signature:, verification_key:) ⇒ Object



21
22
23
24
25
# File 'lib/jwt/jwa/rsa.rb', line 21

def verify(data:, signature:, verification_key:)
  verification_key.verify(digest, signature, data)
rescue OpenSSL::PKey::PKeyError
  raise JWT::VerificationError, 'Signature verification raised'
end