net.i2p.data

## Class PrivateKeyFile

- java.lang.Object
- - net.i2p.data.PrivateKeyFile

-     public class PrivateKeyFile
      extends Object

  This helper class reads and writes files in the same "eepPriv.dat" format used by the client code. The format is:

        - Destination (387 bytes if no certificate, otherwise longer)
           - Public key (256 bytes)
           - Signing Public key (128 bytes)
           - Cert. type (1 byte)
           - Cert. length (2 bytes)
           - Certificate if length != 0
        - Private key (256 bytes)
        - Signing Private key (20 bytes)
       Total 663 bytes

  Author:
    welterde, zzz

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `PrivateKeyFile(File file)`
    `PrivateKeyFile(File file, I2PClient client)`
    `PrivateKeyFile(String file)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static boolean` | `checkSignature(Signature s, byte[] data, SigningPublicKey spk)`
    `Destination` | `createIfAbsent()` Also reads in the file to get the privKey and signingPrivKey, which aren't available from I2PClient.
    `static String` | `estimateHashCashTime(int hashEffort)`
    `Destination` | `getDestination()` Also sets the local privKey and signingPrivKey
    `PrivateKey` | `getPrivKey()`
    `SigningPrivateKey` | `getSigningPrivKey()`
    `static void` | `main(String[] args)` Create a new PrivateKeyFile, or modify an existing one, with various types of Certificates.
    `I2PSession` | `open()`
    `I2PSession` | `open(Properties opts)`
    `Certificate` | `setCertType(int t)` change cert type - caller must also call write()
    `void` | `setDestination(Destination d)`
    `Certificate` | `setHashCashCert(int effort)` change to hashcash cert - caller must also call write()
    `Certificate` | `setSignedCert(PrivateKeyFile pkf2)` sign this dest by dest found in pkf2 - caller must also call write()
    `String` | `toString()`
    `static boolean` | `verifySignature(Destination d)` Sample code to verify a 3rd party signature.
    `void` | `write()` Copied from I2PClientImpl.createDestination()

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### PrivateKeyFile

          public PrivateKeyFile(String file)

    - #### PrivateKeyFile

          public PrivateKeyFile(File file)

    - #### PrivateKeyFile

          public PrivateKeyFile(File file,
                        I2PClient client)

  - ### Method Detail

    - #### main

          public static void main(String[] args)

      Create a new PrivateKeyFile, or modify an existing one, with various types of Certificates. Changing a Certificate does not change the public or private keys. But it does change the Destination Hash, which effectively makes it a new Destination. In other words, don't change the Certificate on a Destination you've already registered in a hosts.txt key add form. Copied and expanded from that in Destination.java

    - #### createIfAbsent

          public Destination createIfAbsent()
                                     throws I2PException,
                                            IOException,
                                            DataFormatException

      Also reads in the file to get the privKey and signingPrivKey, which aren't available from I2PClient.
      Throws:
        `I2PException`
        `IOException`
        `DataFormatException`

    - #### getDestination

          public Destination getDestination()
                                     throws I2PSessionException,
                                            IOException,
                                            DataFormatException

      Also sets the local privKey and signingPrivKey
      Throws:
        `I2PSessionException`
        `IOException`
        `DataFormatException`

    - #### setDestination

          public void setDestination(Destination d)

    - #### setCertType

          public Certificate setCertType(int t)

      change cert type - caller must also call write()

    - #### setHashCashCert

          public Certificate setHashCashCert(int effort)

      change to hashcash cert - caller must also call write()

    - #### setSignedCert

          public Certificate setSignedCert(PrivateKeyFile pkf2)

      sign this dest by dest found in pkf2 - caller must also call write()

    - #### getPrivKey

          public PrivateKey getPrivKey()

    - #### getSigningPrivKey

          public SigningPrivateKey getSigningPrivKey()

    - #### open

          public I2PSession open()
                          throws I2PSessionException,
                                 IOException

      Throws:
        `I2PSessionException`
        `IOException`

    - #### open

          public I2PSession open(Properties opts)
                          throws I2PSessionException,
                                 IOException

      Throws:
        `I2PSessionException`
        `IOException`

    - #### write

          public void write()
                     throws IOException,
                            DataFormatException

      Copied from I2PClientImpl.createDestination()
      Throws:
        `IOException`
        `DataFormatException`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### estimateHashCashTime

          public static String estimateHashCashTime(int hashEffort)

    - #### verifySignature

          public static boolean verifySignature(Destination d)

      Sample code to verify a 3rd party signature. This just goes through all the hosts.txt files and tries everybody. You need to be in the $I2P directory or have a local hosts.txt for this to work. Doubt this is what you want as it is super-slow, and what good is a signing scheme where anybody is allowed to sign? In a real application you would make a list of approved signers, do a naming lookup to get their Destinations, and try those only. Or do a netDb lookup of the Hash in the Certificate, do a reverse naming lookup to see if it is allowed, then verify the Signature.

    - #### checkSignature

          public static boolean checkSignature(Signature s,
                               byte[] data,
                               SigningPublicKey spk)