Sha256: 7452ee0e486be684ca27f46626f19bbb2a5f993b5b2c68562b42c119060c4396

Contents?: true

Size: 912 Bytes

Versions: 1

Compression:

Stored size: 912 Bytes

Contents

# frozen_string_literal: true

module Omniauth
  module DoximityOauth2
    # Static crypto methods
    class Crypto
      def self.create_rsa_key(n, e)
        data_sequence = OpenSSL::ASN1::Sequence([
                                                  OpenSSL::ASN1::Integer(base64_to_long(n)),
                                                  OpenSSL::ASN1::Integer(base64_to_long(e))
                                                ])
        asn1 = OpenSSL::ASN1::Sequence(data_sequence)
        OpenSSL::PKey::RSA.new(asn1.to_der)
      end

      private

      def base64_to_long(data)
        decoded_with_padding = Base64.urlsafe_decode64(data) + Base64.decode64("==")
        decoded_with_padding.to_s.unpack("C*").map do |byte|
          byte_to_hex(byte)
        end.join.to_i(16)
      end

      def byte_to_hex(int)
        int < 16 ? "0#{int.to_s(16)}" : int.to_s(16)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omniauth-doximity-oauth2-1.2.0.pre lib/omniauth-doximity-oauth2/crypto.rb