使用openpgp库在节点上运行时出现意外标识符

时间:2018-09-20 11:25:40

标签: javascript node.js pgp

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

  

openpgpTest.js:32               消息:等待openpgp.message.readArmored(加密),//解析装甲消息                              ^^^^^^^^

     

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中运行此代码。

1 个答案:

答案 0 :(得分:1)

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