Sha256: a9bc4fca83634babdf0f3e9f8770b533b9ef3255c2c771dcb8fd5bfe8054ec77

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module JWT
  module Algos
    module Eddsa
      module_function

      SUPPORTED = %w[ED25519 EdDSA].freeze

      def sign(to_sign)
        algorithm, msg, key = to_sign.values
        if key.class != RbNaCl::Signatures::Ed25519::SigningKey
          raise EncodeError, "Key given is a #{key.class} but has to be an RbNaCl::Signatures::Ed25519::SigningKey"
        end
        unless SUPPORTED.map(&:downcase).map(&:to_sym).include?(algorithm.downcase.to_sym)
          raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key.primitive} signing key was provided"
        end

        key.sign(msg)
      end

      def verify(to_verify)
        algorithm, public_key, signing_input, signature = to_verify.values
        unless SUPPORTED.map(&:downcase).map(&:to_sym).include?(algorithm.downcase.to_sym)
          raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key.primitive} signing key was provided"
        end
        raise DecodeError, "key given is a #{public_key.class} but has to be a RbNaCl::Signatures::Ed25519::VerifyKey" if public_key.class != RbNaCl::Signatures::Ed25519::VerifyKey

        public_key.verify(signature, signing_input)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jwt-2.4.1 lib/jwt/algos/eddsa.rb
jwt-2.4.0 lib/jwt/algos/eddsa.rb
jwt-2.4.0.beta1 lib/jwt/algos/eddsa.rb