我要在 Node.js 中为 SHA 加密导入什么?
What do I import for SHA encryption in Node.js?
我正在尝试从我在网上找到的网站重新创建一些代码。该代码应该在 Node.js 中创建一个区块链。但是,当我 运行 它时,它会识别命令:
return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
它returns错误:
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?
完整代码:
class Block{
constructor(index, data, prevHash){
this.index = index;
this.timestamp = Math.floor(Date.now() / 1000);
this.data = data;
this.prevHash = prevHash;
this.hash = this.getHash();
}
getHash(){
return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
}
}
class BlockChain{
constructor(){
this.chain = [];
}
addBlock(data){
let index = this.chain.length;
let prevHash = this.chain.length !== 0 ? this.chain[this.chain.length - 1].hash : 0;
let block = new Block(index, data, prevHash);
this.chain.push(block);
}
chainIsValid(){
for(var i=0;i<this.chain.length;i++){
if(this.chain[i].hash !== this.chain[i].getHash())
return false;
if(i > 0 && this.chain[i].prevHash !== this.chain[i-1].hash)
return false;
}
return true;
}
}
const CILCoin = new BlockChain();
CILCoin.addBlock({sender: "Bruce wayne", reciver: "Tony stark", amount: 100});
CILCoin.addBlock({sender: "Harrison wells", reciver: "Han solo", amount: 50});
CILCoin.addBlock({sender: "Tony stark", reciver: "Ned stark", amount: 75});
console.log(JSON.stringify(CILCoin, null, 4));
Link 我从以下网站找到的:https://codingislove.com/simple-blockchain-javascript/
您可能需要在此处创建自己的方法,因为 sha() 不是内置于节点中的函数。您需要访问作为节点一部分的 crypto
模块。
https://nodejs.org/api/crypto.html#crypto_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
我喜欢使用 sha256 package. Never mind, this has been deprecated. I would instead opt for use of the sha.js 包。
var sha = require('sha.js');
//...
getHash() {
return sha('sha256').update(JSON.stringify(this.data) + this.preHash + this.index + this.timestamp).digest('hex');
}
我正在尝试从我在网上找到的网站重新创建一些代码。该代码应该在 Node.js 中创建一个区块链。但是,当我 运行 它时,它会识别命令:
return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
它returns错误:
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? 完整代码:
class Block{
constructor(index, data, prevHash){
this.index = index;
this.timestamp = Math.floor(Date.now() / 1000);
this.data = data;
this.prevHash = prevHash;
this.hash = this.getHash();
}
getHash(){
return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
}
}
class BlockChain{
constructor(){
this.chain = [];
}
addBlock(data){
let index = this.chain.length;
let prevHash = this.chain.length !== 0 ? this.chain[this.chain.length - 1].hash : 0;
let block = new Block(index, data, prevHash);
this.chain.push(block);
}
chainIsValid(){
for(var i=0;i<this.chain.length;i++){
if(this.chain[i].hash !== this.chain[i].getHash())
return false;
if(i > 0 && this.chain[i].prevHash !== this.chain[i-1].hash)
return false;
}
return true;
}
}
const CILCoin = new BlockChain();
CILCoin.addBlock({sender: "Bruce wayne", reciver: "Tony stark", amount: 100});
CILCoin.addBlock({sender: "Harrison wells", reciver: "Han solo", amount: 50});
CILCoin.addBlock({sender: "Tony stark", reciver: "Ned stark", amount: 75});
console.log(JSON.stringify(CILCoin, null, 4));
Link 我从以下网站找到的:https://codingislove.com/simple-blockchain-javascript/
您可能需要在此处创建自己的方法,因为 sha() 不是内置于节点中的函数。您需要访问作为节点一部分的 crypto
模块。
https://nodejs.org/api/crypto.html#crypto_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
我喜欢使用 sha256 package. Never mind, this has been deprecated. I would instead opt for use of the sha.js 包。
var sha = require('sha.js');
//...
getHash() {
return sha('sha256').update(JSON.stringify(this.data) + this.preHash + this.index + this.timestamp).digest('hex');
}