www.pudn.com > encryption.rar > HashOutputStream.java
/*
Christoforos Pirillos @ Villanova University - May 1999
based on code from the book "Java Network Programming" by Hughes
*/
package encryption;
import java.io.*;
/**We attach this to an OutputStream and use a hash function to provide
message integrity and authenticity*/
public class HashOutputStream extends FilterOutputStream {
protected ByteArrayOutputStream byteO;
protected DataOutputStream dataO;
protected Hash h;
/**In the constructor we create the ByteArrayOutputStream byteO that will
buffer all of the data that are written to us. We attach a
DataOutputStream dataO to the OutputStream o; we will use this to write
the message and digest*/
public HashOutputStream (OutputStream o, Hash h) {
super (new ByteArrayOutputStream () );
byteO = (ByteArrayOutputStream) out;
dataO = new DataOutputStream (o);
this.h = h;
}
/**Flush the stream*/
public void flush() throws IOException {
writeHashed ();
dataO.flush();
}
/**close the stream*/
public void close () throws IOException {
flush();
dataO.close();
}
/**Writes the message and digest to the attached stream dataO*/
protected void writeHashed () throws IOException {
if (byteO.size () > 0 ) {
byte[] b = byteO.toByteArray();
byteO.reset();
dataO.writeInt (b.length);
dataO.write(b);
byte[] hash = h.digest (b);
dataO.write(hash);
}
}
} /*end of class HashOutputStream */