1 28 package net.sf.jguard.ext.util; 29 30 import java.security.MessageDigest ; 31 import java.security.NoSuchAlgorithmException ; 32 import java.security.Security ; 33 import java.util.Iterator ; 34 import java.util.Set ; 35 36 37 38 47 public class CryptUtils { 48 49 public final static String NONE_ALGORITHM = "NONE"; 51 private static String digestAlgorithm = NONE_ALGORITHM; 52 private static MessageDigest md = null; 53 54 public static char[] salt = null; 55 56 65 private static char[] hexDump(byte src[]) 66 { 67 char buf[] = new char[src.length * 2]; 68 for (int b = 0; b < src.length; b++) { 69 String byt = Integer.toHexString(src[b] & 0xFF); 70 if (byt.length() < 2) { 71 buf[b * 2 + 0] = '0'; 72 buf[b * 2 + 1] = byt.charAt(0); 73 } else { 74 buf[b * 2 + 0] = byt.charAt(0); 75 buf[b * 2 + 1] = byt.charAt(1); 76 } 77 } 78 return buf; 79 } 80 81 88 public static void smudge(char pwd[]) 89 { 90 if (null != pwd) { 91 for (int b = 0; b < pwd.length; b++) { 92 pwd[b] = 0; 93 } 94 } 95 } 96 97 102 public static void smudge(byte pwd[]) 103 { 104 if (null != pwd) { 105 for (int b = 0; b < pwd.length; b++) { 106 pwd[b] = 0; 107 } 108 } 109 } 110 111 121 public static char[] cryptPassword(char[] pwd) throws NoSuchAlgorithmException { 122 123 char[] newPwd= null; 124 125 if(salt!=null){ 126 newPwd = saltPassword(pwd); 127 }else{ 128 newPwd = pwd; 129 } 130 131 if(digestAlgorithm.equals(NONE_ALGORITHM)){ 133 return newPwd; 134 } 135 136 if (null == md) { 138 md = MessageDigest.getInstance(digestAlgorithm); 139 } 140 md.reset(); 141 142 byte pwdb[] = new byte[newPwd.length]; 144 145 for (int b = 0; b < newPwd.length; b++) { 146 pwdb[b] = (byte) newPwd[b]; 147 } 148 149 char crypt[] = hexDump(md.digest(pwdb)); 150 smudge(pwdb); 151 return crypt; 152 } 153 154 private static char[] saltPassword(char[] pwd) { 155 char[] merged = new char[pwd.length+salt.length]; 157 int mergedIndex = 0; 158 for(int i=0;i<pwd.length;i++){ 159 merged[mergedIndex]=pwd[i]; 160 mergedIndex++; 161 } 162 for(int j=0;j<salt.length;j++){ 163 merged[mergedIndex]=salt[j]; 164 mergedIndex++; 165 } 166 return merged; 167 } 168 169 172 public static String getDigestAlgorithm() { 173 return digestAlgorithm; 174 } 175 176 182 public static void setDigestAlgorithm(String algorithm) throws NoSuchAlgorithmException { 183 if (NONE_ALGORITHM.equals(algorithm)){ 184 digestAlgorithm = algorithm; 185 return; 186 } 187 Set algorithmsSet = Security.getAlgorithms("MessageDigest"); 188 if(algorithmsSet.size()<1){ 189 throw new NoSuchAlgorithmException ("no Message Digest algorithms implemented in this jvm "); 190 } 191 Iterator it = algorithmsSet.iterator(); 192 boolean algorithmImplemented =false; 193 String algorithmTemp; 194 195 short count = 0; 196 while(it.hasNext()){ 197 count++; 198 algorithmTemp = (String )it.next(); 199 if(algorithmTemp.equalsIgnoreCase(algorithm)){ 200 algorithmImplemented=true; 201 break; 202 } 203 } 204 205 if(algorithmImplemented==true){ 206 digestAlgorithm = algorithm; 207 }else{ 208 throw new NoSuchAlgorithmException ("Message Digest algorithm '"+algorithm+"' not implemented "); 209 } 210 } 211 212 219 public static boolean setSalt(char[] saltCandidate) { 220 if(salt!=null && saltCandidate!=null &&!saltCandidate.equals("")){ 221 return false; 222 } 223 CryptUtils.salt = saltCandidate; 224 return true; 225 226 } 227 228 public static char[] getSalt() { 229 return salt; 230 } 231 232 } 233 | Popular Tags |