Class: Acme::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/acme/crypto.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key) ⇒ Crypto

Returns a new instance of Crypto.



4
5
6
# File 'lib/acme/crypto.rb', line 4

def initialize(private_key)
  @private_key = private_key
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



2
3
4
# File 'lib/acme/crypto.rb', line 2

def private_key
  @private_key
end

Instance Method Details

#digestObject



40
41
42
# File 'lib/acme/crypto.rb', line 40

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

#encode64(input) ⇒ Object



44
45
46
# File 'lib/acme/crypto.rb', line 44

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

#generate_protection_header(header) ⇒ Object



28
29
30
# File 'lib/acme/crypto.rb', line 28

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

#generate_signature(protection_header, payload) ⇒ Object



22
23
24
25
26
# File 'lib/acme/crypto.rb', line 22

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

#generate_signed_jws(header:, payload:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/acme/crypto.rb', line 8

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

#jwkObject



32
33
34
# File 'lib/acme/crypto.rb', line 32

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

#public_keyObject



36
37
38
# File 'lib/acme/crypto.rb', line 36

def public_key
  private_key.public_key
end