Sha256: 369a86789865298e96ebd5afce8e530391ede2c6d438996bf21d9e6c69ac6104

Contents?: true

Size: 973 Bytes

Versions: 5

Compression:

Stored size: 973 Bytes

Contents

# frozen_string_literal: true

module OmniAuth
  module DoximityOauth2
    # Static crypto methods
    class Crypto
      class << self
        def 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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
omniauth-doximity-oauth2-1.5.0 lib/omniauth-doximity-oauth2/crypto.rb
omniauth-doximity-oauth2-1.4.0 lib/omniauth-doximity-oauth2/crypto.rb
omniauth-doximity-oauth2-1.3.0 lib/omniauth-doximity-oauth2/crypto.rb
omniauth-doximity-oauth2-1.2.0 lib/omniauth-doximity-oauth2/crypto.rb
omniauth-doximity-oauth2-1.2.0.pre.3 lib/omniauth-doximity-oauth2/crypto.rb