com.didisoft.pgp.net
Class HKPClient

java.lang.Object
  extended by com.didisoft.pgp.net.HKPClient

public final class HKPClient
extends java.lang.Object

Retrieves keys from HTTP/S HKP key servers
Check for details: https://tools.ietf.org/html/draft-shaw-openpgp-hkp-00

Example usage:

 import java.io.ByteArrayInputStream;
 
 import com.didisoft.pgp.net.HKPClient;
 import com.didisoft.pgp.KeyStore;
 
 public class DemoHKP {
        public static void demo() throws Exception {
           // connect to HKP key server
           HKPClient hkp = new HKPClient("pgp.mit.edu");
     // search by part of the User Id
     hkp.setPartialMatchUserIds(true);
           byte[] keyBytes = hkp.getKeyByUserId("DidiSoft");
           // String keyInAscii = new String(keyBytes, "ASCII");

     if (keyBytes != null && keyBytes.length > 0) {
        KeyStore ks = new KeyStore();
        ks.importKeyRing(new ByteArrayInputStream(keyBytes));
     } else {
                System.out.println("no key found")
     }
        }
}
 


Constructor Summary
HKPClient(java.lang.String name)
          creates a new istance
HKPClient(java.lang.String name, int port)
          creates a new istance
HKPClient(java.lang.String name, int port, boolean useHttps)
           
 
Method Summary
 byte[] getKeyByKeyId(long keyId)
          Tries to retrieve a key from a HKP HTTP/S server searching by Key Id.
 byte[] getKeyByKeyIdHex(java.lang.String keyIdHex)
          Tries to retrieve a key from a HKP HTTP/S server searching by hexadecimal Key Id.
 byte[] getKeyByUserId(java.lang.String userId)
          Tries to retrieve a key from a HKP HTTP/S key server searching by User Id.
 byte[] getKeysByUserId(java.lang.String userId)
          Tries to retrieve all keys from a HKP HTTP/S key server searching by User Id.
 java.lang.String getUserAgent()
          Returns the User-Agent HTTP header that this class sends when communicates with HKP key servers
 boolean isPartialMatchUserIds()
          Returns should keys be search by part of the User ID (true) or with the whole User ID only (false)
 void setPartialMatchUserIds(boolean partialMatch)
          Sets should keys be search by part of the User ID (true) or with the whole User ID only (false)
 void setUserAgent(java.lang.String userAgent)
          Sets the User-Agent HTTP header that this class sends when communicates with HKP key servers
 boolean submitKey(byte[] keyBytes)
          Uploads an OpenPGP key bytes into a HKP HTTP key server
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HKPClient

public HKPClient(java.lang.String name)
creates a new istance

Parameters:
name - IP address or domain name of the HKP key server

HKPClient

public HKPClient(java.lang.String name,
                 int port)
creates a new istance

Parameters:
name - IP address or domain name of the HKP key server
port - port (usually 80)

HKPClient

public HKPClient(java.lang.String name,
                 int port,
                 boolean useHttps)
Method Detail

getKeyByKeyIdHex

public byte[] getKeyByKeyIdHex(java.lang.String keyIdHex)
                        throws java.io.IOException
Tries to retrieve a key from a HKP HTTP/S server searching by hexadecimal Key Id.

Parameters:
keyIdHex - key Id in hexadecimal format (like "B29931AC")
Returns:
key serialized as byte array, or empty array if there is no such key
Throws:
java.io.IOException - in case of a network connection problem Example:
        HKPClient hkp = new HKPClient("pgp.mit.edu", 80);

        byte[] keyBytes = hkp.getKeyByKeyIdHex("292AD653");
  if (keyBytes.length > 0) {
          System.out.println(new String(keyBytes, "ASCII"));
  } else {
    System.out.println("key not fond");
  }
 

getKeyByUserId

public byte[] getKeyByUserId(java.lang.String userId)
                      throws java.io.IOException
Tries to retrieve a key from a HKP HTTP/S key server searching by User Id.

Parameters:
userId - Whole or part of one of the User Id's of the key
Returns:
key serialized as byte array, or empty array if there is no such key
Throws:
java.io.IOException - in case of a network connection problem Example:
        HKPClient hkp = new HKPClient("pgp.mit.edu", 80);

        byte[] keyBytes = hkp.getKeyByUserId("DidiSoft");
  if (keyBytes.length > 0) {
          System.out.println(new String(keyBytes, "ASCII"));
  } else {
    System.out.println("key not fond");
  }
 

getKeysByUserId

public byte[] getKeysByUserId(java.lang.String userId)
                       throws java.io.IOException
Tries to retrieve all keys from a HKP HTTP/S key server searching by User Id.

Parameters:
userId - Whole or part of one of the User Id's of the key
Returns:
keys serialized as byte array, or empty array if there is no such key
Throws:
java.io.IOException - in case of a network connection problem Example:
        HKPClient hkp = new HKPClient("pgp.mit.edu", 80);

        byte[] keysBytes = hkp.getKeysByUserId("DidiSoft");
  if (keyBytes.length > 0) {
          System.out.println(new String(keyBytes, "ASCII"));
  } else {
    System.out.println("key not fond");
  }
  
  KeyStore ks = new KeyStore();
  KeyPairInformation[] keys = ks.importPublicKey(keysBytes); 
 

getKeyByKeyId

public byte[] getKeyByKeyId(long keyId)
                     throws java.io.IOException
Tries to retrieve a key from a HKP HTTP/S server searching by Key Id.

Parameters:
keyId - key Id
Returns:
key serialized as byte array, or empty array if there is no such key
Throws:
java.io.IOException - in case of a network connection problem

submitKey

public boolean submitKey(byte[] keyBytes)
                  throws java.lang.Exception
Uploads an OpenPGP key bytes into a HKP HTTP key server

Parameters:
keyBytes - OpenPGP key serialized as array of bytes
Returns:
true on success, false on error
Throws:
java.lang.Exception - in case of an error Example:
        HKPClient hkp = new HKPClient("pgp.mit.edu", 80);
                
  java.io.FileInputStream fIn = new java.io.FileInputStream("c:\\Test\\new3\\silverpub.asc");
  ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  int i = -1;
  while ((i = fIn.read()) != -1) {
       bOut.write(i);
  }             
  boolean ok = hkp.submitKey(bOut.toByteArray());
 

getUserAgent

public java.lang.String getUserAgent()
Returns the User-Agent HTTP header that this class sends when communicates with HKP key servers

Returns:
User-Agent HTTP header value

setUserAgent

public void setUserAgent(java.lang.String userAgent)
Sets the User-Agent HTTP header that this class sends when communicates with HKP key servers

Parameters:
userAgent - User-Agent HTTP header value

isPartialMatchUserIds

public boolean isPartialMatchUserIds()
Returns should keys be search by part of the User ID (true) or with the whole User ID only (false)

Returns:
should keys be search by part of the User ID (true) or with the whole User ID only (false)
See Also:
setPartialMatchUserIds(boolean)

setPartialMatchUserIds

public void setPartialMatchUserIds(boolean partialMatch)
Sets should keys be search by part of the User ID (true) or with the whole User ID only (false)

Parameters:
partialMatch - if true, User ID parameters can be specified with part of the User ID when false the whole User ID must be specified
See Also:
isPartialMatchUserIds()


Copyright © 2006-2017 DidiSoft Ltd. All Rights Reserved.