gpt4 book ai didi

javascript - 字符串比较真的不安全吗?

转载 作者:行者123 更新时间:2023-11-29 21:00:40 26 4
gpt4 key购买 nike

有人建议使用简单的字符串比较来匹配密码是不安全的,因为 timing attacks .例如参见 this question .好吧,我尝试测量 node.js 中两次密码猜测之间的时间差。这是代码:

const pass   = '................................................................';
const guess1 = '..X.............................................................';
const guess2 = '.............................................................X..';

function ns(hrtime) {
return hrtime[0] * 1e9 + hrtime[1];
}

test(guess1);
test(guess2);
test(guess1);
test(guess2);
test(guess1);
test(guess2);
test(guess1);
test(guess2);
test(guess1);
test(guess2);
test(guess1);
test(guess2);

function test(guess) {
const start = process.hrtime();
for (let i = 0; i < 1e5; ++i) {
if (guess === pass) throw new Error('HIT');
}
const time = ns(process.hrtime(start));
console.log('%d ns %s', time, guess);
}

这是在我的机器上执行一次的结果:

2073045 ns ..X.............................................................
58420 ns .............................................................X..
57778 ns ..X.............................................................
57468 ns .............................................................X..
57554 ns ..X.............................................................
57436 ns .............................................................X..
57589 ns ..X.............................................................
57798 ns .............................................................X..
57798 ns ..X.............................................................
57506 ns .............................................................X..
57969 ns ..X.............................................................
57974 ns .............................................................X..

时间和不同的密码猜测之间似乎没有相关性。是我做错了什么还是真的没有可测量的时差?

最佳答案

1) 我希望您安全地散列密码。这意味着可以计算哈希值彼此之间的接近程度。只要散列函数是安全的,那根本就没有用。

2) 直接在机器上测量和真实世界的攻击是有区别的。当攻击者计时一个请求时,该请求将包括网络延迟以及 node.js 延迟。如果我们谈论纳秒,没有人会注意到任何差异。

关于javascript - 字符串比较真的不安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46636279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com