Class: Mysql::Authenticator::MysqlNativePassword

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql/authenticator/mysql_native_password.rb

Overview

mysql_native_password

Instance Method Summary collapse

Constructor Details

#initialize(protocol) ⇒ MysqlNativePassword

Returns a new instance of MysqlNativePassword.

Parameters:



8
9
10
# File 'lib/mysql/authenticator/mysql_native_password.rb', line 8

def initialize(protocol)
  @protocol = protocol
end

Instance Method Details

#authenticate(passwd, scramble) {|String| ... } ⇒ Mysql::Packet

Parameters:

  • passwd (String)
  • scramble (String)

Yields:

  • (String)

    hashed password

Returns:



21
22
23
24
# File 'lib/mysql/authenticator/mysql_native_password.rb', line 21

def authenticate(passwd, scramble)
  yield hash_password(passwd, scramble)
  @protocol.read
end

#hash_password(passwd, scramble) ⇒ String

Returns hashed password.

Parameters:

  • passwd (String)
  • scramble (String)

Returns:

  • (String)

    hashed password



29
30
31
32
33
34
35
# File 'lib/mysql/authenticator/mysql_native_password.rb', line 29

def hash_password(passwd, scramble)
  return '' if passwd.nil? or passwd.empty?
  hash1 = Digest::SHA1.digest(passwd)
  hash2 = Digest::SHA1.digest(hash1)
  hash3 = Digest::SHA1.digest(scramble + hash2)
  hash1.unpack("C*").zip(hash3.unpack("C*")).map{|a, b| a ^ b}.pack("C*")
end

#nameString

Returns:

  • (String)


13
14
15
# File 'lib/mysql/authenticator/mysql_native_password.rb', line 13

def name
  'mysql_native_password'
end