📄 hashinputstream.java
字号:
/*Christoforos Pirillos @ Villanova University - May 1999based on code from the book "Java Network Programming" by Hughes*/package encryption;import java.io.*;/**This class provides the corresponding integrity-checking reading classfor a hashed stream. It attaches to an InputStream and verifies the datathat arrive on this stream, using a hash function*/public class HashInputStream extends InputStream {protected ByteArrayInputStream byteI;protected DataInputStream dataI;protected Hash h;/**The InputStream to be read. Hash to be used*/public HashInputStream (InputStream i, Hash h) { dataI = new DataInputStream (i); this.h = h; byte[] nothing = new byte[0]; byteI = new ByteArrayInputStream (nothing);}/**reads a single byte from the InputStream.*/public int read() throws IOException { if (available() ==0) readHashed(); return byteI.read();}/**reads a subarray of bytes from the InputStream*/public int read (byte[] b, int o, int l) throws IOException { if (available()==0) readHashed(); return byteI.read (b,o,l);}/**returns the number of bytes that are currently available in theInputStream buffer*/public int available () throws IOException { return byteI.available();}/**attempts to skip over the specified number of bytes*/public long skip (long n) throws IOException { if (available()==0) readHashed(); return byteI.skip(n);}public void close () throws IOException { dataI.close();}/**Reads a new message from DataInputStream and then verifies that it isnot corrupt by checking the received digest against one computed from thereceived message */protected void readHashed () throws IOException { int n; try { n=dataI.readInt(); }catch (EOFException ex) { return; } byte[] buffer = new byte[n]; dataI.readFully(buffer,0,n); byte[] digest = new byte[h.digestSize()]; byte[] newDigest = h.digest(buffer); dataI.readFully(digest); if (!Crypt.equals (digest, newDigest) ) throw new HashException (h.getClass().getName() + " digest check failed."); byteI = new ByteArrayInputStream (buffer);}} /* end of class HashInputStream */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -