0

私はオンラインでphpセキュリティを学んでおり(php 5.4を使用)、学びたい/使用したい次のコードに出くわしました。次のコードは bcrypt を使用しており、bcrypt の適切な実装ですか? 問題が存在する場合は、修正またはリソースを提案してください。ありがとう。

    class PassHash {  

    // blowfish  
    private static $algo = '$2a';  

    // cost parameter  
    private static $cost = '$10';  

    // mainly for internal use  
    public static function unique_salt() {  
        return substr(sha1(mt_rand()),0,22);  
    }  

    // this will be used to generate a hash  
    public static function hash($password) {  

        return crypt($password,  
                    self::$algo .  
                    self::$cost .  
                    '$' . self::unique_salt());  

    }  

    // this will be used to compare a password against a hash  
    public static function check_password($hash, $password) {  

        $full_salt = substr($hash, 0, 29);  

        $new_hash = crypt($password, $full_salt);  

        return ($hash == $new_hash);  

    }  

}  

ユーザー登録時の使用方法は次のとおりです。

// include the class 
require ("PassHash.php");
// ...
// read all form input from $_POST
// ...
// do your regular form validation stuff
// ...
// hash the password
$pass_hash = PassHash::hash($_POST['password']);
// store all user info in the DB, excluding $_POST['password']
// store $pass_hash instead
// ...

ユーザーログインプロセス中の使用法は次のとおりです。

// include the class  
require ("PassHash.php");        
// read all form input from $_POST
// ...
// fetch the user record based on $_POST['username']  or similar  
// ...
// ... 
// check the password the user tried to login with  
if (PassHash::check_password($user['pass_hash'], $_POST['password']) {  
    // grant access  
    // ...  
} else {  
    // deny access  
    // ...  
}  
4

1 に答える 1

0

簡潔な答え :

はい、bcrypt ブローフィッシュを使用します (PHP では、ブローフィッシュは bcrypt の現在のアルゴリズムです)。

正解 :

このような信頼できる PHP 互換ライブラリを使用してみませんか?

あなたが投稿したものよりもこれを使用する利点は? :

  1. 多くの人に広く使われている (信頼され、コミュニティに受け入れられている必要があります)

  2. PHP 5.5 のネイティブ bcrypt 関数 (passwd_compat の名前) との上位互換性を考慮してください。 詳細はこちら:情報はこちら!

  3. 巧妙な再ハッシュを可能にします (アルゴリズムのコストを上げることに決めた場合は、ほとんど簡単に実行でき、コストがライブラリ ファイルのコストと一致するかどうかを確認できます。そうでない場合は、パスワードを更新するだけです)。

結論 : 自分が何をしているのかわからない場合にのみ、bcrypt で問題が発生する可能性があります。覚えておくべきことの1つは、車輪がすでにそこにある場合は、車輪を再発明しないことです.

うまくいけば、この答えがあなたを助けたり、知識を広げたりするのに役立ちます。

于 2013-04-07T00:32:40.213 に答える