简体   繁体   English

在php中使用django密码进行身份验证

[英]Using django passwords in php for authentication

I am trying to create a login system on the same server in php which creates registration through django. 我试图在php中的同一台服务器上创建一个登录系统,通过django创建注册。

I have no idea of how django encrypts passwords. 我不知道django如何加密密码。 The password which I can see in my database are like this: 我在数据库中可以看到的密码是这样的:

pbkdf2_sha256$10000$qlzlSSgHottd$5hV9BfLpzyAS62KZhvRyDBnagr1rYf29VbkZbfjipV4=

Now I want to create a login system in PHP which validates using the above hashed specified password . 现在我想用PHP创建一个登录系统,它使用上面的散列指定密码进行验证。 So Please help me out how to create a login system for PHP 所以请帮我解释如何为PHP创建登录系统

Note: The database is already setup and I have thousands of users who are using it I need authentication for a different system which I am building 注意:数据库已经设置好,我有成千上万的用户正在使用它我需要对我正在构建的其他系统进行身份验证

I met the same situation as you did, Prateek, and with some studies, the following codes did what I want, and it's what you want. 我遇到了和你一样的情况,Prateek和一些研究,下面的代码做了我想要的,这就是你想要的。

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

function make_password($password) {
    $algorithm = "pbkdf2_sha256";
    $iterations = 10000;

    $newSalt = mcrypt_create_iv(6, MCRYPT_DEV_URANDOM);
    $newSalt = base64_encode($newSalt);

    $hash = hash_pbkdf2("SHA256", $password, $newSalt, $iterations, 0, true);    
    $toDBStr = $algorithm ."$". $iterations ."$". $newSalt ."$". base64_encode($hash);

    // This string is to be saved into DB, just like what Django generate.
    echo $toDBStr;
}

function verify_Password($dbString, $password) {
    $pieces = explode("$", $dbString);

    $iterations = $pieces[1];
    $salt = $pieces[2];
    $old_hash = $pieces[3];

    $hash = hash_pbkdf2("SHA256", $password, $salt, $iterations, 0, true);
    $hash = base64_encode($hash);

    if ($hash == $old_hash) {
       // login ok.
       return true;
    }
    else {
       //login fail       
       return false; 
    }
}
?>

The $toDBStr generated in make_password is exactly the same as what you post in this thread, and you can save the string to any database, even keep use the DB created by Django. make_password中生成的$ toDBStr与您在此线程中发布的内容完全相同,您可以将字符串保存到任何数据库,甚至可以使用Django创建的数据库。

And you need to select the string to pass to the verify_Password function to verify if the password is the same as what user inputed. 您需要选择要传递给verify_Password函数的字符串,以验证密码是否与输入的用户相同。

The above PHP code requires PHP 5.5 to have the hash_pbkdf2 function. 上面的PHP代码要求PHP 5.5具有hash_pbkdf2函数。

Enjoy it. 好好享受。

Instead of recreating the hashing process in PHP, you might wanna let Django handle that for you. 您可能希望让Django为您处理这个问题,而不是在PHP中重新创建散列过程。

Use this script to get hashes from raw passwords: 使用此脚本从原始密码中获取哈希值:

django_password_hash.py : django_password_hash.py

import sys
from django.conf import settings
settings.configure()
from django.contrib.auth import hashers
raw_password = sys.argv[1]
try:
    salt = sys.argv[2]
except IndexError:
    salt = None
hash = hashers.make_password(raw_password, salt=salt)
sys.stdout.write("%s\n" % hash)
sys.stdout.flush()
sys.exit(0)

Then call it from PHP, something like this: 然后从PHP调用它,如下所示:

<?php
    $password_hash = shell_exec('python /path/to/django_password_hash.py ' . $raw_password . ' ' . $salt);
    // compare the value in $password_hash to database, etc...
    // account for the "\n" at the end of $password_hash

Don't forget http://www.php.net/manual/en/function.escapeshellcmd.php 不要忘记http://www.php.net/manual/en/function.escapeshellcmd.php

this function create a django hash in php 7.0+ 这个函数在php 7.0+中创建一个django哈希

function create(string $password, $iterations=36000, $algorithm='sha256', $iterations=36000) : string{
    $salt = base64_encode(openssl_random_pseudo_bytes(9));

    $hash = hash_pbkdf2($algorithm, $password, $salt, $iterations, 32, true);

    return 'pbkdf2_' . $algorithm . '$' . $iterations . '$' . $salt . '$' . base64_encode($hash);
}

The whole point of encryption (at least for passwords) is that it is, in theory, very difficult to go backwards from some encrypted data to the clear text. 加密的整个要点(至少对于密码而言)是理论上很难从一些加密数据倒退到明文。 However, it seems in this case, the data is left with the annotation of how it was encrypted. 但是,在这种情况下,数据似乎留下了加密方式的注释。 It appears to have used SHA256 with a salt of 10000. I'm not a django expert (or an encryption expert), but this seems very relevant: Django Passlib 它似乎使用了SHA256和10000的盐。我不是django专家(或加密专家),但这似乎非常相关: Django Passlib

So for whatever you're implementing, you just need to use the same encryption method. 因此,对于您正在实施的任何内容,您只需要使用相同的加密方法。

the doc should help you 文档应该帮助你

<algorithm>$<iterations>$<sal>$<hash> <算法> $ <迭代> $ <SAL> $ <散列>

so the algorithm used is: pbkdf2 sha256 所以使用的算法是:pbkdf2 sha256

By default, Django uses the PBKDF2 algorithm with a SHA256 hash 默认情况下,Django使用带有SHA256哈希的PBKDF2算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM