KickJava   Java API By Example, From Geeks To Geeks.

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


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 written to 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 MD5InputStream
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 MD5OutputStream extends FilterOutputStream {
53
54     /**
55      * MD5 context
56      */

57     private MD5 md5;
58
59     /**
60      * Creates MD5OutputStream
61      * @param out The output stream
62      *
63      * @since ostermillerutils 1.00.00
64      */

65     public MD5OutputStream(OutputStream out) {
66         super(out);
67         md5 = new MD5();
68     }
69
70     /**
71      * Writes the specified byte to this output stream.
72      *
73      * @param b the byte.
74      * @throws IOException if an I/O error occurs.
75      *
76      * @since ostermillerutils 1.00.00
77      */

78     public void write(int b) throws IOException {
79         out.write(b);
80         md5.update((byte)(b & 0xff));
81     }
82
83     /**
84      * Writes len bytes from the specified byte array starting a
85      * offset off to this output stream.
86      *
87      * @param b the data.
88      * @param off the start offset in the data.
89      * @param len the number of bytes to write.
90      * @throws IOException if an I/O error occurs.
91      *
92      * @since ostermillerutils 1.00.00
93      */

94     public void write(byte b[], int off, int len) throws IOException {
95         out.write(b, off, len);
96         md5.update(b, off, len);
97     }
98
99     /**
100      * Returns array of bytes representing hash of the stream so far.
101      *
102      * @return Array of 16 bytes, the hash of all written bytes.
103      *
104      * @since ostermillerutils 1.00.00
105      */

106     public byte[] getHash(){
107         return md5.getHash();
108     }
109
110     /**
111      * Get a 32-character hex representation representing hash of the stream so far.
112      *
113      * @return A string containing the hash of all written bytes.
114      *
115      * @since ostermillerutils 1.00.00
116      */

117     public String JavaDoc getHashString(){
118         return md5.getHashString();
119     }
120 }
121
122
Popular Tags