我需要重复这个哈希函数 x 次,但我现在有点迷路了
I need to repeat this hash function x amount of times but I'm a bit lost now
我正在尝试制作一个以秘密开头的脚本,然后对其进行哈希处理,然后对其进行哈希处理,用 Javascript w/Node 编写。我需要重复这个过程 500 万次。代码的基础很简单,但执行却让我慢了下来。这是我目前所拥有的。
const secret = "Secret"; // Make the secret a const so it won't change.
var sha256 = require('sha256'); // Set-up sha256
var prehash = sha256(secret); // Set variable 'prehash' to the hash of 'secret'
console.log(prehash); // Display the hash
currentHash = sha256(prehash); //Hash the previous hash
console.log(currentHash); //display the new hash
我需要它来做几件事:
- 散列一个秘密。 (完成)
- 散列散列。 (完成)
- 重复 500 万次。
这就是问题所在。
尝试的解决方案#1
如果我开始重新分配变量,我会忘记正在对哪个散列进行散列,因为我必须将每个迭代设置为一个新的 var。这很耗时,我无法检查 1 个拼写错误的变量的数百万行代码,却发现它没有被正确调用。
尝试的解决方案 #2
在秘密之后,我可以尝试将新哈希分别分配给 preHash 和 currentHash 但我认为它将值设置为变量的字符串表示形式而不是值本身。这就是我的意思。
var preHash = sha256(secret);
currentHash = sha256(preHash);
preHash = currentHash;
currentHash = sha256(preHash);
如果他们按这个顺序发生,我可以期待
- preHash to = 'secret'
的散列
- currentHash = preHash
的哈希值
- preHash 设置为 = currentHash,这在当时意味着 sha256(preHash)
- currentHash 设置为 = 新 preHash 的 sha256
这是否意味着@第3步,preHash = currentHash or
preHash = sha256(preHash)?
我也一直在查看 node 和 js 的文档,看看我是否可以;
- 将
console.log
的每个输出写入文件或
- 导出为 CSV 文件。
但是我没能找到任何有意义的东西。
这是一个迭代解决方案,它将继续将哈希结果反馈回 sha256 500 万次。
const sha256 = require('sha256');
const fs = require('fs');
const secret = "Secret";
const max = 5e6; // shorthand for 5 million
let output = secret;
let hash = secret;
let i = 0;
while(i++ < max){
hash = sha256(hash);
output += `\n${hash}`;
}
console.log('done', hash);
// save results
const fileName = "path/to/file/output.csv"; // overwrite for desired name
fs.writeFile(fileName, output, err => console.log(err));
我怀疑如果您想跟踪每个散列的结果然后将其写入文件,这会非常 慢。您应该首先尝试 运行 此块以获得较小的 max
值,然后从那里开始。对此有信心后,删除构建输出字符串和打印结果的步骤。
警告:这不会很快,只是一种非常天真的方法
使用 https://www.freecodecamp.org/news/pipe-and-compose-in-javascript-5b04004ac937/ 中的 pipe
函数,我们可以将 f(g(x))
定义为 pipe(f,g)(x)
。
因此我们可以使用 Array(5,000,000).fill(f)
为我们提供 500 万个函数引用 f
。
然后我们可以将其传递给 pipe
的参数,这将 return 传递给我们一个函数,该函数将 运行 函数 f
传递 500 万次return 从一个到下一个的输入。
const pipe = (fns) => (x) => fns.reduce((v, f) => f(v), x);
const addOne = (x) => x+1;
const arrayOfFuncs = Array(5_000_000).fill(addOne);
const addOneChungus = pipe(arrayOfFuncs);
console.log(
addOneChungus(0)
);
我正在尝试制作一个以秘密开头的脚本,然后对其进行哈希处理,然后对其进行哈希处理,用 Javascript w/Node 编写。我需要重复这个过程 500 万次。代码的基础很简单,但执行却让我慢了下来。这是我目前所拥有的。
const secret = "Secret"; // Make the secret a const so it won't change.
var sha256 = require('sha256'); // Set-up sha256
var prehash = sha256(secret); // Set variable 'prehash' to the hash of 'secret'
console.log(prehash); // Display the hash
currentHash = sha256(prehash); //Hash the previous hash
console.log(currentHash); //display the new hash
我需要它来做几件事:
- 散列一个秘密。 (完成)
- 散列散列。 (完成)
- 重复 500 万次。
这就是问题所在。
尝试的解决方案#1 如果我开始重新分配变量,我会忘记正在对哪个散列进行散列,因为我必须将每个迭代设置为一个新的 var。这很耗时,我无法检查 1 个拼写错误的变量的数百万行代码,却发现它没有被正确调用。
尝试的解决方案 #2 在秘密之后,我可以尝试将新哈希分别分配给 preHash 和 currentHash 但我认为它将值设置为变量的字符串表示形式而不是值本身。这就是我的意思。
var preHash = sha256(secret);
currentHash = sha256(preHash);
preHash = currentHash;
currentHash = sha256(preHash);
如果他们按这个顺序发生,我可以期待
- preHash to = 'secret' 的散列
- currentHash = preHash 的哈希值
- preHash 设置为 = currentHash,这在当时意味着 sha256(preHash)
- currentHash 设置为 = 新 preHash 的 sha256
这是否意味着@第3步,preHash = currentHash or
preHash = sha256(preHash)?
我也一直在查看 node 和 js 的文档,看看我是否可以;
- 将
console.log
的每个输出写入文件或 - 导出为 CSV 文件。
但是我没能找到任何有意义的东西。
这是一个迭代解决方案,它将继续将哈希结果反馈回 sha256 500 万次。
const sha256 = require('sha256');
const fs = require('fs');
const secret = "Secret";
const max = 5e6; // shorthand for 5 million
let output = secret;
let hash = secret;
let i = 0;
while(i++ < max){
hash = sha256(hash);
output += `\n${hash}`;
}
console.log('done', hash);
// save results
const fileName = "path/to/file/output.csv"; // overwrite for desired name
fs.writeFile(fileName, output, err => console.log(err));
我怀疑如果您想跟踪每个散列的结果然后将其写入文件,这会非常 慢。您应该首先尝试 运行 此块以获得较小的 max
值,然后从那里开始。对此有信心后,删除构建输出字符串和打印结果的步骤。
警告:这不会很快,只是一种非常天真的方法
使用 https://www.freecodecamp.org/news/pipe-and-compose-in-javascript-5b04004ac937/ 中的 pipe
函数,我们可以将 f(g(x))
定义为 pipe(f,g)(x)
。
因此我们可以使用 Array(5,000,000).fill(f)
为我们提供 500 万个函数引用 f
。
然后我们可以将其传递给 pipe
的参数,这将 return 传递给我们一个函数,该函数将 运行 函数 f
传递 500 万次return 从一个到下一个的输入。
const pipe = (fns) => (x) => fns.reduce((v, f) => f(v), x);
const addOne = (x) => x+1;
const arrayOfFuncs = Array(5_000_000).fill(addOne);
const addOneChungus = pipe(arrayOfFuncs);
console.log(
addOneChungus(0)
);