The MD5 algorithm not secure. Can be decoded with a big database.
So PHP has created a new function better than. But that's not the reason let us leave the MD5 function. It is also useful in encryption. Get the same only 32 digits code every time.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3VToVi3_gZdaXdIEbiuejLa1KV4tw7_y3Cd-lJkRuNbhPdFDh27pghuiMs7jmvO7uNunkT8KW79gGGpOAcVUEGpf3lFvtskjL6zxdEAR64ZFo48I0hHzas5JAZL-VyeujGDaDJ8QIOek/s640/md5+to+hash.jpg)
But for the security of the code. We need to merge data the data with secret key. When decrypt code, it can not understand. Encode md5() first and running password_hash() again.
This below is sample code
PHP CI MANIA - PHP Code Generator
So PHP has created a new function better than. But that's not the reason let us leave the MD5 function. It is also useful in encryption. Get the same only 32 digits code every time.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3VToVi3_gZdaXdIEbiuejLa1KV4tw7_y3Cd-lJkRuNbhPdFDh27pghuiMs7jmvO7uNunkT8KW79gGGpOAcVUEGpf3lFvtskjL6zxdEAR64ZFo48I0hHzas5JAZL-VyeujGDaDJ8QIOek/s640/md5+to+hash.jpg)
But for the security of the code. We need to merge data the data with secret key. When decrypt code, it can not understand. Encode md5() first and running password_hash() again.
This below is sample code
<?php
header('Content-Type: text/html; charset=utf-8');
function pass_encrypt($pass, $show=false){
//you secret word
$key1 = 'asdfasf';
$key2 = 'asdfasdf';
$loop = 1;
$reverse = strrev($pass);
if($show==true){echo '<br> กลับตัวอักษร : ' , $reverse;}
for($i=0;$i<$loop;$i++){
$md5 = md5($reverse);
if($show==true){echo '<br> เข้ารหัสเป็น 32 หลัก : ' , $md5;}
$reverse_md5 = strrev($md5);
if($show==true){echo '<br> กลับตัวอักษร : ' , $reverse_md5;}
$salt = substr($reverse_md5, -13) . md5($key1) . substr($reverse_md5, 0, 19) . md5($key2);
if($show==true){echo '<br> สร้างข้อความใหม่ : ' , $salt;}
$new_md5 = md5($salt);
if($show==true){echo '<br> เข้ารหัสเป็น 32 หลัก : ' , $new_md5;}
$reverse = strrev($new_md5);
if($show==true){echo '<br> กลับตัวอักษรอีกครั้ง : ' , $reverse;}
}
return md5($reverse);
}
$pass = "love999";
echo '<br> md5() ธรรมดา : ' , md5($pass);
//เข้ารหัส md5 ก่อน
$encrypt = pass_encrypt($pass, true);
// และเข้ารหัส hash เพื่อนำไปบันทึกลงฐานข้อมูล
$hash = password_hash($encrypt, PASSWORD_DEFAULT);
echo '<br/><br/> ผลลัพธ์ : <b>' . $hash .'</b>';
echo '<br/>ความยาวของตัวอักษร : <b>',strlen($hash),'</b>';
//ข้อมูลทดสอบ
$pass_in_db = '$2y$10$XO/2J2l2U70aZHRFixz32.1VU.GnfjM/Z/KifKalOdoZwctpZIYfC';
$post_data = "love999";
if (password_verify(pass_encrypt($post_data) , $pass_in_db)) {
echo '<br/><br/><span style="color:green">Password is valid!</span>';
} else {
echo '<br/><br/><span style="color:red">Invalid password.</span>';
}
?>
Function Reference
http://php.net/manual/en/function.strrev.php
http://php.net/manual/en/function.md5.php
http://php.net/manual/en/function.password-hash.php
http://php.net/manual/en/function.password-verify.php
ความคิดเห็น
แสดงความคิดเห็น