HEX
Server: Apache
System: Linux s198.coreserver.jp 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64
User: nagasaki (10062)
PHP: 7.1.33
Disabled: NONE
Upload Files
File: //usr/local/rvm/gems/ruby-2.5.9/gems/ruby-mysql-2.11.0/lib/mysql/authenticator/sha256_password.rb
require 'openssl'

class Mysql
  class Authenticator
    class Sha256Password
      # @param protocol [Mysql::Protocol]
      def initialize(protocol)
        @protocol = protocol
      end

      # @return [String]
      def name
        'sha256_password'
      end

      # @param passwd [String]
      # @param scramble [String]
      # @yield [String] hashed password
      # @return [Mysql::Packet]
      def authenticate(passwd, scramble)
        if @protocol.client_flags & CLIENT_SSL != 0
          yield passwd+"\0"
          return @protocol.read
        end
        yield "\x01"  # request public key
        pkt = @protocol.read
        data = pkt.to_s
        if data[0] == "\x01"
          pkt.utiny # skip
          pubkey = pkt.to_s
          hash = (passwd+"\0").unpack("C*").zip(scramble.unpack("C*")).map{|a, b| a ^ b}.pack("C*")
          enc = OpenSSL::PKey::RSA.new(pubkey).public_encrypt(hash, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
          @protocol.write enc
          pkt = @protocol.read
        end
        return pkt
      end
    end
  end
end