public static byte[] hashBytes( MessageDigest d, byte[] b, int offset, int length) { d.update(b, offset, length); return d.digest(); }
/** Fill byte array with random data. * randomBytes(random, buf) is same as random.nextBytes(buf) */ static public void randomBytes(Random r, byte[] buf) { randomBytes(r, buf, 0, buf.length); }
@Override public byte[] asBytes() { return Util.MPIbytes(x); }
public static void main(String[] args) throws Exception { if ((args.length == 0) || args[0].equals("write")) { writeMPI(new BigInteger("9"), System.out); writeMPI(new BigInteger("1234567890123456789"), System.out); writeMPI(new BigInteger("100200300400500600700800900"), System.out); } else if (args[0].equals("read")) { System.out.println("9"); System.out.println(readMPI(System.in)); System.out.println("1234567890123456789"); System.out.println(readMPI(System.in)); System.out.println("100200300400500600700800900"); System.out.println(readMPI(System.in)); } else if (args[0].equals("write-mpi")) { writeMPI(new BigInteger(args[1]), System.out); } else if (args[0].equals("read-mpi")) { System.err.println(readMPI(System.in)); } else if (args[0].equals("keygen")) { byte[] entropy = readMPI(System.in).toByteArray(); byte[] key = new byte[(args.length > 1 ? Integer.parseInt(args[1]) : 16)]; makeKey(entropy, key, 0, key.length); System.err.println(HexUtil.bytesToHex(key, 0, key.length)); } else if (args[0].equals("shatest")) {
public static CryptoKey read(InputStream i, DSAGroup g) throws IOException { return new DSAPrivateKey(Util.readMPI(i), g); }
/** * Reads the initialization vector from the given stream. */ public void readIV(InputStream in) throws IOException { //for (int i=0; i<feedback_register.length; i++) { // feedback_register[i]=(byte)in.read(); //} Util.readFully(in, feedback_register); }
static public void randomBytes(SecureRandom r, byte[] buf, int from, int len) { randomBytesSlowNextInt(r, buf, from, len); }
private void fast_pool_reseed() { long startTime = System.currentTimeMillis(); byte[] v0 = fast_pool.digest(); byte[] vi = v0; for(byte i = 0; i < Pt; i++) { reseed_ctx.update(vi, 0, vi.length); reseed_ctx.update(v0, 0, v0.length); reseed_ctx.update(i); vi = reseed_ctx.digest(); } // vPt=vi Util.makeKey(vi, tmp, 0, tmp.length); rekey(tmp); Arrays.fill(v0, (byte) 0); // blank out for security fast_entropy = 0; if (DEBUG) { long endTime = System.currentTimeMillis(); if(endTime - startTime > 5000) Logger.normal(this, "Fast pool reseed took " + (endTime - startTime) + "ms"); } }
private void generator_init(String cipher) { cipher_ctx = Util.getCipherByName(cipher); output_buffer = new byte[cipher_ctx.getBlockSize() / 8]; counter = new byte[cipher_ctx.getBlockSize() / 8]; allZeroString = new byte[cipher_ctx.getBlockSize() / 8]; tmp = new byte[cipher_ctx.getKeySize() / 8]; fetch_counter = output_buffer.length; }
/** * Parses a DSA Group from a string, where p, q, and g are in unsigned * hex-strings, separated by a commas */ // see readFromField() below //public static DSAGroup parse(String grp) { // StringTokenizer str=new StringTokenizer(grp, ","); // BigInteger p,q,g; // p = new BigInteger(str.nextToken(), 16); // q = new BigInteger(str.nextToken(), 16); // g = new BigInteger(str.nextToken(), 16); // return new DSAGroup(p,q,g); //} public static CryptoKey read(InputStream i) throws IOException, CryptFormatException { BigInteger p, q, g; p = Util.readMPI(i); q = Util.readMPI(i); g = Util.readMPI(i); try { DSAGroup group = new DSAGroup(p, q, g); if(group.equals(Global.DSAgroupBigA)) return Global.DSAgroupBigA; else return group; } catch (IllegalArgumentException e) { throw (CryptFormatException)new CryptFormatException("Invalid group: "+e).initCause(e); } }
public static void readFully(InputStream in, byte[] b) throws IOException { readFully(in, b, 0, b.length); }
randomBytesSlowNextInt(r, buf, from, len); return;
public void finishSentAnnouncementRequest(PeerNode peer, byte[] noderef, ByteCounter ctr, long xferUID) throws NotConnectedException { byte[] padded = new byte[paddedSize(noderef.length)]; System.arraycopy(noderef, 0, padded, 0, noderef.length); Util.randomBytes(node.fastWeakRandom, padded, noderef.length, padded.length-noderef.length); innerSendOpennetRef(xferUID, padded, peer, ctr, null); }
@Override public byte[] asBytes() { byte[] pb = Util.MPIbytes(p); byte[] qb = Util.MPIbytes(q); byte[] gb = Util.MPIbytes(g); byte[] tb = new byte[pb.length + qb.length + gb.length]; System.arraycopy(pb, 0, tb, 0, pb.length); System.arraycopy(qb, 0, tb, pb.length, qb.length); System.arraycopy(gb, 0, tb, pb.length + qb.length, gb.length); return tb; }
public DSAPublicKey(InputStream is) throws IOException, CryptFormatException { DSAGroup g = (DSAGroup) DSAGroup.read(is); if(g == Global.DSAgroupBigA) g = null; group = g; y = Util.readMPI(is); if(y.compareTo(getGroup().getP()) > 0) throw new IllegalArgumentException("y must be < p but y=" + y + " p=" + getGroup().getP()); }
public static BigInteger readMPI(InputStream in) throws IOException { int b1 = in.read(); int b2 = in.read(); if ((b1 == -1) || (b2 == -1)) throw new EOFException(); byte[] data = new byte[(((b1 << 8) + b2) + 8) >> 3]; readFully(in, data, 0, data.length); //(new DataInputStream(in)).readFully(data, 0, data.length); // REDFLAG: This can't possibly be negative, right? return new BigInteger(1, data); }
Util.randomBytes(paddingGen, buf, offset, buf.length - offset);
public static void writeMPI(BigInteger num, OutputStream out) throws IOException { out.write(MPIbytes(num)); }
byte[] b2 = new byte[len + off]; r1.nextBytes(b1); Util.randomBytes(r2, b2, off, len); assertTrue("SecureRandom offset="+off+" length="+len, Fields.byteArrayEqual(b1, b2, 0, off, len));