Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于Node.js中的SHA加密,我应该导入什么?_Javascript_Node.js_Encryption_Blockchain_Sha - Fatal编程技术网

Javascript 对于Node.js中的SHA加密,我应该导入什么?

Javascript 对于Node.js中的SHA加密,我应该导入什么?,javascript,node.js,encryption,blockchain,sha,Javascript,Node.js,Encryption,Blockchain,Sha,我正在尝试从我在网上找到的网站上重新创建一些代码。代码应该在Node.js中创建区块链。但是,当我运行它时,它不会识别命令: return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp); 它返回错误: return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);

我正在尝试从我在网上找到的网站上重新创建一些代码。代码应该在Node.js中创建区块链。但是,当我运行它时,它不会识别命令:

return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
它返回错误:

return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
                ^

ReferenceError: sha is not defined
    at Block.getHash (C:\Users\winst\OneDrive\Documents\Blockchain\main.js:12:3)
    at new Block (C:\Users\winst\OneDrive\Documents\Blockchain\main.js:8:19)
    at BlockChain.addBlock (C:\Users\winst\OneDrive\Documents\Blockchain\main.js:22:14)
    at Object.<anonymous> (C:\Users\winst\OneDrive\Documents\Blockchain\main.js:42:9)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
返回sha(JSON.stringify(this.data)+this.prevHash+this.index+this.timestamp);
^
ReferenceError:未定义sha
在Block.getHash(C:\Users\winst\OneDrive\Documents\Blockchain\main.js:12:3)
在新区块(C:\Users\winst\OneDrive\Documents\Blockchain\main.js:8:19)
在BlockChain.addBlock(C:\Users\winst\OneDrive\Documents\BlockChain\main.js:22:14)

在对象处。

您可能需要在此处创建自己的方法,因为sha()不是节点中内置的函数。您需要访问作为节点一部分的
加密
模块

您可以构建一个函数

const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
                   .update('I love cupcakes')
                   .digest('hex');
console.log(hash);
// Prints:
//   c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
我喜欢用英语。没关系,这已经被弃用了。我会选择使用这个软件包


当有一个内置方法来实现完全相同的功能时,推荐外部软件包在我看来是不好的做法。OP的代码中提到的SHA不是HMAC。有一个超级酷的东西叫做Google,我曾经在
crypto
中找到它。
const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
                   .update('I love cupcakes')
                   .digest('hex');
console.log(hash);
// Prints:
//   c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
var sha = require('sha.js');
//...
getHash() {
    return sha('sha256').update(JSON.stringify(this.data) + this.preHash + this.index + this.timestamp).digest('hex');
}