理论知识部分摘录自: http://blog.chinaunix.net/uid-9234131-id-5088292.html http://blog.csdn.net/sissiyinxi/article/details/7660389
1.服务器端会随机生成一个random string发送给客户端;
2.客户端收到random string后,进行hash加密
第一步,将密码hash,得到hash值hash_stage1; eg.hash_stage1=sha1("password");
第二步,二次hash,得到hash_stage2; eg. hash_stage2=sha1(hash_stage1);
第三步,将密码二次hash得到的值与random string进行hash,得到hash_stage3; eg. hash_stage3=sha1("randomstring",hash_stage2);
第四步,异或处理准备发送给服务器端,得到reply=xor(hash_stage1,hash_stage3);
最后,将reply的值发送给服务器端。
3.服务器端收到reply后同样进行hash运算
第一步,将保存的hash形式的密码hashpassword与random string进行hash,得到server_hash_stage1=sha1("randomstring","hashpassword");
第二步,将客户端发送的reply与刚才得到的hash值进行异或运算,得到xor_value; eg. xor_value=xor(reply,server_hash_stage1);
第三步,将得到的异或值进行hash,得到server_hash_stage2; eg. server_hash_stage2=sha1(server_hash_stage1);
第四步,验证,将最后得到的hash值server_hash_stage2与保存的密码hashpassword进行比较。eg. server_hash_stage2==hashpassword,相等则验证通过。 伪代码如下:
SERVER: public_seed=create_random_string()
send(public_seed)
CLIENT: recv(public_seed)
hash_stage1=sha1("password")
hash_stage2=sha1(hash_stage1)
reply=xor(hash_stage1,sha1(public_seed,hash_stage2)
// this three steps are done inscramble()
send(reply)
SERVER: recv(reply)
hash_stage1=xor(reply,sha1(public_seed,hash_stage2))
candidate_hash2=sha1(hash_stage1)
check(candidate_hash2==hash_stage2)
// this three steps are done incheck_scramble() 官方文档:
MySQL uses passwords in two phasesof client/server communication:
When a client attempts to connectto the server, there is an initial authentication step in which the client mustpresent a password that has a hash value matching the hash value stored in theuser table for the account the client wants to use.
After the client connects, it can(if it has sufficient privileges) set or change the password hash for accountslisted in the user table. The client can do this by using the PASSWORD()function to generate a password hash, or by using a password-generatingstatement (CREATE USER, GRANT, or SET PASSWORD).
> SELECT PASSWORD('Abcd@1234'); # mysql用户密码的计算方式:(hash方式)
+-------------------------------------------+
| PASSWORD('Abcd@1234') |
|-------------------------------------------|
|*47B150E012313114C04A1C9336709424085B6BD0 |
+-------------------------------------------+ 使用wireshark抓取通过navicat登录虚拟机的mysql-5.7的部分截图: