/** * Copyright © 2001 The JA-SIG Collaborative. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *u * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the JA-SIG Collaborative * (http://www.jasig.org/)." * * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * */ package org.jasig.portal.security; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Date; /** *
A version of md5passwd which can be called from other Java classes to encode a password.
* Returns an encoded password string. * * @author rtwigg@uccs.edu * (Excerpted from md5passwd authored by Andrew Newman, newman@yale.edu) * @version $Revision: 2.41 $ */ public class Md5 { public static String encryptPasswd(String passwd) { byte[] hash, rnd = new byte[8], fin = new byte[24]; Long date = new Long((new Date()).getTime()); SecureRandom r = new SecureRandom((date.toString()).getBytes()); MessageDigest md = null; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) {} r.nextBytes(rnd); md.update(rnd); hash = md.digest(passwd.getBytes()); System.arraycopy(rnd, 0, fin, 0, 8); System.arraycopy(hash, 0, fin, 8, 16); String encrPwd = (encode(fin)); return(encrPwd); } // // This was originally Jonathan B. Knudsen's Example from his book // Java Cryptography published by O'Reilly Associates (1st Edition 1998) // private static String encode(byte[] raw) { StringBuffer encoded = new StringBuffer(); for (int i = 0; i < raw.length; i += 3) { encoded.append(encodeBlock(raw, i)); } return encoded.toString(); } private static char[] encodeBlock(byte[] raw, int offset) { int block = 0; int slack = raw.length - offset - 1; int end = (slack >= 2) ? 2 : slack; for (int i = 0; i <= end; i++) { byte b = raw[offset + i]; int neuter = (b < 0) ? b + 256 : b; block += neuter << (8 * (2 - i)); } char[] base64 = new char[4]; for (int i = 0; i < 4; i++) { int sixbit = (block >>> (6 * (3 - i))) & 0x3f; base64[i] = getChar(sixbit); } if (slack < 1) base64[2] = '='; if (slack < 2) base64[3] = '='; return base64; } private static char getChar(int sixBit) { if (sixBit >= 0 && sixBit <= 25) return (char)('A' + sixBit); if (sixBit >= 26 && sixBit <= 51) return (char)('a' + (sixBit - 26)); if (sixBit >= 52 && sixBit <= 61) return (char)('0' + (sixBit - 52)); if (sixBit == 62) return '+'; if (sixBit == 63) return '/'; return '?'; } }