Sha256: 24f06cb94ae538ac50113ac5e02d6c1bf41164c2e1cc73ff5ba01d356987311e

Contents?: true

Size: 905 Bytes

Versions: 10

Compression:

Stored size: 905 Bytes

Contents

class Epics::Key
  attr_accessor :key

  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

  ###
  # concat the exponent and modulus (hex representation) with a single whitespace
  # remove leading zeros from both
  # calculate digest (SHA256)
  # encode as Base64
  ####
  def public_digest
    c = [ e.gsub(/^0*/,''), n.gsub(/^0*/,'') ].map(&:downcase).join(" ")

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

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

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

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

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

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
epics-2.7.0 lib/epics/key.rb
epics-2.6.0 lib/epics/key.rb
epics-2.5.0 lib/epics/key.rb
epics-2.4.0 lib/epics/key.rb
epics-2.3.0 lib/epics/key.rb
epics-2.2.0 lib/epics/key.rb
epics-2.1.2 lib/epics/key.rb
epics-2.1.1 lib/epics/key.rb
epics-2.1.0 lib/epics/key.rb
epics-2.0.0 lib/epics/key.rb