Search code examples
javascriptpgpopenpgp

OpenPGP.min.js doesn't generate key pair


I'm since yesterday trying to make the openpgp.js work. I tried the full version, but no success, so right now I tried the min version, and still no success. I have the following HTML code:

<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>

But in my Chrome's console, I get only:

My private key:

undefined


poc.html:17 My public key:

undefined

Why it's undefined?


Solution

  • The library uses promises. To get your generated keys you will have to use the .then() method of the returned promise:

    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);
                    });