2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 58a25bcff9fc0f352b528e7e
|
2021-07-15 13:04:11 +05:30
|
|
|
title: 哈希和同步比较密码
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 2
|
2020-09-17 03:53:22 -07:00
|
|
|
forumTopicId: 301579
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: hash-and-compare-passwords-synchronously
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
请注意,本项目在 [这个 Repl.it 项目](https://replit.com/github/freeCodeCamp/boilerplate-bcrypt) 的基础上进行开发。 你也可以从 [GitHub](https://github.com/freeCodeCamp/boilerplate-bcrypt/) 上克隆。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
同步执行哈希运算是非常简单的,但这会在哈希计算量大并且次数多的情况下造成延迟。 用这个方法哈希就像调用函数一样简单。
|
2020-09-17 03:53:22 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
var hash = bcrypt.hashSync(myPlaintextPassword, saltRounds);
|
|
|
|
```
|
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
把同步哈希的方法添加到你的代码,并在控制台输出。 和之前一样,我们已经为你定义好了你需要使用的变量,你不需要做任何改动。 你可能会注意到即使你使用与异步函数相同的密码进行哈希处理,控制台中的结果也不同,这是由于每次哈希值随机生成,如第三个哈希字符串中的前 22 个字符所示。 现在,为了比较一个密码输入和新的同步哈希值,你将使用 compareSync 方法。
|
2020-09-17 03:53:22 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
var result = bcrypt.compareSync(myPlaintextPassword, hash);
|
|
|
|
```
|
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
返回的结果为 true 或 false。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
请添加这个方法,并把结果输出到控制台,以此来验证同步哈希操作是否成功。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
请在完成挑战后提交你的页面。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-07-15 13:04:11 +05:30
|
|
|
应同步地进行哈希并正确地执行对比
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
(getUserInput) =>
|
|
|
|
$.get(getUserInput('url') + '/_api/server.js').then(
|
|
|
|
(data) => {
|
|
|
|
assert.match(
|
|
|
|
data,
|
|
|
|
/START_SYNC[^]*hash.*=.*bcrypt.hashSync.*myPlaintextPassword( |),( |)saltRounds[^]*END_SYNC/gi,
|
|
|
|
'You should call bcrypt.hashSync on myPlaintextPassword with saltRounds'
|
|
|
|
);
|
|
|
|
assert.match(
|
|
|
|
data,
|
|
|
|
/START_SYNC[^]*result.*=.*bcrypt.compareSync.*myPlaintextPassword( |),( |)hash[^]*END_SYNC/gi,
|
|
|
|
'You should call bcrypt.compareSync on myPlaintextPassword with the hash generated in the last line'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(xhr) => {
|
|
|
|
throw new Error(xhr.statusText);
|
|
|
|
}
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
/**
|
|
|
|
Backend challenges don't need solutions,
|
|
|
|
because they would need to be tested against a full working project.
|
|
|
|
Please check our contributing guidelines to learn more.
|
|
|
|
*/
|
|
|
|
```
|