Sha256: ebca8be884b5ef79f5ca1a4080b2c9be800ab802dfea441d5386d936edc62058

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module JWT
  module JWA
    class Ps
      include JWT::JWA::SigningAlgorithm

      def initialize(alg)
        @alg = alg
        @digest_algorithm = alg.sub('PS', 'sha')
      end

      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_pss(digest_algorithm, data, salt_length: :digest, mgf1_hash: digest_algorithm)
      end

      def verify(data:, signature:, verification_key:)
        verification_key.verify_pss(digest_algorithm, signature, data, salt_length: :auto, mgf1_hash: digest_algorithm)
      rescue OpenSSL::PKey::PKeyError
        raise JWT::VerificationError, 'Signature verification raised'
      end

      register_algorithm(new('PS256'))
      register_algorithm(new('PS384'))
      register_algorithm(new('PS512'))

      private

      attr_reader :digest_algorithm
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jwt-2.9.3 lib/jwt/jwa/ps.rb
jwt-2.9.2 lib/jwt/jwa/ps.rb
jwt-2.9.1 lib/jwt/jwa/ps.rb
jwt-2.9.0 lib/jwt/jwa/ps.rb