2.9 KiB
2.9 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
58a25c98f9fc0f352b528e7f | Hashing Your Passwords | 2 | 哈希密码 |
Description
将BCrypt添加为依赖项并在服务器中将其需要。您需要在两个关键区域处理散列:您在哪里处理注册/保存新帐户以及在登录时检查密码是否正确。目前在我们的注册路线上,您将用户的密码插入数据库,如下所示:
password: req.body.password
。实现保存哈希的一种简单方法是在数据库逻辑var hash = bcrypt.hashSync(req.body.password, 12);
之前添加以下内容var hash = bcrypt.hashSync(req.body.password, 12);
并使用password: hash
替换数据库保存中的req.body.password
。最后在我们的身份验证策略中,我们在完成流程之前在代码中检查以下内容: if (password !== user.password) { return done(null, false); }
。完成之前的更改后,现在user.password
是一个哈希。在更改现有代码之前,请注意语句如何检查密码是否不相等,然后返回未经过身份验证的密码。考虑到这一点,您的代码可能如下所示,以正确检查针对哈希输入的密码: if (!bcrypt.compareSync(password, user.password)) { return done(null, false); }
这是所有需要实现的最重要的安全功能之一,当你有来存储密码!当您认为自己已经做对时,请提交您的页面。 Instructions
Tests
tests:
- text: BCrypt是一种依赖
testString: ' getUserInput => $.get(getUserInput("url")+ "/_api/package.json") .then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, "bcrypt", "Your project should list "bcrypt" as a dependency"); }, xhr => { throw new Error(xhr.statusText); })'
- text: BCrypt正确需要并实施
testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /require.*("|")bcrypt("|")/gi, "You should have required bcrypt"); assert.match(data, /bcrypt.hashSync/gi, "You should use hash the password in the registration"); assert.match(data, /bcrypt.compareSync/gi, "You should compare the password to the hash in your strategy"); }, xhr => { throw new Error(xhr.statusText); })'
Challenge Seed
Solution
// solution required