Class: JWT::JWA::Ecdsa
- Inherits:
-
Object
- Object
- JWT::JWA::Ecdsa
- Includes:
- SigningAlgorithm
- Defined in:
- lib/jwt/jwa/ecdsa.rb
Overview
ECDSA signing algorithm
Constant Summary collapse
- NAMED_CURVES =
{ 'prime256v1' => { algorithm: 'ES256', digest: 'sha256' }, 'secp256r1' => { # alias for prime256v1 algorithm: 'ES256', digest: 'sha256' }, 'secp384r1' => { algorithm: 'ES384', digest: 'sha384' }, 'secp521r1' => { algorithm: 'ES512', digest: 'sha512' }, 'secp256k1' => { algorithm: 'ES256K', digest: 'sha256' } }.freeze
Instance Attribute Summary
Attributes included from SigningAlgorithm
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(alg, digest) ⇒ Ecdsa
constructor
A new instance of Ecdsa.
- #sign(data:, signing_key:) ⇒ Object
- #verify(data:, signature:, verification_key:) ⇒ Object
Methods included from SigningAlgorithm
#header, #raise_sign_error!, #raise_verify_error!, #valid_alg?
Constructor Details
#initialize(alg, digest) ⇒ Ecdsa
Returns a new instance of Ecdsa.
9 10 11 12 |
# File 'lib/jwt/jwa/ecdsa.rb', line 9 def initialize(alg, digest) @alg = alg @digest = OpenSSL::Digest.new(digest) end |
Class Method Details
.curve_by_name(name) ⇒ Object
59 60 61 62 63 |
# File 'lib/jwt/jwa/ecdsa.rb', line 59 def self.curve_by_name(name) NAMED_CURVES.fetch(name) do raise UnsupportedEcdsaCurve, "The ECDSA curve '#{name}' is not supported" end end |
Instance Method Details
#sign(data:, signing_key:) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/jwt/jwa/ecdsa.rb', line 14 def sign(data:, signing_key:) curve_definition = curve_by_name(signing_key.group.curve_name) key_algorithm = curve_definition[:algorithm] raise IncorrectAlgorithm, "payload algorithm is #{alg} but #{key_algorithm} signing key was provided" if alg != key_algorithm asn1_to_raw(signing_key.dsa_sign_asn1(digest.digest(data)), signing_key) end |
#verify(data:, signature:, verification_key:) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/jwt/jwa/ecdsa.rb', line 22 def verify(data:, signature:, verification_key:) curve_definition = curve_by_name(verification_key.group.curve_name) key_algorithm = curve_definition[:algorithm] raise IncorrectAlgorithm, "payload algorithm is #{alg} but #{key_algorithm} verification key was provided" if alg != key_algorithm verification_key.dsa_verify_asn1(digest.digest(data), raw_to_asn1(signature, verification_key)) rescue OpenSSL::PKey::PKeyError raise JWT::VerificationError, 'Signature verification raised' end |