KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > security > Digester


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.security;
14
15 import java.security.MessageDigest JavaDoc;
16 import java.security.NoSuchAlgorithmException JavaDoc;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21
22 /**
23  * Date: Jul 6, 2004 Time: 11:26:05 AM
24  * @author Sameer Charles
25  * @version 2.0
26  */

27 public final class Digester {
28
29     /**
30      * supported algorithms
31      */

32     public static final String JavaDoc SHA1 = "SHA-1"; //$NON-NLS-1$
33

34     public static final String JavaDoc MD5 = "MD5"; //$NON-NLS-1$
35

36     /**
37      * There are five (5) FIPS-approved* algorithms for generating a condensed representation of a message (message
38      * digest): SHA-1, SHA-224, SHA-256,SHA-384, and SHA-512. <strong>Not supported yet </strong>
39      */

40     public static final String JavaDoc SHA256 = "SHA-256"; //$NON-NLS-1$
41

42     public static final String JavaDoc SHA384 = "SHA-384"; //$NON-NLS-1$
43

44     public static final String JavaDoc SHA512 = "SHA-512"; //$NON-NLS-1$
45

46     private static Logger log = LoggerFactory.getLogger(Digester.class);
47
48     /**
49      * Utility class, don't instantiate.
50      */

51     private Digester() {
52         // unused
53
}
54
55     /**
56      * Supported algorithm:
57      * <ul>
58      * <li>SHA-1 </li>
59      * <li>MD5 </li>
60      * </ul>
61      * Future:
62      * <ul>
63      * <li>SHA-256 </li>
64      * <li>SHA-384 </li>
65      * <li>SHA-512 </li>
66      * </ul>
67      */

68     public static String JavaDoc getDigest(String JavaDoc data, String JavaDoc algorithm) throws NoSuchAlgorithmException JavaDoc {
69         MessageDigest JavaDoc md = MessageDigest.getInstance(algorithm);
70         md.reset();
71         return new String JavaDoc(md.digest(data.getBytes()));
72     }
73
74     /**
75      * Supported algorithm:
76      * <ul>
77      * <li>SHA-1 </li>
78      * <li>MD5 </li>
79      * </ul>
80      * Future:
81      * <ul>
82      * <li>SHA-256 </li>
83      * <li>SHA-384 </li>
84      * <li>SHA-512 </li>
85      * </ul>
86      */

87     public static byte[] getDigest(byte[] data, String JavaDoc algorithm) throws NoSuchAlgorithmException JavaDoc {
88         MessageDigest JavaDoc md = MessageDigest.getInstance(algorithm);
89         md.reset();
90         return md.digest(data);
91     }
92
93     /**
94      * <p>
95      * gets SHA-1 encoded -> hex string
96      * </p>
97      */

98     public static String JavaDoc getSHA1Hex(String JavaDoc data) {
99         try {
100             String JavaDoc result = Digester.getDigest(data, Digester.SHA1);
101             return Digester.toHEX(result);
102         }
103         catch (NoSuchAlgorithmException JavaDoc e) {
104             log.error(e.getMessage(), e);
105         }
106         return data;
107     }
108
109     /**
110      * <p>
111      * gets MD5 encoded -> hex string
112      * </p>
113      */

114     public static String JavaDoc getMD5Hex(String JavaDoc data) {
115         try {
116             String JavaDoc result = Digester.getDigest(data, Digester.MD5);
117             return Digester.toHEX(result);
118         }
119         catch (NoSuchAlgorithmException JavaDoc e) {
120             log.error(e.getMessage(), e);
121         }
122         return data;
123     }
124
125     /**
126      * <p>
127      * converts a byte array to a string Hex
128      * @param data to be converted
129      * @return string representing hex values of the byte array
130      * </p>
131      */

132     public static String JavaDoc toHEX(String JavaDoc data) {
133         return Digester.toHEX(data.getBytes());
134     }
135
136     /**
137      * <p>
138      * converts a byte array to a string Hex
139      * @param data to be converted
140      * @return string representing hex values of the byte array
141      * </p>
142      */

143     public static String JavaDoc toHEX(byte[] data) {
144         StringBuffer JavaDoc hexValue = new StringBuffer JavaDoc();
145         char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
146         for (int i = 0; i < data.length; i++) {
147             byte byteValue = data[i];
148             hexValue.append(digits[(byteValue & 0xf0) >> 4]);
149             hexValue.append(digits[byteValue & 0x0f]);
150         }
151         return hexValue.toString();
152     }
153 }
154
Popular Tags