Sha256: 53c6e2933dfb2e96af2714ee7df2896138f53c2f071e19cc5809b58d653bc621

Contents?: true

Size: 997 Bytes

Versions: 2

Compression:

Stored size: 997 Bytes

Contents

class Acme::Crypto
  attr_reader :private_key

  def initialize(private_key)
    @private_key = private_key
  end

  def generate_signed_jws(header:, payload:)
    protection_header = generate_protection_header(header)
    payload = encode64(JSON.dump(payload))

    JSON.dump(
      {
        header: { alg: :RS256, jwk: jwk },
        protected: protection_header,
        payload: payload,
        signature: generate_signature(protection_header, payload)
      }
    )
  end

  def generate_signature(protection_header, payload)
    input = "#{protection_header}.#{payload}"
    signature = private_key.sign(digest, input)
    encode64(signature)
  end

  def generate_protection_header(header)
    encode64(JSON.dump(header))
  end

  def jwk
    JSON::JWK.new(public_key)
  end

  def thumbprint
    jwk.thumbprint
  end

  def public_key
    private_key.public_key
  end

  def digest
    OpenSSL::Digest::SHA256.new
  end

  def encode64(input)
    UrlSafeBase64.encode64(input)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acme-client-0.2.0 lib/acme/crypto.rb
acme-client-0.1.3 lib/acme/crypto.rb