Changeset ee57bd7 for core/java


Ignore:
Timestamp:
Oct 13, 2018 10:39:20 AM (22 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f0b3815
Parents:
4c970fa
Message:

Data: Add encryption types for PublicKey?, PrivateKey?

Location:
core/java/src/net/i2p/data
Files:
2 edited

Legend:

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

    r4c970fa ree57bd7  
    1212import java.util.Arrays;
    1313
     14import net.i2p.crypto.EncType;
    1415import net.i2p.crypto.KeyGenerator;
    1516
     
    1920 * exponent, not the primes, which are constant and defined in the crypto spec.
    2021 *
    21  * Note that we use short exponents, so all but the last 28.25 bytes are zero.
    22  * See http://www.i2p2.i2p/how_cryptography for details.
     22 * As of release 0.9.38, keys of arbitrary length and type are supported.
     23 * See EncType.
    2324 *
    2425 * @author jrandom
    2526 */
    2627public class PrivateKey extends SimpleDataStructure {
    27     public final static int KEYSIZE_BYTES = 256;
     28    private static final EncType DEF_TYPE = EncType.ELGAMAL_2048;
     29    public final static int KEYSIZE_BYTES = DEF_TYPE.getPrivkeyLen();
     30
     31    private final EncType _type;
    2832
    2933    public PrivateKey() {
     34        this(DEF_TYPE);
     35    }
     36
     37    /**
     38     *  @param type non-null
     39     *  @since 0.9.38
     40     */
     41    public PrivateKey(EncType type) {
    3042        super();
     43        _type = type;
    3144    }
    3245
    3346    public PrivateKey(byte data[]) {
     47        this(DEF_TYPE, data);
     48    }
     49
     50    /**
     51     *  @param type non-null
     52     *  @param data must be non-null
     53     *  @since 0.9.38
     54     */
     55    public PrivateKey(EncType type, byte data[]) {
    3456        super(data);
     57        _type = type;
    3558    }
    3659
     
    4063     */
    4164    public PrivateKey(String base64Data) throws DataFormatException {
    42         super();
     65        this(DEF_TYPE);
    4366        fromBase64(base64Data);
    4467    }
    4568
    4669    public int length() {
    47         return KEYSIZE_BYTES;
     70        return _type.getPrivkeyLen();
     71    }
     72
     73    /**
     74     *  @return non-null
     75     *  @since 0.9.38
     76     */
     77    public EncType getType() {
     78        return _type;
    4879    }
    4980
     
    5889
    5990    /**
     91     *  @since 0.9.38
     92     */
     93    @Override
     94    public String toString() {
     95        StringBuilder buf = new StringBuilder(64);
     96        buf.append("[PrivateKey ").append(_type).append(": ");
     97        int length = length();
     98        if (_data == null) {
     99            buf.append("null");
     100        } else {
     101            buf.append("size: ").append(length);
     102        }
     103        buf.append(']');
     104        return buf.toString();
     105    }
     106
     107    /**
    60108     * We assume the data has enough randomness in it, so use the last 4 bytes for speed.
    61109     * Overridden since we use short exponents, so the first 227 bytes are all zero.
     
    65113        if (_data == null)
    66114            return 0;
     115        if (_type != DEF_TYPE)
     116            return DataHelper.hashCode(_data);
    67117        int rv = _data[KEYSIZE_BYTES - 4];
    68118        for (int i = 1; i < 4; i++)
     
    75125        if (obj == this) return true;
    76126        if ((obj == null) || !(obj instanceof PrivateKey)) return false;
    77         return Arrays.equals(_data, ((PrivateKey) obj)._data);
     127        PrivateKey p = (PrivateKey) obj;
     128        return _type == p._type && Arrays.equals(_data, p._data);
    78129    }
    79130}
  • core/java/src/net/i2p/data/PublicKey.java

    r4c970fa ree57bd7  
    1313import java.io.IOException;
    1414
     15import net.i2p.crypto.EncType;
     16
    1517/**
    1618 * Defines the PublicKey as defined by the I2P data structure spec.
     
    1820 * exponent, not the primes, which are constant and defined in the crypto spec.
    1921 *
     22 * As of release 0.9.38, keys of arbitrary length and type are supported.
     23 * See EncType.
     24 *
    2025 * @author jrandom
    2126 */
    2227public class PublicKey extends SimpleDataStructure {
    23     public final static int KEYSIZE_BYTES = 256;
     28    private static final EncType DEF_TYPE = EncType.ELGAMAL_2048;
     29    public final static int KEYSIZE_BYTES = DEF_TYPE.getPubkeyLen();
    2430    private static final int CACHE_SIZE = 1024;
    2531
    2632    private static final SDSCache<PublicKey> _cache = new SDSCache<PublicKey>(PublicKey.class, KEYSIZE_BYTES, CACHE_SIZE);
     33
     34    private final EncType _type;
    2735
    2836    /**
     
    4654
    4755    public PublicKey() {
     56        this(DEF_TYPE);
     57    }
     58
     59    /**
     60     *  @param type if null, type is unknown
     61     *  @since 0.9.38
     62     */
     63    public PublicKey(EncType type) {
    4864        super();
     65        _type = type;
    4966    }
    5067
    5168    /** @param data must be non-null */
    5269    public PublicKey(byte data[]) {
     70        this(DEF_TYPE, data);
     71    }
     72
     73    /**
     74     *  @param type if null, type is unknown
     75     *  @param data must be non-null
     76     *  @since 0.9.38
     77     */
     78    public PublicKey(EncType type, byte data[]) {
    5379        super();
     80        _type = type;
    5481        if (data == null)
    5582            throw new IllegalArgumentException("Data must be specified");
     
    6289     */
    6390    public PublicKey(String base64Data)  throws DataFormatException {
    64         super();
     91        this(DEF_TYPE);
    6592        fromBase64(base64Data);
    6693    }
    6794   
    6895    public int length() {
     96        if (_type != null)
     97            return _type.getPubkeyLen();
     98        if (_data != null)
     99            return _data.length;
    69100        return KEYSIZE_BYTES;
     101    }
     102
     103    /**
     104     *  @return null if unknown
     105     *  @since 0.9.38
     106     */
     107    public EncType getType() {
     108        return _type;
    70109    }
    71110
     
    76115        _cache.clear();
    77116    }
     117
     118    /**
     119     *  @since 0.9.38
     120     */
     121    @Override
     122    public String toString() {
     123        StringBuilder buf = new StringBuilder(64);
     124        buf.append("[PublicKey ").append((_type != null) ? _type.toString() : "unknown type").append(' ');
     125        if (_data == null) {
     126            buf.append("null");
     127        } else {
     128            buf.append("size: ").append(length());
     129        }
     130        buf.append(']');
     131        return buf.toString();
     132    }
    78133}
Note: See TracChangeset for help on using the changeset viewer.