## net.i2p.crypto
Class TrustedUpdate

    java.lang.Object
      net.i2p.crypto.TrustedUpdate

    public class TrustedUpdate

extends java.lang.Object

Handles DSA signing and verification of update files.

For convenience this class also makes certain operations available via the command line. These can be invoked as follows:

     java net.i2p.crypto.TrustedUpdate keygen       publicKeyFile privateKeyFile
     java net.i2p.crypto.TrustedUpdate showversion  signedFile
     java net.i2p.crypto.TrustedUpdate sign         inputFile signedFile privateKeyFile version
     java net.i2p.crypto.TrustedUpdate verifysig    signedFile
     java net.i2p.crypto.TrustedUpdate verifyupdate signedFile

Author:
  jrandom and smeghead

Constructor Summary
`TrustedUpdate()` Constructs a new `TrustedUpdate` with the default global context.
`TrustedUpdate(I2PAppContext context)` Constructs a new `TrustedUpdate` with the given `I2PAppContext`.

Method Summary
`java.util.ArrayList` `getTrustedKeys()` Fetches the trusted keys for the current instance.
`java.lang.String` `getVersionString(java.lang.String signedFile)` Reads the version string from a signed update file.
`boolean` `isUpdatedVersion(java.lang.String currentVersion, java.lang.String signedFile)` Verifies that the version of the given signed update file is newer than `currentVersion`.
`static void` `main(java.lang.String[] args)` Parses command line arguments when this class is used from the command line.
`boolean` `migrateVerified(java.lang.String currentVersion, java.lang.String signedFile, java.lang.String outputFile)` Verifies the signature of a signed update file, and if it's valid and the file's version is newer than the given current version, migrates the data out of `signedFile` and into `outputFile`.
`static boolean` `needsUpdate(java.lang.String currentVersion, java.lang.String newVersion)` Checks if the given version is newer than the given current version.
`Signature` `sign(java.lang.String inputFile, java.lang.String signedFile, SigningPrivateKey signingPrivateKey, java.lang.String version)` Uses the given `SigningPrivateKey` to sign the given input file along with its version string using DSA.
`Signature` `sign(java.lang.String inputFile, java.lang.String signedFile, java.lang.String privateKeyFile, java.lang.String version)` Uses the given private key to sign the given input file along with its version string using DSA.
`boolean` `verify(java.lang.String signedFile)` Verifies the DSA signature of a signed update file.
`boolean` `verify(java.lang.String signedFile, SigningPublicKey signingPublicKey)` Verifies the DSA signature of a signed update file.
`boolean` `verify(java.lang.String signedFile, java.lang.String publicKeyFile)` Verifies the DSA signature of a signed update file.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### TrustedUpdate

    public TrustedUpdate()

  Constructs a new `TrustedUpdate` with the default global context.

### TrustedUpdate

    public TrustedUpdate(I2PAppContext context)

  Constructs a new `TrustedUpdate` with the given `I2PAppContext`.

  Parameters:
    `context` - An instance of `I2PAppContext`.

Method Detail

### main

    public static void main(java.lang.String[] args)

  Parses command line arguments when this class is used from the command line.

  Parameters:
    `args` - Command line parameters.

### needsUpdate

    public static final boolean needsUpdate(java.lang.String currentVersion,
                                            java.lang.String newVersion)

  Checks if the given version is newer than the given current version.

  Parameters:
    `currentVersion` - The current version.
    `newVersion` - The version to test.
  Returns:
    `true` if the given version is newer than the current version, otherwise `false`.

### getTrustedKeys

    public java.util.ArrayList getTrustedKeys()

  Fetches the trusted keys for the current instance.

  Returns:
    An `ArrayList` containting the trusted keys.

### getVersionString

    public java.lang.String getVersionString(java.lang.String signedFile)

  Reads the version string from a signed update file.

  Parameters:
    `signedFile` - A signed update file.
  Returns:
    The version string read, or an empty string if no version string is present.

### isUpdatedVersion

    public boolean isUpdatedVersion(java.lang.String currentVersion,
                                    java.lang.String signedFile)

  Verifies that the version of the given signed update file is newer than `currentVersion`.

  Parameters:
    `currentVersion` - The current version to check against.
    `signedFile` - The signed update file.
  Returns:
    `true` if the signed update file's version is newer than the current version, otherwise `false`.

### migrateVerified

    public boolean migrateVerified(java.lang.String currentVersion,
                                   java.lang.String signedFile,
                                   java.lang.String outputFile)

  Verifies the signature of a signed update file, and if it's valid and the file's version is newer than the given current version, migrates the data out of `signedFile` and into `outputFile`.

  Parameters:
    `currentVersion` - The current version to check against.
    `signedFile` - A signed update file.
    `outputFile` - The file to write the verified data to.
  Returns:
    `true` if the signature and version were valid and the data was moved, `false` otherwise.

### sign

    public Signature sign(java.lang.String inputFile,
                          java.lang.String signedFile,
                          java.lang.String privateKeyFile,
                          java.lang.String version)

  Uses the given private key to sign the given input file along with its version string using DSA. The output will be a signed update file where the first 40 bytes are the resulting DSA signature, the next 16 bytes are the input file's version string encoded in UTF-8 (padded with trailing `0h` characters if necessary), and the remaining bytes are the raw bytes of the input file.

  Parameters:
    `inputFile` - The file to be signed.
    `signedFile` - The signed update file to write.
    `privateKeyFile` - The name of the file containing the private key to sign `inputFile` with.
    `version` - The version string of the input file. If this is longer than 16 characters it will be truncated.
  Returns:
    An instance of `Signature`, or `null` if there was an error.

### sign

    public Signature sign(java.lang.String inputFile,
                          java.lang.String signedFile,
                          SigningPrivateKey signingPrivateKey,
                          java.lang.String version)

  Uses the given `SigningPrivateKey` to sign the given input file along with its version string using DSA. The output will be a signed update file where the first 40 bytes are the resulting DSA signature, the next 16 bytes are the input file's version string encoded in UTF-8 (padded with trailing `0h` characters if necessary), and the remaining bytes are the raw bytes of the input file.

  Parameters:
    `inputFile` - The file to be signed.
    `signedFile` - The signed update file to write.
    `signingPrivateKey` - An instance of `SigningPrivateKey` to sign `inputFile` with.
    `version` - The version string of the input file. If this is longer than 16 characters it will be truncated.
  Returns:
    An instance of `Signature`, or `null` if there was an error.

### verify

    public boolean verify(java.lang.String signedFile)

  Verifies the DSA signature of a signed update file.

  Parameters:
    `signedFile` - The signed update file to check.
  Returns:
    `true` if the file has a valid signature, otherwise `false`.

### verify

    public boolean verify(java.lang.String signedFile,
                          java.lang.String publicKeyFile)

  Verifies the DSA signature of a signed update file.

  Parameters:
    `signedFile` - The signed update file to check.
    `publicKeyFile` - A file containing the public key to use for verification.
  Returns:
    `true` if the file has a valid signature, otherwise `false`.

### verify

    public boolean verify(java.lang.String signedFile,
                          SigningPublicKey signingPublicKey)

  Verifies the DSA signature of a signed update file.

  Parameters:
    `signedFile` - The signed update file to check.
    `signingPublicKey` - An instance of `SigningPublicKey` to use for verification.
  Returns:
    `true` if the file has a valid signature, otherwise `false`.