我正在使用节点进行OpenPGP加密。 This是我的引用库。当我运行演示时,我得到了以下错误。
openpgpTest.js:32消息:等待openpgp.message.readArmored(加密),//解析装甲消息^^^^^^^
SyntaxError:位于Object.Module._extensions..js的Module._compile(module.js:599:28)处的Object.runInThisContext(vm.js:139:10)处的createScript(vm.js:80:10)处的意外标识符(module.load:646:10)在Module.load(module.js:554:32)的tryModuleLoad(module.js:497:12)上的Function.Module._load(module.js:489:3)at Function启动时的.Module.runMain(module.js:676:10)(bootstrap_node.js:187:16)at bootstrap_node.js:608:3
以下是我的代码
var openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp
openpgp.initWorker({ path:'openpgp.worker.js' })
// put keys in backtick (``) to avoid errors caused by spaces or tabs
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`
const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----` //encrypted private key
const passphrase = `yourPassphrase` //what the privKey is encrypted with
const encryptDecryptFunction = async() => {
console.log("init",openpgp)
const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0]
await privKeyObj.decrypt(passphrase)
const options = {
message: openpgp.message.fromText('Hello, World!'), // input as Message object
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for encryption
privateKeys: [privKeyObj] // for signing (optional)
}
console.log("init",options)
openpgp.encrypt(options).then(ciphertext => {
encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
return encrypted
})
.then(encrypted => {
const options = {
message: await openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
}
openpgp.decrypt(options).then(plaintext => {
console.log(plaintext.data)
return plaintext.data // 'Hello, World!'
})
})
}
encryptDecryptFunction()
有谁知道我为什么会收到这个错误?我在Windows系统上的cmd
中运行此代码。
await不能在异步函数之外使用。您可以在.then中添加async,以便在内部使用await。
.then(async(encrypted) => {
const options = {
message: await openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
}
openpgp.decrypt(options).then(plaintext => {
console.log(plaintext.data)
return plaintext.data // 'Hello, World!'
})
})
但我宁愿建议重构你的代码如下,以摆脱冗余。然后。因为您使用async / await,所以不需要它们。
const encryptDecryptFunction = async () => {
console.log("init", openpgp)
const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0]
await privKeyObj.decrypt(passphrase)
const options = {
message: openpgp.message.fromText('Hello, World!'), // input as Message object
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for encryption
privateKeys: [privKeyObj] // for signing (optional)
}
console.log("init", options)
const { data: encripted } = await openpgp.encrypt(options)
const options = {
message: await openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
}
const plaintext = await openpgp.decrypt(options);
console.log(plaintext.data)
return plaintext.data // 'Hello, World!'
}