1 25 26 package org.objectweb.jonas.security.realm.lib; 27 28 import java.security.MessageDigest ; 29 import java.security.NoSuchAlgorithmException ; 30 31 38 public class HashHelper { 39 40 43 private static final int HEX_CONSTANT = 0xFF; 44 45 48 private static final String [] ALGORITHM = {"MD5", "MD2", "SHA-1", "SHA"}; 49 50 53 private static final String [] SEPARATOR_ALGORITHM = {"MD5:", "MD2:", "SHA-1:", "SHA:"}; 54 55 58 private static final String [] SEPARATOR_ALGORITHM_BIS = {"{MD5}", "{MD2}", "{SHA-1}", "{SHA}"}; 59 60 63 public static final String DEFAULT_ALGO = "MD5"; 64 65 68 private HashHelper() { 69 } 70 71 78 private static boolean isAValidAlgorithm(String algo) { 79 for (int i = 0; i < ALGORITHM.length; i++) { 80 if (algo.equalsIgnoreCase(ALGORITHM[i])) { 81 return true; 82 } 83 } 84 return false; 85 } 86 87 93 public static HashPassword getHashPassword(String password) { 94 String pass = null; 95 String algo = null; 96 for (int i = 0; i < ALGORITHM.length; i++) { 98 if (password.toUpperCase().startsWith(SEPARATOR_ALGORITHM[i])) { 99 pass = password.substring(SEPARATOR_ALGORITHM[i].length()); 100 algo = password.substring(0, SEPARATOR_ALGORITHM[i].length() - 1); 101 return new HashPassword(pass, algo); 102 } 103 if (password.toUpperCase().startsWith(SEPARATOR_ALGORITHM_BIS[i])) { 104 pass = password.substring(SEPARATOR_ALGORITHM_BIS[i].length()); 105 algo = password.substring(1, SEPARATOR_ALGORITHM_BIS[i].length() - 1); 106 return new HashPassword(pass, algo); 107 } 108 } 109 110 return new HashPassword(password, null); 112 } 113 114 121 public static char[] hexDump(byte[] src) { 122 char[] buf = new char[src.length * 2]; 123 124 for (int b = 0; b < src.length; b++) { 125 String byt = Integer.toHexString(src[b] & HEX_CONSTANT); 126 127 if (byt.length() < 2) { 128 buf[b * 2 + 0] = '0'; 129 buf[b * 2 + 1] = byt.charAt(0); 130 } else { 131 buf[b * 2 + 0] = byt.charAt(0); 132 buf[b * 2 + 1] = byt.charAt(1); 133 } 134 } 135 return buf; 136 } 137 138 144 public static void smudge(char[] pwd) { 145 if (pwd != null) { 146 for (int b = 0; b < pwd.length; b++) { 147 pwd[b] = 0; 148 } 149 } 150 } 151 152 156 public static void smudge(byte[] pwd) { 157 if (pwd != null) { 158 for (int b = 0; b < pwd.length; b++) { 159 pwd[b] = 0; 160 } 161 } 162 } 163 164 172 public static String hashPassword(char[] pwd) throws NoSuchAlgorithmException { 173 return hashPassword(pwd, DEFAULT_ALGO); 174 } 175 176 186 public static String hashPassword(char[] pwd, String algo) throws NoSuchAlgorithmException { 187 188 if (!isAValidAlgorithm(algo)) { 189 throw new NoSuchAlgorithmException ("Your algorithm isn't valid or not yet supported."); 190 } 191 MessageDigest md = MessageDigest.getInstance(algo); 192 md.reset(); 193 194 byte[] pwdb = new byte[pwd.length]; 195 byte[] crypt = null; 196 for (int b = 0; b < pwd.length; b++) { 197 pwdb[b] = (byte) pwd[b]; 198 } 199 crypt = md.digest(pwdb); 200 smudge(pwdb); 201 return new String (Base64.encode(crypt)); 202 } 203 204 214 public static String hashPassword(String string, String algo) throws NoSuchAlgorithmException { 215 return hashPassword(string.toCharArray(), algo); 216 } 217 } | Popular Tags |