从用户电子邮件创build用于电子邮件validation的散列/标记

任何人都可以帮我解决什么用来创build一个用户input(电子邮件地址)的安全令牌(或哈希)。 我想做一个电子邮件validation系统注册。

  • 用户使用电子邮件地址和密码进行注册
  • 我想创build一个独特的URL,我将发送给用户(因此问题)
  • 我将这些(安全明智)存储在临时表中
  • 用户通过发送的URLvalidation自己

我的问题是这个url应该如何。 我想我应该通过编码电子邮件地址,将其保存为唯一的,将url保存到临时表,当用户打开链接,我会比较这两个。 如果匹配,我会将凭据移动到真正的表。

你有什么好的资源关于这个话题。 我在后端有nodejs。 谢谢

我认为你的方法也是正确的。 我在Express(REST API)中使用类似的方法完成了电子邮件validation。 我已经使用jsonwebtoken来编码email_id,user_id和expiry_date(用于检查在用户注册的电子邮件地址上发送的链接的有效性)。

然后附加这个编码的数据链接示例: http:// your_backend_server_link / verifyEmail / 1234567890afshgdf ..

  router.post('/AddUser', function(req, res, next) { var user = new User(); user.name = req.body.name; user.email = req.body.email; user.is_verified = false; user.save(function(err,user){ if(err){ console.log(err); res.json(err); } else{ console.log("User data saved"); var transport = mailer.createTransport({ service : "Gmail", auth : { user : config.central_email, pass : config.central_email_password } }); // tommorow's date var info = {}; info.user = user; info.expiry = new Date(new Date().getTime() + 24 * 60 * 60 * 1000); var token = jwt.encode(info,config.secret); console.log("http://localhost:3000/verifyEmail/" + token); var mailOptions = { from : "TEST<noreply@vysly.com>", to : user.email, subject : "Welcome to TEST", text : 'Visit this http://localhost:3000/verifyEmail/'+token, html : '<a href="http://localhost:3000/verifyEmail/'+token+'"><H2>Click on this</H2></a>' } transport.sendMail(mailOptions,function(email_err,email_data){ if(email_err){ console.log(email_err); res.json(email_err); }else{ console.log("Email is Sent"); res.json({result : 1}); } }); } }); }); 

当用户点击此链接时,从URL获取令牌并对其进行解码。 通过比较链接的有效性与服务器的当前date检查expiry_date

 router.get('/verifyEmail/:token',function(req,res){ var token = req.params.token; var data = jwt.decode(token,config.secret); console.log(new Date(data.expiry)); console.log(new Date()); if(new Date(data.expiry) > new Date()){ User.findOne({ _id : data.user._id, name : data.user.name }) .exec(function(err,user){ if(err){ console.log(err); res.json(err); }else if(!user){ console.log("User not found"); res.json({error : "User not found"}); }else{ console.log("User found"); user.is_verified = true; user.save(function(update_err,update_data){ if(update_err){ console.log(update_err); res.json(update_err); }else{ console.log("Email is verified of user "+update_data._id); res.json({result : 1}); } }); } }); }else{ console.log("Link is expired"); res.json({error : "Link is expired"}); } }); 

为什么不是SHA1哈希?

var crypto = require('crypto');

var uString = crypto.createHash('sha1')。update('mannu1200@gmail.com')。digest('hex');

将为每个唯一的电子邮件ID生成唯一的密钥。 您可以连接时间戳以进一步发送电子邮件。

编辑:

正如评论中所提到的,上面的方法是不安全的,因为它只使用公共信息来创build散列。 因此:

var uString = crypto.createHash('sha1')。update('mannu1200@gmail.com'+ mySecretKey).digest('hex');

如果您的计划不是在validation过程中重新创build散列,那么mySecretKey可以是随机string。