class HealthCards::PrivateKey
A key used for signing JWS
Public Class Methods
from_file(path)
click to toggle source
# File lib/health_cards/private_key.rb, line 6 def self.from_file(path) pem = OpenSSL::PKey::EC.new(File.read(path)) PrivateKey.new(pem) end
generate_key(file_path: nil)
click to toggle source
# File lib/health_cards/private_key.rb, line 19 def self.generate_key(file_path: nil) key = OpenSSL::PKey::EC.generate('prime256v1') File.write(file_path, key.to_pem) if file_path PrivateKey.new(key) end
load_from_or_create_from_file(path)
click to toggle source
# File lib/health_cards/private_key.rb, line 11 def self.load_from_or_create_from_file(path) if File.exist?(path) from_file(path) else generate_key(file_path: path) end end
Public Instance Methods
public_key()
click to toggle source
# File lib/health_cards/private_key.rb, line 29 def public_key return @public_key if @public_key pub = OpenSSL::PKey::EC.new('prime256v1') pub.public_key = @key.public_key @public_key = PublicKey.new(pub) end
sign(payload)
click to toggle source
# File lib/health_cards/private_key.rb, line 25 def sign(payload) asn1_to_raw(@key.sign(OpenSSL::Digest.new('SHA256'), payload), self) end
Private Instance Methods
asn1_to_raw(signature, private_key)
click to toggle source
Convert the ASN.1 Representation into the raw signature
Adapted from ruby-jwt and json-jwt gems. More info here: github.com/nov/json-jwt/issues/21 github.com/jwt/ruby-jwt/pull/87 github.com/jwt/ruby-jwt/issues/84
# File lib/health_cards/private_key.rb, line 45 def asn1_to_raw(signature, private_key) byte_size = (private_key.group.degree + 7) / 8 OpenSSL::ASN1.decode(signature).value.map { |value| value.value.to_s(2).rjust(byte_size, "\x00") }.join end