gpt4 book ai didi

javascript - 使用 openpgp 库在 Node 上运行时出现意外的标识符

转载 作者:行者123 更新时间:2023-12-03 01:05:28 25 4
gpt4 key购买 nike

我正在使用 Node 进行 OpenPGP 加密。 This是我的引用图书馆。当我运行演示时,出现以下错误。

openpgpTest.js:32 message: await openpgp.message.readArmored(encrypted), // parse armored message ^^^^^^^

SyntaxError: Unexpected identifier at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:599:28) at Object.Module._extensions..js (module.js:646:10) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Function.Module.runMain (module.js:676:10) at startup (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 不能在 async 函数之外使用。您可以将 async 添加到您的 .then 中,以便您可以在内部使用 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!'
})

})

但我宁愿建议按如下方式重构您的代码,以消除多余的.then。由于您使用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!'
}

关于javascript - 使用 openpgp 库在 Node 上运行时出现意外的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52424075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com