1 17 package org.cofax.util.digest; 18 19 import java.security.MessageDigest ; 20 import java.security.NoSuchAlgorithmException ; 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 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 hexits = "0123456789abcdef"; 29 30 private static final String id = "SHA"; 31 32 public boolean isSupported(String 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 46 public boolean checkPassword(String password, String digest) { 47 boolean passwordMatch = false; 48 49 MessageDigest 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); } else if (digest.regionMatches(true, 0, "{SSHA}", 0, 6)) { 56 digest = digest.substring(6); } 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 nsae) { 69 CofaxToolsUtil.log("Algorithme SHA-1 non supporte a la verification du password" + nsae + id); 70 } 71 72 return passwordMatch; 73 } 74 75 78 public static String getSSHADigest(String password, String salt) { 79 String digest = null; 80 81 MessageDigest 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 (Base64.encode(concatenate(pwhash, salt.getBytes()))); 90 91 } catch (NoSuchAlgorithmException nsae) { 92 CofaxToolsUtil.log("Algorithme SHA-1 non supporte a la creation du hashage" + nsae + id); 93 } 94 95 return digest; 96 } 97 98 101 public static String getSHADigest(String password) { 102 String digest = null; 103 104 MessageDigest 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 (Base64.encode(pwhash)); 112 113 } catch (NoSuchAlgorithmException 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 toHex(byte[] block) { 143 StringBuffer buf = new StringBuffer (); 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 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 |