Changeset 0ceb957


Ignore:
Timestamp:
Apr 17, 2016 3:55:41 PM (4 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
b69677b
Parents:
193ad43
Message:

PrivateKeyFile?: Add method to specify sig type on creation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/PrivateKeyFile.java

    r193ad43 r0ceb957  
    7575        int mode = 0;
    7676        boolean error = false;
    77         Getopt g = new Getopt("pkf", args, "t:nuxhse:");
     77        Getopt g = new Getopt("pkf", args, "t:nuxhse:c:");
    7878        int c;
    7979        while ((c = g.getopt()) != -1) {
    8080          switch (c) {
     81            case 'c':
     82                stype = g.getOptarg();
     83                break;
     84
    8185            case 't':
    8286                stype = g.getOptarg();
     
    118122        try {
    119123            File f = new File(filearg);
     124            boolean exists = f.exists();
    120125            PrivateKeyFile pkf = new PrivateKeyFile(f, client);
    121             Destination d = pkf.createIfAbsent();
    122             System.out.println("Original Destination:");
     126            Destination d;
     127            if (stype != null) {
     128                SigType type = SigType.parseSigType(stype);
     129                if (type == null)
     130                    throw new IllegalArgumentException("Signature type " + stype + " is not supported");
     131                d = pkf.createIfAbsent(type);
     132            } else {
     133                d = pkf.createIfAbsent();
     134            }
     135            if (exists)
     136                System.out.println("Original Destination:");
     137            else
     138                System.out.println("Created Destination:");
    123139            System.out.println(pkf);
    124140            verifySignature(d);
     
    186202
    187203    private static void usage() {
    188         System.err.println("Usage: PrivateKeyFile filename (generates if nonexistent, then prints)\n" +
     204        System.err.println("Usage: PrivateKeyFile [-c sigtype] filename (generates if nonexistent, then prints)\n" +
    189205                           "       PrivateKeyFile -h filename (generates if nonexistent, adds hashcash cert)\n" +
    190206                           "       PrivateKeyFile -h -e effort filename (specify HashCash effort instead of default " + HASH_EFFORT + ")\n" +
    191207                           "       PrivateKeyFile -n filename (changes to null cert)\n" +
    192208                           "       PrivateKeyFile -s filename signwithdestfile (generates if nonexistent, adds cert signed by 2nd dest)\n" +
    193                            "       PrivateKeyFile -t sigtype filename (changes to KeyCertificate of the given sig type\n" +
     209                           "       PrivateKeyFile -t sigtype filename (changes to KeyCertificate of the given sig type)\n" +
    194210                           "       PrivateKeyFile -u filename (changes to unknown cert)\n" +
    195211                           "       PrivateKeyFile -x filename (changes to hidden cert)\n");
     
    258274    }
    259275   
    260     /** Also reads in the file to get the privKey and signingPrivKey,
     276    /**
     277     *  Create with the default signature type if nonexistent.
     278     *
     279     *  Also reads in the file to get the privKey and signingPrivKey,
    261280     *  which aren't available from I2PClient.
    262281     */
    263282    public Destination createIfAbsent() throws I2PException, IOException, DataFormatException {
     283        return createIfAbsent(I2PClient.DEFAULT_SIGTYPE);
     284    }
     285   
     286    /**
     287     *  Create with the specified signature type if nonexistent.
     288     *
     289     *  Also reads in the file to get the privKey and signingPrivKey,
     290     *  which aren't available from I2PClient.
     291     *
     292     *  @since 0.9.26
     293     */
     294    public Destination createIfAbsent(SigType type) throws I2PException, IOException, DataFormatException {
    264295        if(!this.file.exists()) {
    265296            OutputStream out = null;
     
    267298                out = new SecureFileOutputStream(this.file);
    268299                if (this.client != null)
    269                     this.client.createDestination(out);
     300                    this.client.createDestination(out, type);
    270301                else
    271302                    write();
Note: See TracChangeset for help on using the changeset viewer.