简体   繁体   English

基于PHP令牌的系统

[英]PHP token-based system

I'm building a login system and I want it to be secure so that I can re-use it on several projects. 我正在构建一个登录系统,我希望它是安全的,以便可以在多个项目中重复使用它。 I'm using the function below to encrypt the given password with my secret key (32 chars). 我正在使用下面的功能通过我的秘密密钥(32个字符)对给定的密码进行加密。

function Encrypt($key, $payload) 
{
    $iv = mcrypt_create_iv(IV_SIZE, MCRYPT_DEV_URANDOM);
    $crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $payload, MCRYPT_MODE_CBC, $iv);
    $combo = $iv . $crypt;
    $garble = base64_encode($iv . $crypt);
    return $garble;
}

My IV_SIZE is the following: 我的IV_SIZE如下:

mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC))

Before you ask I don't allow users to enter a password. 在您问我之前,我不允许用户输入密码。 I generate a very complicated and long password for them with everything inside (uppercase, lowercase, digits and symbols). 我为它们生成了一个非常复杂且很长的密码,里面包含了所有内容(大写,小写,数字和符号)。 I've also implemented a ban system that allows you to insert a wrong password for a verly limited amount of times and, as if it wasn't enough, I also lock the entire account so that you can't try again even when the ban has expired. 我还实施了一个禁令系统,允许您在一定有限的时间内插入错误的密码,而且似乎还不够,我还锁定了整个帐户,即使您遇到了这种情况,也无法再次尝试禁令已过期。

This is the decrypt function still pretty standard: 这是解密功能还很标准的:

function Decrypt($key, $garble) 
{
    $combo = base64_decode($garble);
    $iv = substr($combo, 0, IV_SIZE);
    $crypt = substr($combo, IV_SIZE, strlen($combo));
    $payload = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypt, MCRYPT_MODE_CBC, $iv);
    return $payload;
}

Said that it's 2 hours that I'm trying to figure out what's the best approach to issue a login session/cookie. 说我想找出发出登录会话/ cookie的最佳方法是2个小时。 I want to adopt a token-based approach like follows. 我想采用基于令牌的方法,如下所示。

As soon as login is successful, I want to store in a cookie the first part of an hash. 登录成功后,我想将散列的第一部分存储在cookie中。 The entire hash is stored in my DB and it is unique. 整个哈希存储在我的数据库中,并且是唯一的。 The second part of it would be something like a salt + sha/md5/base64 (...) of userid, email, timestamp etc. Is it a good approach? 它的第二部分是用户名,电子邮件,时间戳等的salt + sha / md5 / base64(...)。这是一个好方法吗?

No. 没有。

Unless you need to have access to the plain-text password (eg if it's a password for a configured SMTP server that you'd use), you MUST NOT encrypt passwords, but hash them. 除非您需要访问纯文本密码(例如,如果该密码是您要使用的已配置SMTP服务器的密码),否则您不得加密密码,而应对其进行哈希处理
And use password_hash() (bcrypt) to hash them; 并使用password_hash() (bcrypt)对其进行哈希处理; it is specifically designed for this purpose. 它是专门为此目的而设计的。

Also, it is not made clear in your question what these hashes are for 另外,在您的问题中也不清楚这些哈希的用途是什么 , but it sounds like a flawed choice at best - MD5 and SHA1 shouldn't be used for any kind of security mechanisms today ,但听起来充其量似乎是一个有缺陷的选择-MD5和SHA1不应用于任何类型的安全机制 , and if you want to verify the authenticity of something, you should be using a keyed hash function like hash_hmac() instead anyway. ,并且如果您想验证某物的真实性,则无论如何都应该使用诸如hash_hmac()类的键控哈希函数。

Use a generic PHP session to maintain the login state. 使用通用的PHP会话来维护登录状态。

It seems to me that you're over-thinking the problem at hand and in turn over-engineering the solution. 在我看来,您过度考虑了眼前的问题,反过来又过度设计了解决方案。 Keep it simple. 把事情简单化。

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

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