Sha256: caff98a939127585fbf64d53350bd3194c4b4a47c56f95f4e562a22114507a9f

Contents?: true

Size: 858 Bytes

Versions: 4

Compression:

Stored size: 858 Bytes

Contents

# frozen_string_literal: true

require 'jwt/security_utils'
require 'openssl'
require 'jwt/algos'
begin
  require 'rbnacl'
rescue LoadError
  raise if defined?(RbNaCl)
end

# JWT::Signature module
module JWT
  # Signature logic for JWT
  module Signature
    module_function

    ToSign = Struct.new(:algorithm, :msg, :key)
    ToVerify = Struct.new(:algorithm, :public_key, :signing_input, :signature)

    def sign(algorithm, msg, key)
      algo, code = Algos.find(algorithm)
      algo.sign ToSign.new(code, msg, key)
    end

    def verify(algorithm, key, signing_input, signature)
      algo, code = Algos.find(algorithm)
      algo.verify(ToVerify.new(code, key, signing_input, signature))
    rescue OpenSSL::PKey::PKeyError
      raise JWT::VerificationError, 'Signature verification raised'
    ensure
      OpenSSL.errors.clear
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jwt-2.5.0 lib/jwt/signature.rb
jwt-2.4.1 lib/jwt/signature.rb
jwt-2.4.0 lib/jwt/signature.rb
jwt-2.4.0.beta1 lib/jwt/signature.rb