0
我是Node.js的新手,我创建了一个小型加密脚本。然而,林在访问从一个函数的变量在另一个函数节点访问变量外功能
var triplesec = require('triplesec');
var data = 'secretthings'
// Encrypt Function
triplesec.encrypt({
key: new triplesec.Buffer('secretkey'),
data: new triplesec.Buffer(data),
}, function (err, buff) {
if(!err) {
global.data = buff.toString('hex')
//console.log(buff.toString('hex'))
}
});
// Decrypt Function
triplesec.decrypt({
data: new triplesec.Buffer(global.data, "hex"),
key: new triplesec.Buffer('secretkey')
}, function (err, buff) {
if(!err) {
console.log(buff.toString());
}
});
问题,当我运行上面的代码,我得到一个错误,指出:
buffer.js:67 抛出新的TypeError('必须从数字,缓冲区,数组或字符串开始');
我该如何做到这一点?
要么使一些全局变量和变异,或者明确地把它作为参数传递给函数调用。 –
将整个'triplesec.decrypt'函数调用移到定义了'ciphertext'的'if'语句的末尾,因为它只在该块中可用。 –
我使它成为一个全球性的,现在我得到一个错误,说:'buffer.js:67 扔新TypeError('必须以数字,缓冲区,数组或字符串开始');'' – bshah