/** * Put all the bytes to the buffer. Block until the operation is completed. * * @param buffer */ public void put(byte[] buffer) { for (byte b : buffer) put(b); }
@Override public void write(int b) throws IOException { buffer.put(b); } };
/** * Put all the bytes to the buffer. Blocks until the operation is completed. * * @param collection */ public <T extends Number> void put(Iterable<T> collection) { for (T x : collection) { put(x.intValue()); } }
/** * Put string in UTF-8 encoding into the buffer blocking until there is enough space. * * @param s string to put */ public void put(String s) { put(s.getBytes()); }
@Override public int read() throws IOException { int nextByte = inputStream.read(); if (nextByte < 0) { readingFinished = true; end(); return -1; } else { ring.put(nextByte); try { int encrypted = ring.get(); hmac.update(encrypted); return transformer.transformByte(encrypted); } catch (EncryptionError encryptionError) { throw new IOException("failed to decrypt", encryptionError); } } }
EtaDecryptingStream(InputStream inputStream) throws IOException, EncryptionError { this.inputStream = inputStream; byte[] IV = new byte[getCipher().getBlockSize()]; inputStream.read(IV); transformer = new CTRTransformer(getCipher(), IV); hmac = new HMAC(key); // We should have always block bytes in the buffer to finish: ring = new ByteRingBuffer(hmac.getLength() + 8); for (int i = 0; i < hmac.getLength(); i++) ring.put(inputStream.read()); }