2016-04-23 132 views
0

我得到「openpgp.encrypt不是一個函數」的錯誤,而試圖在openpgp.js GitHub的頁面給出的示例:https://github.com/openpgpjs/openpgpjs/blob/master/README.md#getting-started錯誤:「openpgp.encrypt不是一個函數」

繼的例子給出,並與npm install --save openpgp

安裝後我然後試圖摘錄標記爲「設置」和「加密和用密碼解密Uint8Array數據」

// Set up 

var openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp 

openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path 

openpgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard) 

// Encrypt and decrypt Uint8Array data with a password 

var options, encrypted; 

options = { 
    data: new Uint8Array([0x01, 0x01, 0x01]), // input as Uint8Array (or String) 
    passwords: ['secret stuff'],    // multiple passwords possible 
    armor: false        // don't ASCII armor (for Uint8Array output) 
}; 

openpgp.encrypt(options).then(function(ciphertext) { 
    encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array 
}); 

options = { 
    message: openpgp.message.read(encrypted), // parse encrypted bytes 
    password: 'secret stuff',     // decrypt with password 
    format: 'binary'       // output as Uint8Array 
}; 

openpgp.decrypt(options).then(function(plaintext) { 
    return plaintext.data // Uint8Array([0x01, 0x01, 0x01]) 
}); 

以下是錯誤:

TypeError: openpgp.encrypt is not a function 
    at Object.<anonymous> (/home/tgrego/1/Src/Example/Javascript/Node.js/OpenPgp/openpgpExamp.js:20:9) 
    at Module._compile (module.js:409:26) 
    at Object.Module._extensions..js (module.js:416:10) 
    at Module.load (module.js:343:32) 
    at Function.Module._load (module.js:300:12) 
    at Function.Module.runMain (module.js:441:10) 
    at startup (node.js:139:18) 
    at node.js:968:3 
+0

在調試器檢查OpenPGP的顯示.encryptMessage是OpenPGP的的方法,但不是。加密。然而,用encryptMessage代替加密來嘗試代碼仍然失敗。 – tgoneil

+0

也許這更適合作爲GitHub上的問題。在我看來,像有人忘記在代碼擺弄之後更新文檔。 –

+0

謝謝Artjom。我剛剛在GitHub存儲庫中提交了這個問題。 – tgoneil

回答

1

使用npm install --save [email protected]安裝解決了版本問題。

另外,爲了讓這個例子工作時,解密部分需要被嵌入在加密部分的回調函數,如下所示:

var openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp 

openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path 

openpgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard) 


var options, encrypted; 

options = { 
    data: new Uint8Array([0x01, 0x01, 0x01]), // input as Uint8Array (or String) 
    passwords: ['secret stuff'],    // multiple passwords possible 
    armor: false        // don't ASCII armor (for Uint8Array output) 
}; 

openpgp.encrypt(options).then(function(ciphertext) { 
    encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array 

    options = { 
     message: openpgp.message.read(encrypted), // parse encrypted bytes 
     password: 'secret stuff',     // decrypt with password 
     format: 'binary'       // output as Uint8Array 
    }; 

    openpgp.decrypt(options).then(function(plaintext) { 
     console.log (plaintext.data); 
     return plaintext.data // Uint8Array([0x01, 0x01, 0x01]) 
    }); 

}); 
相關問題