KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > MD5InputStream


1 /*
2  * Implements MD5 functionality on a stream.
3  *
4  * written Santeri Paavolainen, Helsinki Finland 1996
5  * (c) Santeri Paavolainen, Helsinki Finland 1996
6  * modifications Copyright (C) 2002 Stephen Ostermiller
7  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * See COPYING.TXT for details.
20  *
21  * The original work by Santeri Paavolainen can be found a
22  * http://www.helsinki.fi/~sjpaavol/programs/md5/
23  */

24 package com.Ostermiller.util;
25
26 import java.io.*;
27
28 /**
29  * Implements MD5 functionality on a stream.
30  * More information about this class is available from <a target="_top" HREF=
31  * "http://ostermiller.org/utils/MD5.html">ostermiller.org</a>.
32  * <p>
33  * This class produces a 128-bit "fingerprint" or "message digest" for
34  * all data read from this stream.
35  * It is conjectured that it is computationally infeasible to produce
36  * two messages having the same message digest, or to produce any
37  * message having a given pre-specified target message digest. The MD5
38  * algorithm is intended for digital signature applications, where a
39  * large file must be "compressed" in a secure manner before being
40  * encrypted with a private (secret) key under a public-key cryptosystem
41  * such as RSA.
42  * <p>
43  * For more information see RFC1321.
44  *
45  * @see MD5
46  * @see MD5OutputStream
47  *
48  * @author Santeri Paavolainen http://www.helsinki.fi/~sjpaavol/programs/md5/
49  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
50  * @since ostermillerutils 1.00.00
51  */

52 public class MD5InputStream extends FilterInputStream {
53     /**
54      * MD5 context
55      */

56     private MD5 md5;
57
58     /**
59      * Creates a MD5InputStream
60      * @param in the underlying input stream
61      */

62     public MD5InputStream (InputStream in) {
63         super(in);
64         md5 = new MD5();
65     }
66
67     /**
68      * Reads the next byte of data from this input stream. The value byte
69      * is returned as an int in the range 0 to 255. If no byte is available
70      * because the end of the stream has been reached, the value -1 is returned.
71      * This method blocks until input data is available, the end of the stream is
72      * detected, or an exception is thrown.
73      * <p>
74      * This method simply performs in.read() and returns the result.
75      *
76      * @return the next byte of data, or -1 if the end of the stream is reached.
77      * @throws IOException if an I/O error occurs.
78      *
79      * @since ostermillerutils 1.00.00
80      */

81     public int read() throws IOException {
82         int c = in.read();
83         if (c == -1) {
84             return -1;
85         } else {
86             md5.update((byte)(c & 0xff));
87             return c;
88         }
89     }
90
91     /**
92      * Reads up to len bytes of data from this input stream into an
93      * array of bytes. This method blocks until some input is available.
94      *
95      * @param bytes the buffer into which the data is read.
96      * @param offset the start offset of the data.
97      * @param length the maximum number of bytes read.
98      * @throws IOException if an I/O error occurs.
99      *
100      * @since ostermillerutils 1.00.00
101      */

102     public int read(byte[] bytes, int offset, int length) throws IOException {
103         int r;
104         if ((r = in.read(bytes, offset, length)) == -1) {
105             return r;
106         } else {
107             md5.update(bytes, offset, r);
108             return r;
109         }
110     }
111
112     /**
113      * Returns array of bytes representing hash of the stream so far.
114      *
115      * @return Array of 16 bytes, the hash of all read bytes.
116      *
117      * @since ostermillerutils 1.00.00
118      */

119     public byte[] getHash(){
120         return md5.getHash();
121     }
122
123     /**
124      * Get a 32-character hex representation representing hash of the stream so far.
125      *
126      * @return A string containing the hash of all written bytes.
127      *
128      * @since ostermillerutils 1.00.00
129      */

130     public String JavaDoc getHashString(){
131         return md5.getHashString();
132     }
133 }
134
Popular Tags