【问题标题】:Unexpected identifier when running on node using openpgp library使用 openpgp 库在节点上运行时出现意外标识符
【发布时间】:2019-02-24 16:53:10
【问题描述】:

我正在使用 node 进行 OpenPGP 加密。 This 是我的参考库。当我运行演示时,出现以下错误。

openpgpTest.js:32 message: await openpgp.message.readArmored(encrypted), // 解析装甲消息 ^^^^^^^

SyntaxError:意外的标识符 在 createScript (vm.js:80:10) 在 Object.runInThisContext (vm.js:139:10) 在 Module._compile (module.js:599:28) 在 Object.Module._extensions..js (module.js:646:10) 在 Module.load (module.js:554:32) 在 tryModuleLoad (module.js:497:12) 在 Function.Module._load (module.js:489:3) 在 Function.Module.runMain (module.js:676:10) 启动时(bootstrap_node.js:187:16) 在 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 中运行此代码。

【问题讨论】:

  • 不,当我尝试在浏览器中运行它时(没有 node.js),我也不断收到错误消息。来自 openpgp.js/protonmail 的人能否提供更详细的规范?

标签: javascript node.js pgp


【解决方案1】:

await 不能在 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!'
}

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 2020-08-04
    • 2018-03-01
    相关资源
    最近更新 更多