如何通过 md5 技术使用盐

How to use salt with md5 technique

我已经编写了 md5 技术来将密码存储在哈希中,但我想用盐来实现它。

这是我的注册表单 php,带有 md5 功能,运行良好。

<?php
require("common.php");
if (! empty ( $_POST )) {
    if (empty ( $_POST ['username'] )) {
        die ( "Please enter a username." );
    }
    if (empty ( $_POST ['password'] )) {
        die ( "Please enter a password." );
    }
    $query = "SELECT 1 FROM User WHERE username = :username";
    $query_params = array (
            ':username' => $_POST ['username'] 
    );
    try {
        $stmt = $db->prepare ( $query );
        $result = $stmt->execute ( $query_params );
    } catch ( PDOException $ex ) {
        die ( "Failed to run query: " . $ex->getMessage () );
    }
    $row = $stmt->fetch ();
    if ($row) {
        die ( "This user name is already registered" );
    }
    $password = md5($_POST['password']);
    $query = "INSERT INTO User (username, password) VALUES (:username, :password)";
    $query_params = array (
            ':username' => $_POST ['username'],
            ':password' => $password 
    );
    try {
        $stmt = $db->prepare ( $query );
        $result = $stmt->execute ( $query_params );
    } catch ( PDOException $ex ) {
        die ( "Failed to run query: " . $ex->getMessage () );
    }
    header ( "Location: login.php" );
    die ( "Redirecting to login.php" );
}
?>

Md5 是存储 passwords.Please 的旧方法,请查看以下链接以使用 salt[ 散列密码=12=]

https://crackstation.net/hashing-security.htm

http://www.sitepoint.com/hashing-passwords-php-5-5-password-hashing-api/

Md5 被认为是不安全的,不再使用,您可以使用 password_hash,它默认使用 salt 来生成强密码哈希。只使用几行就更容易了,它就完成了。记住不要使用你自己的 salt,password_hash salt 选项从 PHP 7.0.0 开始被弃用。现在最好使用默认生成的盐。

您不应使用 MD5 或 SHA1 进行哈希处理(即使使用盐),因为它们 proven to be insecure

Using salted md5 for passwords is a bad idea. Not because of MD5's cryptographic weaknesses, but because it's fast. This means that an attacker can try billions of candidate passwords per second on a single GPU.

PHP 现在提供了一种通过 password_hash 函数使用更安全的 bcrypt 散列的简单方法,它不仅会生成强散列,还会生成随机盐

$password = password_hash($_POST['password'], PASSWORD_DEFAULT);