/** * This method accepts an array of MPIs, and returns a byte array containing a reversible * serialization. * * @throws OTRException */ public static byte[] serializeMPIArray(MPI[] mpis) throws OTRException { int totalsize = 0; for (int i = 0; i < mpis.length; i++) { totalsize += 4 + mpis[i].getLength(); } totalsize += 4; byte[] buf = new byte[totalsize]; OutBuf obuf = new OutBuf(buf); obuf.writeUInt(mpis.length); for (int i = 0; i < mpis.length; i++) { mpis[i].write(obuf); } return obuf.getBytes(); }
/** Hash one or two MPIs. To hash only one MPI, b may be set to NULL. */ public static MPI hash(int version, MPI a, MPI b, Provider prov) throws OTRException { int totalsize = 1 + 4 + a.getLength(); if (b != null) { totalsize += 4 + b.getLength(); } byte[] buf = new byte[totalsize]; OutBuf obuf = new OutBuf(buf); obuf.writeByte((byte) version); obuf.writeUInt(a.getLength()); a.writeRaw(obuf); if (b != null) { obuf.writeUInt(b.getLength()); b.writeRaw(obuf); } byte[] out = obuf.getBytes(); SHA256 sha = prov.getSHA256(); byte[] digest = sha.hash(out); return new MPI(digest); }