Javascript 使用openpgp库在节点上运行时出现意外标识符
我正在使用node进行OpenPGP加密。这是我的参考图书馆。当我运行一个演示时,我得到了以下错误 openpgpTest.js:32 message:wait openpgp.message.readarmared(加密),//解析铠装消息 ^^^^^^^ SyntaxError:意外的标识符 在createScript上(vm.js:80:10) 在Object.runInThisContext(vm.js:139:10) 在模块处编译(Module.js:599:28) 在Object.Module.\u extensions..js(Module.js:646:10) 在Module.load(Module.js:554:32) 在tryModuleLoad时(module.js:497:12) 在Function.Module.\u加载(Module.js:489:3) 位于Function.Module.runMain(Module.js:676:10) 启动时(bootstrap_node.js:187:16) 在bootstrap_node.js:608:3 下面是我的代码Javascript 使用openpgp库在节点上运行时出现意外标识符,javascript,node.js,pgp,Javascript,Node.js,Pgp,我正在使用node进行OpenPGP加密。这是我的参考图书馆。当我运行一个演示时,我得到了以下错误 openpgpTest.js:32 message:wait openpgp.message.readarmared(加密),//解析铠装消息 ^^^^^^^ SyntaxError:意外的标识符 在createScript上(vm.js:80:10) 在Object.runInThisContext(vm.js:139:10) 在模块处编译(Module.js:599:28) 在Object.M
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
中运行此代码。等待不能在异步函数之外使用。您可以将async添加到。然后以便在内部使用wait
.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!'
})
})
但我建议您按照以下方式重构代码,以消除冗余的。您不需要它们,因为您使用的是异步/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!'
}
不,当我试图在浏览器中运行它时(没有node.js),我也会不断出错。来自openpgp.js/protonmail的人能提供更详细的规范吗?