001: /*
002: * Implements MD5 functionality on a stream.
003: *
004: * written Santeri Paavolainen, Helsinki Finland 1996
005: * (c) Santeri Paavolainen, Helsinki Finland 1996
006: * modifications Copyright (C) 2002 Stephen Ostermiller
007: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
008: *
009: * This program is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * See COPYING.TXT for details.
020: *
021: * The original work by Santeri Paavolainen can be found a
022: * http://www.helsinki.fi/~sjpaavol/programs/md5/
023: */
024: package com.Ostermiller.util;
025:
026: import java.io.*;
027:
028: /**
029: * Implements MD5 functionality on a stream.
030: * More information about this class is available from <a target="_top" href=
031: * "http://ostermiller.org/utils/MD5.html">ostermiller.org</a>.
032: * <p>
033: * This class produces a 128-bit "fingerprint" or "message digest" for
034: * all data read from this stream.
035: * It is conjectured that it is computationally infeasible to produce
036: * two messages having the same message digest, or to produce any
037: * message having a given pre-specified target message digest. The MD5
038: * algorithm is intended for digital signature applications, where a
039: * large file must be "compressed" in a secure manner before being
040: * encrypted with a private (secret) key under a public-key cryptosystem
041: * such as RSA.
042: * <p>
043: * For more information see RFC1321.
044: *
045: * @see MD5
046: * @see MD5OutputStream
047: *
048: * @author Santeri Paavolainen http://www.helsinki.fi/~sjpaavol/programs/md5/
049: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
050: * @since ostermillerutils 1.00.00
051: */
052: public class MD5InputStream extends FilterInputStream {
053: /**
054: * MD5 context
055: */
056: private MD5 md5;
057:
058: /**
059: * Creates a MD5InputStream
060: * @param in the underlying input stream
061: */
062: public MD5InputStream(InputStream in) {
063: super (in);
064: md5 = new MD5();
065: }
066:
067: /**
068: * Reads the next byte of data from this input stream. The value byte
069: * is returned as an int in the range 0 to 255. If no byte is available
070: * because the end of the stream has been reached, the value -1 is returned.
071: * This method blocks until input data is available, the end of the stream is
072: * detected, or an exception is thrown.
073: * <p>
074: * This method simply performs in.read() and returns the result.
075: *
076: * @return the next byte of data, or -1 if the end of the stream is reached.
077: * @throws IOException if an I/O error occurs.
078: *
079: * @since ostermillerutils 1.00.00
080: */
081: @Override
082: public int read() throws IOException {
083: int c = in.read();
084: if (c == -1) {
085: return -1;
086: }
087: md5.update((byte) (c & 0xff));
088: return c;
089: }
090:
091: /**
092: * Reads up to length bytes of data from this input stream into an
093: * array of bytes. This method blocks until some input is available.
094: *
095: * @param bytes the buffer into which the data is read.
096: * @param offset the start offset of the data.
097: * @param length the maximum number of bytes read.
098: * @throws IOException if an I/O error occurs.
099: *
100: * @since ostermillerutils 1.00.00
101: */
102: @Override
103: public int read(byte[] bytes, int offset, int length)
104: throws IOException {
105: int r;
106: if ((r = in.read(bytes, offset, length)) == -1) {
107: return r;
108: }
109: md5.update(bytes, offset, r);
110: return r;
111: }
112:
113: /**
114: * Returns array of bytes representing hash of the stream so far.
115: *
116: * @return Array of 16 bytes, the hash of all read bytes.
117: *
118: * @since ostermillerutils 1.00.00
119: */
120: public byte[] getHash() {
121: return md5.getHash();
122: }
123:
124: /**
125: * Get a 32-character hex representation representing hash of the stream so far.
126: *
127: * @return A string containing the hash of all written bytes.
128: *
129: * @since ostermillerutils 1.00.00
130: */
131: public String getHashString() {
132: return md5.getHashString();
133: }
134: }
|