我从昨天起就试图让openpgp.js工作。我尝试了完整版,但没有成功,所以现在我尝试了min版本,但仍然没有成功。我有以下HTML代码:
<html>
<head>
<meta charset="utf-8">
<title>OpenPGPJS Unit Tests</title>
<script src="openpgp.min.js"></script>
</head>
<body>
<script>
my_user_id = "John Test <john_test@someserver.com>";
my_passphrase = "123qwe";
my_key = openpgp.generateKeyPair({numBits: 1024, userId: my_user_id, passphrase:
my_passphrase});
// My Private Key String
console.log("My private key:\n\n" + my_key.privateKeyArmored + "\n\n");
// My Public Key String
console.log("My public key:\n\n" + my_key.publicKeyArmored + "\n\n");
</script>
</body>
</html>
但是在我的Chrome控制台中,我只得到:
My private key:
undefined
poc.html:17 My public key:
undefined
为什么它未定义?
答案 0 :(得分:2)
图书馆使用promises。
要获取生成的密钥,您必须使用返回的承诺的.then()
方法:
var my_user_id = "John Test <john_test@someserver.com>";
var my_passphrase = "123qwe";
var my_key = openpgp
.generateKeyPair({numBits: 1024, userId: my_user_id, passphrase: my_passphrase})
.then(function(keyPair) {
console.log("privateKeyArmored: " + keyPair.privateKeyArmored);
});