简体   繁体   English

创建用于电子邮件确认的唯一ID

[英]Create a unique id for email confirmation

I need to create a unique ID for email confirmation, so when the customer click on the link it takes them to a page to enter their email, once they entered their emails, an email will be sent to them. 我需要为电子邮件确认创建一个唯一的ID,因此,当客户单击链接时,会将他们带到一个页面来输入他们的电子邮件,一旦他们输入了电子邮件,就会向他们发送一封电子邮件。 A link is included in the email and by getting into the link they can change their password. 电子邮件中包含一个链接,通过进入该链接,他们可以更改密码。 its the process , but I need you to confirm my steps, first I generate a random number and add it to db associated to user's id, if that page is opened then I allow them to change password right ? 它的过程,但是我需要您确认我的步骤,首先我生成一个随机数,并将其添加到与用户ID相关联的db中,如果打开了该页面,那么我允许他们更改密码吗?

为此使用java.util.UUID。

Your procedure is almost correct. 您的程序几乎是正确的。 You should take care of following points though: 不过,您应该注意以下几点:

1) Make the key unpredictable, so that even with knowledge of the code, nobody can create it's own valid key. 1)使密钥不可预测,因此即使知道了代码,也没有人可以创建它自己的有效密钥。 Since you are storing the key in the database anyway, you can create a "really-random" key, that's much better than using known values, like email and userid. 由于无论如何都将密钥存储在数据库中,因此您可以创建“真正随机”的密钥,这比使用已知值(例如电子邮件和用户ID)要好得多。 Password reset functions are often the weakest link in the security of a web application. 密码重置功能通常是Web应用程序安全性中最薄弱的环节。

// this (untested) code reads from the OS random source to create a random id
function createRandomKey($length)
{
  $buffer = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
  $encodedBuffer = base64_encode($buffer);
  return substr($encodedBuffer, 0, $length);
}

2) Do not store this random-key in the database directly, instead handle it like a password and store a hash from it. 2)不要将此随机密钥直接存储在数据库中,而应像处理密码一样将其存储在哈希表中。 That helps in case that an attacker can read your database (SQL-injection), even then he won't get all the keys for free. 如果攻击者可以读取您的数据库(SQL注入),那将很有帮助,即使那样,他也不会免费获得所有密钥。

3) Give each key an expiry date, so "forgotten" keys cannot be used anymore. 3)给每个密钥指定有效期限,这样就不能再使用“忘记”的密钥了。

4) A password reset key should be removed after using it, so delete it from the database after the password was successfully changed. 4)使用密码重置密钥后,应将其删除,因此,成功更改密码后,请从数据库中将其删除。

Edit: 编辑:

Sorry, i missed the Java tag in your question and my example is in PHP. 抱歉,我在您的问题中错过了Java标记,而我的示例在PHP中。 Unfortunately i cannot provide an example in Java, but the principle is the same. 不幸的是,我无法在Java中提供示例,但是原理是相同的。

I've used PHP for this. 我已经为此使用PHP。 I generally created a hash of some sort of tracking info I can verify later. 我通常创建了某种跟踪信息的哈希,稍后可以进行验证。 Like using an MD5 or SHA1 hash on the Email Address, or user ID. 就像在电子邮件地址或用户ID上使用MD5或SHA1哈希一样。 Or if you want you can just combine the two: 或者,如果您愿意,可以将两者结合起来:

Java Script: hex_md5(random_number + user_id + something_else); Java脚本: hex_md5(random_number + user_id + something_else);

PHP: md5($email.$userID.$unique_server_key); PHP: md5($email.$userID.$unique_server_key);

That should give you a unique key to track them by. 那应该给您一个唯一的密钥来跟踪它们。 Save this in your database as well as the variables used to generate it so you can track it later. 将其以及用于生成它的变量保存在数据库中,以便以后进行跟踪。 I highly suggest the PHP version as since its server-side it will help keep malicious users from knowing your token generation algorithm. 我强烈建议使用PHP版本,因为它是服务器端的版本,它将帮助防止恶意用户了解您的令牌生成算法。

JavaScript hash library: http://pajhome.org.uk/crypt/md5/ JavaScript哈希库: http : //pajhome.org.uk/crypt/md5/

EDIT: Sorry, I too assumed the wrong language. 编辑:对不起,我也假设使用了错误的语言。 For java you need to use a crypto library to generate a hash. 对于Java,您需要使用加密库来生成哈希。 Try this code (untested, I just typed it in) 尝试以下代码(未经测试,我刚刚输入了代码)

import java.security.*;
.....
string hash = unique_salt_key.concat(user_id).concat(email);
byte[] hashBytes = hash.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] emailCrypt = md.digest(hashBytes);
string emailToken = new String(emailCrypt)

I believe thats the correct use of the MD5. 我相信这是MD5的正确使用。 I just use the variables user_id and email as they are allready saved in your database. 我只使用变量user_id和email,因为它们已经全部保存在数据库中了。 And using more then one constant from the database is best. 最好使用数据库中的多个常量。 Sorry if I'm a bit rusty on my java. 抱歉,如果我对Java有点生疏。

import java.security.*;
.....
string hash = unique_salt_key.concat(user_id).concat(email);
byte[] hashBytes = hash.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] emailCrypt = md.digest(hashBytes);
string emailToken = new String(emailCrypt)



How to decrypt it again to get user_id and email?

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

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