KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > util > digest > SHADigestHandler


1 //----------------------------------------------------------------------------79
2
// SMILE
3
//
4
// @(#) $Header: /cvsroot/cofax/cofax/src/org/cofax/util/digest/SHADigestHandler.java,v 1.1.2.1 2006/12/11 16:29:51 fxrobin Exp $
5
//
6
// Langage : Java
7
//
8
// Description : Classe utilitaire pour creer et verifier des digest SHA et SSHA
9
//
10
// Auteur : Benoît Jacquemont
11
// Date creation : 2004-05-19
12
//
13
// Historique :
14
// 2004-05-19 - BJT - Creation
15
//----------------------------------------------------------------------------79
16

17 package org.cofax.util.digest;
18
19 import java.security.MessageDigest JavaDoc;
20 import java.security.NoSuchAlgorithmException JavaDoc;
21 import org.cofax.cms.CofaxToolsUtil;
22 import com.Ostermiller.util.Base64;
23
24 public class SHADigestHandler implements IDigestHandler {
25
26     public static final String JavaDoc RCS_ID = "$Header: /cvsroot/cofax/cofax/src/org/cofax/util/digest/SHADigestHandler.java,v 1.1.2.1 2006/12/11 16:29:51 fxrobin Exp $";
27
28     private static final String JavaDoc hexits = "0123456789abcdef";
29
30     private static final String JavaDoc id = "SHA";
31
32     public boolean isSupported(String JavaDoc digest) {
33         if (digest.regionMatches(true, 0, "{SHA}", 0, 5)) {
34             return true;
35         } else if (digest.regionMatches(true, 0, "{SSHA}", 0, 6)) {
36             return true;
37         }
38
39         return false;
40     }
41
42     /**
43      * Verifie que le mot de passe correspond bien au hashage passe en
44      * parametre. Renvoi true si c'est le cas, false sinon
45      */

46     public boolean checkPassword(String JavaDoc password, String JavaDoc digest) {
47         boolean passwordMatch = false;
48
49         MessageDigest JavaDoc sha = null;
50         try {
51             sha = MessageDigest.getInstance("SHA-1");
52
53             if (digest.regionMatches(true, 0, "{SHA}", 0, 5)) {
54                 digest = digest.substring(5); // ignore the label
55
} else if (digest.regionMatches(true, 0, "{SSHA}", 0, 6)) {
56                 digest = digest.substring(6); // ignore the label
57
}
58             byte[][] hs = split(Base64.decode(digest.getBytes()), 20);
59             byte[] hash = hs[0];
60             byte[] salt = hs[1];
61             sha.reset();
62             sha.update(password.getBytes());
63             sha.update(salt);
64             byte[] pwhash = sha.digest();
65             if (MessageDigest.isEqual(hash, pwhash)) {
66                 passwordMatch = true;
67             }
68         } catch (NoSuchAlgorithmException JavaDoc nsae) {
69             CofaxToolsUtil.log("Algorithme SHA-1 non supporte a la verification du password" + nsae + id);
70         }
71
72         return passwordMatch;
73     }
74
75     /**
76      * Genere le hashage (digest) SSHA partir du mot de passe et du salt.
77      */

78     public static String JavaDoc getSSHADigest(String JavaDoc password, String JavaDoc salt) {
79         String JavaDoc digest = null;
80
81         MessageDigest JavaDoc sha = null;
82         try {
83             sha = MessageDigest.getInstance("SHA-1");
84
85             sha.reset();
86             sha.update(password.getBytes());
87             sha.update(salt.getBytes());
88             byte[] pwhash = sha.digest();
89             digest = "{SSHA}" + new String JavaDoc(Base64.encode(concatenate(pwhash, salt.getBytes())));
90
91         } catch (NoSuchAlgorithmException JavaDoc nsae) {
92             CofaxToolsUtil.log("Algorithme SHA-1 non supporte a la creation du hashage" + nsae + id);
93         }
94
95         return digest;
96     }
97
98     /**
99      * Genere le hashage (digest) SHA partir du mot de passe et du salt.
100      */

101     public static String JavaDoc getSHADigest(String JavaDoc password) {
102         String JavaDoc digest = null;
103
104         MessageDigest JavaDoc sha = null;
105         try {
106             sha = MessageDigest.getInstance("SHA-1");
107
108             sha.reset();
109             sha.update(password.getBytes());
110             byte[] pwhash = sha.digest();
111             digest = "{SHA}" + new String JavaDoc(Base64.encode(pwhash));
112
113         } catch (NoSuchAlgorithmException JavaDoc nsae) {
114             CofaxToolsUtil.log("Algorithme SHA-1 non supporte a la creation du hashage" + nsae + id);
115         }
116
117         return digest;
118     }
119
120     private static byte[] concatenate(byte[] l, byte[] r) {
121         byte[] b = new byte[l.length + r.length];
122         System.arraycopy(l, 0, b, 0, l.length);
123         System.arraycopy(r, 0, b, l.length, r.length);
124         return b;
125     }
126
127     private static byte[][] split(byte[] src, int n) {
128         byte[] l, r;
129         if (src.length <= n) {
130             l = src;
131             r = new byte[0];
132         } else {
133             l = new byte[n];
134             r = new byte[src.length - n];
135             System.arraycopy(src, 0, l, 0, n);
136             System.arraycopy(src, n, r, 0, r.length);
137         }
138         byte[][] lr = { l, r };
139         return lr;
140     }
141
142     private static String JavaDoc toHex(byte[] block) {
143         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
144         for (int i = 0; i < block.length; ++i) {
145             buf.append(hexits.charAt((block[i] >>> 4) & 0xf));
146             buf.append(hexits.charAt(block[i] & 0xf));
147         }
148         return buf + "";
149     }
150
151     private static byte[] fromHex(String JavaDoc s) {
152         s = s.toLowerCase();
153         byte[] b = new byte[(s.length() + 1) / 2];
154         int j = 0;
155         int h;
156         int nybble = -1;
157         for (int i = 0; i < s.length(); ++i) {
158             h = hexits.indexOf(s.charAt(i));
159             if (h >= 0) {
160                 if (nybble < 0) {
161                     nybble = h;
162                 } else {
163                     b[j++] = (byte) ((nybble << 4) + h);
164                     nybble = -1;
165                 }
166             }
167         }
168         if (nybble >= 0) {
169             b[j++] = (byte) (nybble << 4);
170         }
171         if (j < b.length) {
172             byte[] b2 = new byte[j];
173             System.arraycopy(b, 0, b2, 0, j);
174             b = b2;
175         }
176         return b;
177     }
178 }
179
Popular Tags