Class: Epics::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/epics/key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoded_key, passphrase = nil) ⇒ Key

Returns a new instance of Key.



4
5
6
7
8
9
10
# File 'lib/epics/key.rb', line 4

def initialize(encoded_key, passphrase = nil)
  if encoded_key.kind_of?(OpenSSL::PKey::RSA)
    self.key = encoded_key
  else
    self.key = OpenSSL::PKey::RSA.new(encoded_key)
  end
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



2
3
4
# File 'lib/epics/key.rb', line 2

def key
  @key
end

Instance Method Details

#digesterObject



43
44
45
# File 'lib/epics/key.rb', line 43

def digester
  @digester ||= OpenSSL::Digest::SHA256.new
end

#eObject



28
29
30
# File 'lib/epics/key.rb', line 28

def e
  self.key.e.to_s(16)
end

#nObject



24
25
26
# File 'lib/epics/key.rb', line 24

def n
  self.key.n.to_s(16)
end

#public_digestObject

concat the exponent and modulus (hex representation) with a single whitespace remove leading zeros from both calculate digest (SHA256) encode as Base64



18
19
20
21
22
# File 'lib/epics/key.rb', line 18

def public_digest
  c = [ e.gsub(/^0*/,''), n.gsub(/^0*/,'') ].map(&:downcase).join(" ")

  Base64.encode64(digester.digest(c)).strip
end

#sign(msg) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/epics/key.rb', line 32

def sign(msg)
  Base64.encode64(
    key.sign_pss(
      'SHA256',
      msg,
      salt_length: :digest,
      mgf1_hash:   'SHA256',
    ),
  ).gsub("\n", '')
end