TypeError: CryptoJS.SHA256 is not a function
TypeError: CryptoJS.SHA256 is not a function
import * as CryptoJS from 'crypto-js';
import * as ecdsa from 'elliptic';
import * as _ from 'lodash'
class Transaction {
id = null;
constructor(id, txIns, txOuts) {
this.id = id;
this.txIns = txIns;
this.txOuts = txOuts;
}
}
function getTransactionId (Transaction)
{
const txInContent = Transaction.txIns.txOutId + Transaction.txIns.txOutIndex;
// const txInContent = Transaction.TxIN.map((TxIN) => TxIN.txOutId + TxIN.txOutIndex).reduce((a, b) => a + b, '');
const txOutContent = Transaction.txOuts.amount + Transaction.txOuts.address;
// const txOutContent = Transaction.TxOut.map((TxOUT) => txOut.amount + txOut.address).reduce((a, b) => a + b, '');
return (txInContent + txOutContent).toString();
}
class TxIN {
txOutId = null;
txOutIndex = null;
signature = null;
constructor(txOutId, txOutIndex, signature) {
this.txOutId = txOutId; // Previous transaction ID
this.txOutIndex = txOutIndex;
this.signature = signature; // Sender's signature
}
}
class TxOUT {
amount = null;
address = null;
constructor(amount, address) {
this.amount = amount; // Amount value of output
this.address = address; // Receiver's address
}
}
let testOut = new TxOUT(10, "tb1****************************ntxt");
let testIn = new TxIN("tb******************************tntxt", 0, "gg")
let testTrans = new Transaction("1329***********************1e99ba8", testIn, testOut);
var test = 'GG';
console.log(test);
var t = CryptoJS.SHA256(test);
我在我的 JavaScript 项目中使用 CryptoJS.SHA256()。但是显示“TypeError:CryptoJS.SHA256 不是函数”。我已经导入了 'crypto-js'。当我尝试记录加密时。我找到了“SHA256:[函数(匿名)]”。但是无法解决TypeError。
我有什么地方做错了吗?
试试这些:
npm install @types/node --save-dev
安装 NodeJS 定义
npm install --save @types/crypto-js
在 tsconfig.ts 文件中添加以下内容:
"files": [
"./node_modules/@types/node/index.d.ts"
]
使用 import 导入要使用的库
import * as sha256 from 'crypto-js/sha256';
或
使用特殊的导入语法
import sha256 = require("crypto-js/sha256");
尝试这样获取:
var SHA256 = require("crypto-js/sha256");
console.log(SHA256('my message'));
这里是 运行 演示:
https://replit.com/@slash851/crypto-js-test#index.js
import * as CryptoJS from 'crypto-js';
import * as ecdsa from 'elliptic';
import * as _ from 'lodash'
class Transaction {
id = null;
constructor(id, txIns, txOuts) {
this.id = id;
this.txIns = txIns;
this.txOuts = txOuts;
}
}
function getTransactionId (Transaction)
{
const txInContent = Transaction.txIns.txOutId + Transaction.txIns.txOutIndex;
// const txInContent = Transaction.TxIN.map((TxIN) => TxIN.txOutId + TxIN.txOutIndex).reduce((a, b) => a + b, '');
const txOutContent = Transaction.txOuts.amount + Transaction.txOuts.address;
// const txOutContent = Transaction.TxOut.map((TxOUT) => txOut.amount + txOut.address).reduce((a, b) => a + b, '');
return (txInContent + txOutContent).toString();
}
class TxIN {
txOutId = null;
txOutIndex = null;
signature = null;
constructor(txOutId, txOutIndex, signature) {
this.txOutId = txOutId; // Previous transaction ID
this.txOutIndex = txOutIndex;
this.signature = signature; // Sender's signature
}
}
class TxOUT {
amount = null;
address = null;
constructor(amount, address) {
this.amount = amount; // Amount value of output
this.address = address; // Receiver's address
}
}
let testOut = new TxOUT(10, "tb1****************************ntxt");
let testIn = new TxIN("tb******************************tntxt", 0, "gg")
let testTrans = new Transaction("1329***********************1e99ba8", testIn, testOut);
var test = 'GG';
console.log(test);
var t = CryptoJS.SHA256(test);
我在我的 JavaScript 项目中使用 CryptoJS.SHA256()。但是显示“TypeError:CryptoJS.SHA256 不是函数”。我已经导入了 'crypto-js'。当我尝试记录加密时。我找到了“SHA256:[函数(匿名)]”。但是无法解决TypeError。 我有什么地方做错了吗?
试试这些:
npm install @types/node --save-dev
安装 NodeJS 定义
npm install --save @types/crypto-js
在 tsconfig.ts 文件中添加以下内容:
"files": [
"./node_modules/@types/node/index.d.ts"
]
使用 import 导入要使用的库
import * as sha256 from 'crypto-js/sha256';
或 使用特殊的导入语法
import sha256 = require("crypto-js/sha256");
尝试这样获取:
var SHA256 = require("crypto-js/sha256");
console.log(SHA256('my message'));
这里是 运行 演示: https://replit.com/@slash851/crypto-js-test#index.js