KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > digests > Hash


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.digests;
21
22 import com.maverick.util.ByteArrayWriter;
23
24 import java.math.BigInteger JavaDoc;
25
26
27 /**
28  * <p>Useful utility class that provides a wrapper around a digest with
29  * methods to update the digest with many of the common parameter types
30  * used throughout the API.</p>
31  * @author Lee David Painter
32  */

33 public class Hash {
34   private Digest digest;
35
36   /**
37    * Create a hash with the digest provided.
38    * @param digest
39    */

40   public Hash(String JavaDoc type) {
41     this.digest = DigestFactory.createDigest(type);
42   }
43
44   public Hash(Digest digest) {
45       this.digest = digest;
46   }
47
48   /* (non-Javadoc)
49  * @see com.maverick.crypto.digests.Tmp#putBigInteger(java.math.BigInteger)
50  */

51   public void putBigInteger(BigInteger JavaDoc bi) {
52     byte[] data = bi.toByteArray();
53
54     putInt(data.length);
55     putBytes(data);
56   }
57
58   /* (non-Javadoc)
59  * @see com.maverick.crypto.digests.Tmp#putByte(byte)
60  */

61   public void putByte(byte b) {
62     digest.update(b);
63   }
64
65   /* (non-Javadoc)
66  * @see com.maverick.crypto.digests.Tmp#putBytes(byte[])
67  */

68   public void putBytes(byte[] data) {
69     digest.update(data, 0, data.length);
70   }
71
72   /* (non-Javadoc)
73    * @see com.maverick.crypto.digests.Tmp#putBytes(byte[], int, int)
74    */

75   public void putBytes(byte[] data, int offset, int len) {
76     digest.update(data, offset, len);
77   }
78
79   /* (non-Javadoc)
80    * @see com.maverick.crypto.digests.Tmp#putInt(int)
81    */

82   public void putInt(int i) {
83     putBytes(ByteArrayWriter.encodeInt(i));
84   }
85
86   /* (non-Javadoc)
87    * @see com.maverick.crypto.digests.Tmp#putString(java.lang.String)
88    */

89   public void putString(String JavaDoc str) {
90     putInt(str.length());
91     putBytes(str.getBytes());
92   }
93
94   /* (non-Javadoc)
95    * @see com.maverick.crypto.digests.Tmp#reset()
96    */

97   public void reset() {
98     digest.reset();
99   }
100
101   /* (non-Javadoc)
102  * @see com.maverick.crypto.digests.Tmp#doFinal()
103  */

104   public byte[] doFinal() {
105     byte[] hash = new byte[digest.getDigestSize()];
106     digest.doFinal(hash, 0);
107     return hash;
108   }
109 }
110
Popular Tags