1 23 24 package com.sun.enterprise.security.util; 25 26 import java.lang.*; 27 import java.io.*; 28 import java.util.*; 29 import java.security.*; 30 import java.security.spec.*; 31 32 import com.sun.enterprise.util.i18n.StringManager; 33 34 import sun.misc.BASE64Decoder; 35 import sun.misc.BASE64Encoder; 36 37 38 48 public class SSHA 49 { 50 private static final String SSHA_TAG = "{SSHA}"; 51 private static StringManager sm = 52 StringManager.getManager("com.sun.enterprise.security.util"); 53 private static MessageDigest md = null; 54 55 56 65 public static byte[] compute(byte[] salt, byte[] password) 66 throws IASSecurityException 67 { 68 byte[] buff = new byte[password.length + salt.length]; 69 System.arraycopy(password, 0, buff, 0, password.length); 70 System.arraycopy(salt, 0, buff, password.length, salt.length); 71 72 byte[] hash = null; 73 74 synchronized (SSHA.class) { 75 76 if (md == null) { 77 try { 78 md = MessageDigest.getInstance("SHA"); 79 } catch (Exception e) { 80 throw new IASSecurityException(e); 81 } 82 } 83 84 assert (md != null); 85 md.reset(); 86 hash = md.digest(buff); 87 } 88 89 assert (hash.length==20); 91 return hash; 92 } 93 94 95 106 public static byte[] compute(int saltBytes, byte[] password) 107 throws IASSecurityException 108 { 109 SecureRandom rng=new SecureRandom(); 110 byte[] salt=new byte[saltBytes]; 111 rng.nextBytes(salt); 112 113 return compute(salt, password); 114 } 115 116 117 125 public static String encode(byte[] salt, byte[] hash) 126 { 127 assert (hash.length==20); 128 byte[] res = new byte[20+salt.length]; 129 System.arraycopy(hash, 0, res, 0, 20); 130 System.arraycopy(salt, 0, res, 20, salt.length); 131 132 BASE64Encoder encoder = new BASE64Encoder(); 133 String encoded = encoder.encode(res); 134 135 String out = SSHA_TAG + encoded; 136 return out; 137 } 138 139 140 150 public static String computeAndEncode(byte[] salt, byte[] password) 151 throws IASSecurityException 152 { 153 byte[] hash = compute(salt, password); 154 return encode(salt, hash); 155 } 156 157 158 168 public static String computeAndEncode(int saltBytes, byte[] password) 169 throws IASSecurityException 170 { 171 SecureRandom rng=new SecureRandom(); 172 byte[] salt=new byte[saltBytes]; 173 rng.nextBytes(salt); 174 175 byte[] hash = compute(salt, password); 176 return encode(salt, hash); 177 } 178 179 180 192 public static boolean verify(String encoded, byte[] password) 193 throws IASSecurityException 194 { 195 byte[] hash = new byte[20]; 196 byte[] salt = decode(encoded, hash); 197 return verify(salt, hash, password); 198 } 199 200 201 214 public static boolean verify(byte[] salt, byte[] hash, byte[] password) 215 throws IASSecurityException 216 { 217 byte[] newHash = compute(salt, password); 218 return Arrays.equals(hash, newHash); 219 } 220 221 222 235 public static byte[] decode(String encoded, byte[] hashResult) 236 throws IASSecurityException 237 { 238 assert (hashResult.length==20); 239 if (!encoded.startsWith(SSHA_TAG)) { 240 String msg = sm.getString("ssha.badformat", encoded); 241 throw new IASSecurityException(msg); 242 } 243 244 String ssha = encoded.substring(SSHA_TAG.length()); 245 246 BASE64Decoder decoder = new BASE64Decoder(); 247 byte[] result = null; 248 249 try { 250 result = decoder.decodeBuffer(ssha); 251 } catch (IOException e) { 252 throw new IASSecurityException(e); 253 } 254 assert (result.length > 20); 255 256 byte[] salt = new byte[result.length - 20]; 257 258 System.arraycopy(result, 0, hashResult, 0, 20); 259 System.arraycopy(result, 20, salt, 0, result.length-20); 260 261 return salt; 262 } 263 264 265 266 267 } 268 | Popular Tags |