Sha256: 43c64c78802cdcec02a6cd56f6746e8728baa13960c3f5af93bf4e3159f3c073

Contents?: true

Size: 1.15 KB

Versions: 25

Compression:

Stored size: 1.15 KB

Contents

class Acme::Client::JWK::RSA < Acme::Client::JWK::Base
  # Digest algorithm to use when signing.
  DIGEST = OpenSSL::Digest::SHA256

  # Instantiate a new RSA JWK.
  #
  # private_key - A OpenSSL::PKey::RSA instance.
  #
  # Returns nothing.
  def initialize(private_key)
    unless private_key.is_a?(OpenSSL::PKey::RSA)
      raise ArgumentError, 'private_key must be a OpenSSL::PKey::RSA'
    end

    @private_key = private_key
  end

  # Get this JWK as a Hash for JSON serialization.
  #
  # Returns a Hash.
  def to_h
    {
      e: Acme::Client::Util.urlsafe_base64(public_key.e.to_s(2)),
      kty: 'RSA',
      n: Acme::Client::Util.urlsafe_base64(public_key.n.to_s(2))
    }
  end

  # Sign a message with the private key.
  #
  # message - A String message to sign.
  #
  # Returns a String signature.
  def sign(message)
    @private_key.sign(DIGEST.new, message)
  end

  # The name of the algorithm as needed for the `alg` member of a JWS object.
  #
  # Returns a String.
  def jwa_alg
    # https://tools.ietf.org/html/rfc7518#section-3.1
    # RSASSA-PKCS1-v1_5 using SHA-256
    'RS256'
  end

  private

  def public_key
    @private_key.public_key
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
acme-client-2.0.19 lib/acme/client/jwk/rsa.rb
acme-client-2.0.18 lib/acme/client/jwk/rsa.rb
acme-client-2.0.17 lib/acme/client/jwk/rsa.rb
acme-client-2.0.16 lib/acme/client/jwk/rsa.rb
acme-client-2.0.15 lib/acme/client/jwk/rsa.rb
acme-client-2.0.14 lib/acme/client/jwk/rsa.rb
acme-client-2.0.13 lib/acme/client/jwk/rsa.rb
acme-client-2.0.12 lib/acme/client/jwk/rsa.rb
acme-client-2.0.11 lib/acme/client/jwk/rsa.rb
acme-client-2.0.10 lib/acme/client/jwk/rsa.rb
acme-client-2.0.9 lib/acme/client/jwk/rsa.rb
acme-client-2.0.8 lib/acme/client/jwk/rsa.rb
acme-client-2.0.7 lib/acme/client/jwk/rsa.rb
acme-client-2.0.6 lib/acme/client/jwk/rsa.rb
acme-client-2.0.5 lib/acme/client/jwk/rsa.rb
acme-client-2.0.4 lib/acme/client/jwk/rsa.rb
acme-client-2.0.3 lib/acme/client/jwk/rsa.rb
acme-client-2.0.2 lib/acme/client/jwk/rsa.rb
acme-client-2.0.1 lib/acme/client/jwk/rsa.rb
acme-client-2.0.0 lib/acme/client/jwk/rsa.rb