kbpgp.js

KeyManager

Loading from a public or private key

The following examples walk through the conversion of a PGP key string (in classic armored format) to a KeyManager.

Example 1 - a KeyManager from a public key

alice_pgp_key    = "-----BEGIN PGP PUBLIC ... etc."

kbpgp.KeyManager.import_from_armored_pgp {
  armored: alice_pgp_key
}, (err, alice) ->

  console.log "alice is loaded" unless err

Example 2 - a KeyManager from a private key

Now let's assume instead that we have alice's private key. Recall this includes her public key, so it's all we need.

alice_pgp_key    = "-----BEGIN PGP PRIVATE ... etc."
alice_passphrase = "rat:beast::np:complete"

kbpgp.KeyManager.import_from_armored_pgp {
  armored:    alice_pgp_key
}, (err, alice) ->

  unless err
    if alice.is_pgp_locked()
      alice.unlock_pgp { passphrase: alice_passphrase }, (err) ->
        console.log "Loaded private key with passphrase" unless err
    else
      console.log "Loaded private key w/o passphrase"

Example 3 - a KeyManager from a public key, then adding private key

The above example (#2) can be performed in two steps. You can create a KeyManager instance with alice's public key, and then add her private key to it afterwards. This will generate an error if her private key does not match her public key.

alice_public_key  = "-----BEGIN PGP PUBLIC ... etc."
alice_private_key = "-----BEGIN PGP PRIVATE ... etc."
alice_passphrase  = "ovarian fred savage "

kbpgp.KeyManager.import_from_armored_pgp {
  armored:    alice_public_key
}, (err, alice) ->

  unless err

    # we now have a KeyManager for alice
    # but we'd like to merge in her private key

    alice.merge_pgp_private {
      armored: alice_private_key
    }, (err) ->

      unless err
        if alice.is_pgp_locked()
          alice.unlock_pgp { passphrase: alice_passphrase }, (err) ->
            console.log "Loaded private key with passphrase" unless err
        else
          console.log "Loaded private key w/o passphrase"

We're just getting started with this tutorial and examples. Hit us up on github if anything is missing.