📄 hashoutputstream.java
字号:
/*Christoforos Pirillos @ Villanova University - May 1999based 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 providemessage 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 willbuffer all of the data that are written to us. We attach aDataOutputStream dataO to the OutputStream o; we will use this to writethe 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -