/** * Sets the data. * @param data of correct length, or null * @throws IllegalArgumentException if data is not the legal number of bytes (but null is ok) * @throws RuntimeException if data already set. */ public void setData(byte[] data) { if (_data != null) throw new RuntimeException("Data already set"); if (data != null && data.length != length()) throw new IllegalArgumentException("Bad data length: " + data.length + "; required: " + length()); _data = data; }
/** * Sets the data. * @param in the stream to read * @throws RuntimeException if data already set. */ public void readBytes(InputStream in) throws DataFormatException, IOException { if (_data != null) throw new RuntimeException("Data already set"); int length = length(); _data = new byte[length]; // Throws on incomplete read read(in, _data); }
@Override public String toString() { StringBuilder buf = new StringBuilder(64); buf.append('[').append(getClass().getSimpleName()).append(": "); int length = length(); if (_data == null) { buf.append("null"); } else if (length <= 32) { buf.append(toBase64()); } else { buf.append("size: ").append(Integer.toString(length)); } buf.append(']'); return buf.toString(); }
/** * Overridden for efficiency. * Does the same thing as setData() but null not allowed. * @param data non-null * @throws DataFormatException if null or wrong length * @throws RuntimeException if data already set. */ @Override public void fromByteArray(byte data[]) throws DataFormatException { if (data == null) throw new DataFormatException("Null data passed in"); if (data.length != length()) throw new DataFormatException("Bad data length: " + data.length + "; required: " + length()); // call setData() instead of _data = data in case overridden setData(data); }
int len = _us.length(); int dlen = data.length; if (dlen > len + 1 ||
/** * Use the supplied trim strategy. */ public KBucketSet(I2PAppContext context, T us, int max, int b, KBucketTrimmer<T> trimmer) { _us = us; _context = context; _log = context.logManager().getLog(KBucketSet.class); _trimmer = trimmer; if (max <= 4 || b <= 0 || b > 8) throw new IllegalArgumentException(); KEYSIZE_BITS = us.length() * 8; B_VALUE = b; B_FACTOR = 1 << (b - 1); NUM_BUCKETS = KEYSIZE_BITS * B_FACTOR; BUCKET_SIZE = max; _buckets = createBuckets(); _rangeCalc = new Range<T>(us, B_VALUE); // this verifies the zero-argument constructor makeKey(new byte[us.length()]); }
/** * Sets the data. * @throws DataFormatException if decoded data is not the legal number of bytes or on decoding error * @throws RuntimeException if data already set. */ @Override public void fromBase64(String data) throws DataFormatException { if (data == null) throw new DataFormatException("Null data passed in"); byte[] d = Base64.decode(data); if (d == null) throw new DataFormatException("Bad Base64 encoded data"); if (d.length != length()) throw new DataFormatException("Bad decoded data length, expected " + length() + " got " + d.length); // call setData() instead of _data = data in case overridden setData(d); }
/** * Only for SigType EdDSA_SHA512_Ed25519. * * @param key must be SigType EdDSA_SHA512_Ed25519 * @param h hash of secret data, same length as this key * @throws UnsupportedOperationException unless supported */ public static SigningPublicKey blind(SigningPublicKey key, SimpleDataStructure h) { if (key.getType() != TYPE) throw new UnsupportedOperationException(); if (h.length() != key.length()) throw new IllegalArgumentException(); try { EdDSAPublicKey jk = SigUtil.toJavaEdDSAKey(key); EdDSAPublicKey bjk = EdDSABlinding.blind(jk, h.getData()); return SigUtil.fromJavaKey(bjk, TYPE); } catch (GeneralSecurityException gse) { throw new IllegalArgumentException(gse); } }
/** * Only for SigType EdDSA_SHA512_Ed25519. * * @param key must be SigType EdDSA_SHA512_Ed25519 * @param h hash of secret data, same length as this key * @throws UnsupportedOperationException unless supported */ public static SigningPrivateKey blind(SigningPrivateKey key, SimpleDataStructure h) { if (key.getType() != TYPE) throw new UnsupportedOperationException(); if (h.length() != key.length()) throw new IllegalArgumentException(); try { EdDSAPrivateKey jk = SigUtil.toJavaEdDSAKey(key); EdDSAPrivateKey bjk = EdDSABlinding.blind(jk, h.getData()); return SigUtil.fromJavaKey(bjk, TYPE); } catch (GeneralSecurityException gse) { throw new IllegalArgumentException(gse); } }
/** * Only for SigType EdDSA_SHA512_Ed25519. * * @param key must be SigType EdDSA_SHA512_Ed25519 * @param h hash of secret data, same length as this key * @throws UnsupportedOperationException unless supported */ public static SigningPrivateKey unblind(SigningPrivateKey key, SimpleDataStructure h) { if (key.getType() != TYPE) throw new UnsupportedOperationException(); if (h.length() != key.length()) throw new IllegalArgumentException(); try { EdDSAPrivateKey bjk = SigUtil.toJavaEdDSAKey(key); EdDSAPrivateKey jk = EdDSABlinding.unblind(bjk, h.getData()); return SigUtil.fromJavaKey(jk, TYPE); } catch (GeneralSecurityException gse) { throw new IllegalArgumentException(gse); } } /******
/** * Generic raw sign any type. * * Warning, nonstandard for EdDSA, double-hashes, not recommended. * * @param hash SHA1Hash, Hash, Hash384, or Hash512 * @param type returns a Signature of this type * @throws GeneralSecurityException if algorithm unvailable or on other errors * @since 0.9.9 */ private Signature altSignRaw(String algo, SimpleDataStructure hash, PrivateKey privKey, SigType type) throws GeneralSecurityException { int hashlen = hash.length(); if (type.getHashLen() != hashlen) throw new IllegalArgumentException("type mismatch hash=" + hash.getClass() + " key=" + type); byte[] sigbytes; if (type.getBaseAlgorithm() == SigAlgo.EdDSA) { // take advantage of one-shot mode // Ignore algo, EdDSAKey includes a hash specification. EdDSAEngine jsig = new EdDSAEngine(); jsig.initSign(privKey); sigbytes = jsig.signOneShot(hash.getData()); } else { java.security.Signature jsig = java.security.Signature.getInstance(algo); jsig.initSign(privKey, _context.random()); jsig.update(hash.getData()); sigbytes = jsig.sign(); } return SigUtil.fromJavaSig(sigbytes, type); }
/** * Generic signature type. * * Warning, nonstandard for EdDSA, double-hashes, not recommended. * * @param hash SHA1Hash, Hash, Hash384, or Hash512 * @return null on error * @since 0.9.9 */ public Signature sign(SimpleDataStructure hash, SigningPrivateKey signingKey) { SigType type = signingKey.getType(); int hashlen = type.getHashLen(); if (hash.length() != hashlen) throw new IllegalArgumentException("type mismatch hash=" + hash.getClass() + " key=" + type); if (type == SigType.DSA_SHA1) return signIt(hash, signingKey); try { return altSignRaw(hash, signingKey); } catch (GeneralSecurityException gse) { if (_log.shouldLog(Log.WARN)) _log.warn(type + " Sign Fail", gse); return null; } }
throws GeneralSecurityException { SigType type = signature.getType(); int hashlen = hash.length(); if (type.getHashLen() != hashlen) throw new IllegalArgumentException("type mismatch hash=" + hash.getClass() + " key=" + type);
/** * Generic signature type. * * Warning, nonstandard for EdDSA, double-hashes, not recommended. * * @param hash SHA1Hash, Hash, Hash384, or Hash512 * @since 0.9.9 */ public boolean verifySignature(Signature signature, SimpleDataStructure hash, SigningPublicKey verifyingKey) { SigType type = signature.getType(); if (type != verifyingKey.getType()) throw new IllegalArgumentException("type mismatch sig=" + type + " key=" + verifyingKey.getType()); int hashlen = type.getHashLen(); if (hash.length() != hashlen) throw new IllegalArgumentException("type mismatch hash=" + hash.getClass() + " sig=" + type); if (type == SigType.DSA_SHA1) return verifySig(signature, hash, verifyingKey); try { return altVerifySigRaw(signature, hash, verifyingKey); } catch (GeneralSecurityException gse) { if (_log.shouldLog(Log.WARN)) _log.warn(type + " Sig Verify Fail", gse); return false; } }