KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > tools > MD5


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

18 package sync4j.framework.tools;
19
20 import java.security.SecureRandom JavaDoc;
21 import java.security.MessageDigest JavaDoc;
22
23 /**
24  * This class groups utility methods for MD5 computation.
25  * <p>
26  *
27  * NOTE: when this class is first loaded, it creates and initializes a random
28  * generator that can be used to create random keys. We create and initialize
29  * the generator onece, because it can be a time consuming process. Better
30  * policies might be implemented in the future.
31  *
32  * @author Stefano Fornari @ Funambol
33  *
34  * @version $Id: MD5.java,v 1.2 2005/03/02 20:57:38 harrie Exp $
35  */

36 public class MD5 {
37
38     // ------------------------------------------------------------ Private data
39
private static SecureRandom JavaDoc random = null;
40     private static MessageDigest JavaDoc md = null;
41
42     // ------------------------------------------------------------ Constructors
43

44     /** Creates a new instance of MD5 */
45     protected MD5() {
46     }
47
48     // ---------------------------------------------------------- Public methods
49

50     /**
51      * Returns a new nonce to be used for MD5 authentication. The nonce bytes
52      * are guaranteed to be in the printable range from ascii 32 to 128.
53      *
54      * @return a new 16 bytes long nonce
55      */

56     public static byte[] getNextNonce() {
57         byte[] nextNonce = new byte[16];
58         random.nextBytes(nextNonce);
59
60         int i;
61         for (int j=0; j<nextNonce.length; ++j) {
62             i = nextNonce[j] & 0x000000ff;
63             if ((i<32) || (i>128)) {
64                 nextNonce[j] = (byte)(32 + (i % 64));
65             }
66         }
67
68         return nextNonce;
69     }
70
71     /**
72      * MD5ies the given content
73      *
74      * @param data the data to be digested
75      *
76      */

77     public static byte[] digest(byte[] data) {
78         md.reset();
79         return md.digest(data);
80     }
81
82     // --------------------------------------------------------- Private methods
83

84     /**
85      * Creates and initialize the random generator. Called ad class loading
86      * time.
87      */

88     private static void randomGeneratorInit()
89     throws java.security.NoSuchAlgorithmException JavaDoc {
90         random = SecureRandom.getInstance("SHA1PRNG");
91     }
92
93     // ------------------------------------------------------------- Static code
94

95     static {
96         try {
97             randomGeneratorInit();
98             md = MessageDigest.getInstance("MD5");
99         } catch(Exception JavaDoc e) {
100             e.printStackTrace();
101         }
102     }
103 }
104
Popular Tags