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


40
41
42
# File 'lib/epics/key.rb', line 40

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

#recover(msg) ⇒ Object


36
37
38
# File 'lib/epics/key.rb', line 36

def recover(msg)
  mod_pow(OpenSSL::BN.new(msg.to_s, 2), self.key.e, self.key.n).to_s(2)
end

#sign(msg, salt = OpenSSL::Random.random_bytes(32)) ⇒ Object


32
33
34
# File 'lib/epics/key.rb', line 32

def sign(msg, salt = OpenSSL::Random.random_bytes(32) )
  Base64.encode64(mod_pow(OpenSSL::BN.new(emsa_pss(msg, salt).to_s, 2), self.key.d, self.key.n).to_s(2)).gsub("\n", "")
end