Sha256: 05c11ca49d0c767f46133eafb512f40749b1f943fe3efcdbefa8749f000641bf

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module COSE
  class Key
    class RSA < Key
      N = -1
      E = -2
      D = -3
      P = -4
      Q = -5
      DP = -6
      DQ = -7
      QI = -8

      ALGS = {
        PS256: -37,
        PS384: -38,
        PS512: -39,
        RSAES_OAEP_SHA1: -40,
        RSAES_OAEP_SHA256: -41,
        RSAES_OAEP_SHA512: -42
      }

      attr_accessor :n, :e, :d, :p, :q, :dp, :dq, :qi

      def initialize(attrs = {})
        super
        self.n = attrs[N]
        self.e = attrs[E]
        self.d = attrs[D]
        self.p = attrs[P]
        self.q = attrs[Q]
        self.dp = attrs[DP]
        self.dq = attrs[DQ]
        self.qi = attrs[QI]
      end

      def alg_key
        ALGS.invert[alg] or
        raise UknownAlgorithm, 'Unknown Algorithm'
      end

      def digest
        case alg_key
        when :RSAES_OAEP_SHA1
          OpenSSL::Digest::SHA1
        when :PS256, :RSAES_OAEP_SHA256
          OpenSSL::Digest::SHA256
        when :PS384
          OpenSSL::Digest::SHA384
        when :PS512, :RSAES_OAEP_SHA512
          OpenSSL::Digest::SHA512
        end.new
      end

      def to_key
        key = OpenSSL::PKey::RSA.new
        if key.respond_to? :set_key
          key.set_key n, e, d
          key.set_factors p, q if p && q
          key.set_crt_params dp, dq, qi if dp && dq && qi
        else
          key.e = e
          key.n = n
          key.d = d if d
          key.p = p if p
          key.q = q if q
          key.dmp1 = dp if dp
          key.dmq1 = dq if dq
          key.iqmp = qi if qi
        end
        key
      end

      def verify(signature, signature_base_string)
        to_key.verify_pss digest, signature, signature_base_string
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cose-key-0.2.0 lib/cose/key/rsa.rb