module OrangeData::Credentials::KeyEncoding

nodoc

Public Instance Methods

from_hash(hash) click to toggle source
# File lib/orange_data/credentials.rb, line 52
def from_hash(hash)
  OpenSSL::PKey::RSA.new.tap do |key|
    if key.respond_to?(:set_key)
      # ruby 2.5+
      # a bit ugly - simulating with_indifferent_access
      if hash['n'] || hash[:n]
        # public key only has n and e (without them - there's no key actually)
        key.set_key(
          OpenSSL::BN.new(Base64.decode64(hash['n'] || hash[:n]), 2),
          OpenSSL::BN.new(Base64.decode64(hash['e'] || hash[:e]), 2),
          (hash['d'] || hash[:d]) && OpenSSL::BN.new(Base64.decode64(hash['d'] || hash[:d]), 2)
        )
      end

      if hash['p'] || hash[:p]
        key.set_factors(
          OpenSSL::BN.new(Base64.decode64(hash['p'] || hash[:p]), 2),
          OpenSSL::BN.new(Base64.decode64(hash['q'] || hash[:q]), 2)
        )
        if hash['dmp1'] || hash[:dmp1]
          key.set_crt_params(
            OpenSSL::BN.new(Base64.decode64(hash['dmp1'] || hash[:dmp1]), 2),
            OpenSSL::BN.new(Base64.decode64(hash['dmq1'] || hash[:dmq1]), 2),
            OpenSSL::BN.new(Base64.decode64(hash['iqmp'] || hash[:iqmp]), 2)
          )
        end
      end
    else
      # ruby 2.3 and may be older
      key.params.keys.each do |param|
        if (v = hash[param] || hash[param.to_sym])
          key.send(:"#{param}=", OpenSSL::BN.new(Base64.decode64(v), 2))
        end
      end
    end
  end
end
from_xml(xml) click to toggle source
# File lib/orange_data/credentials.rb, line 30
def from_xml(xml)
  require "rexml/document"
  kv = REXML::Document.new(xml).elements['RSAKeyValue']
  raise ArgumentError, 'no RSAKeyValue in xml' unless kv && kv.name == 'RSAKeyValue'

  mapping = {
    "Modulus" => :n,
    "Exponent" => :e,

    "D" => :d,
    "P" => :p,
    "Q" => :q,

    "DP" => :dmp1,
    "DQ" => :dmq1,
    "InverseQ" => :iqmp
  }
  from_hash(
    kv.elements.each_with_object({}){|k, h| h[mapping[k.name]] = k.text if mapping[k.name] }
  )
end
load_from(val, key_pass=nil) click to toggle source
# File lib/orange_data/credentials.rb, line 90
def load_from(val, key_pass=nil)
  return val unless val

  case val
  when self
    val
  when Hash
    from_hash(val)
  when String
    if val.start_with?('<')
      from_xml(val)
    else
      new(val, key_pass)
    end
  else
    raise ArgumentError, "cannot load from #{val.class}"
  end
end
to_hash() click to toggle source
# File lib/orange_data/credentials.rb, line 24
def to_hash
  params.map{|k, v| v != 0 && [k, Base64.strict_encode64(v.to_s(2))] || nil }.compact.to_h
end
to_xml() click to toggle source
# File lib/orange_data/credentials.rb, line 16
def to_xml
  h_params = to_hash
  h = { 'Modulus' => :n, 'Exponent' => :e }
  h.merge!('P' => :p, 'Q' => :q, 'DP' => :dmp1, 'DQ' => :dmq1, 'InverseQ' => :iqmp, 'D' => :d) if private?

  "<RSAKeyValue>#{h.map{|(k, v)| "<#{k}>#{h_params[v.to_s]}</#{k}>" }.join('')}</RSAKeyValue>"
end