001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: *
019: * Created: 14-Dec-2004 by jejking
020: * Version: $Revision: 1.1 $
021: * Last Updated: $Date: 2004/12/15 15:31:16 $
022: */
023: package org.openharmonise.rm.security.authentication;
024:
025: import java.security.*;
026: import java.util.logging.*;
027:
028: /**
029: * @author John King
030: *
031: * TODO To change the template for this generated type comment go to
032: * Window - Preferences - Java - Code Style - Code Templates
033: */
034: public class CryptPasswordHelper implements PasswordHelper {
035:
036: private final String algorithm;
037:
038: private static final String hexits = "0123456789abcdef";
039: private static final Logger logger = Logger
040: .getLogger(CryptPasswordHelper.class.getName());
041:
042: /**
043: * @throws IllegalArgumentException if unsupported algorithm passed in
044: * @param algorithm hashing algorithm to use, Must be MD5 or SHA-1
045: */
046: public CryptPasswordHelper(String algorithm) {
047: if (algorithm.equals("MD5") || algorithm.equals("SHA-1")) {
048: this .algorithm = algorithm;
049: logger.log(Level.FINE,
050: "Set up cryptpassword handler to use algorithm "
051: + algorithm);
052: } else {
053: throw new IllegalArgumentException(
054: "Unsupported algorithm - only MD5 or SHA-1 supported");
055: }
056: }
057:
058: /* (non-Javadoc)
059: * @see org.openharmonise.rm.security.authentication.PasswordHelper#compare(java.lang.String, java.lang.String, java.lang.String)
060: */
061: public boolean compare(String presentedPassword,
062: String storedPassword, String salt) {
063:
064: // need to see what the presented and the salt hash to
065: String hashedPassword = null;
066: try {
067: MessageDigest md = MessageDigest.getInstance(algorithm);
068: md.update(presentedPassword.getBytes());
069: md.update(salt.getBytes());
070: hashedPassword = toHex(md.digest());
071: } catch (NoSuchAlgorithmException e) {
072: logger.log(Level.SEVERE, "no such algorithm exception", e);
073: }
074:
075: if (hashedPassword.equals(storedPassword)) {
076: return true;
077: } else {
078: return false;
079: }
080: }
081:
082: /**
083: * Generates a hash of the password for storage in the database.
084: *
085: * @see org.openharmonise.rm.security.authentication.PasswordHelper#getNewPassword(java.lang.String, java.lang.String)
086: */
087: public String getNewPassword(String newPassword, String salt) {
088: String hashedPassword = null;
089: try {
090: MessageDigest md = MessageDigest.getInstance(algorithm);
091: md.update(newPassword.getBytes());
092: md.update(salt.getBytes());
093: hashedPassword = toHex(md.digest());
094: } catch (NoSuchAlgorithmException e) {
095: logger.log(Level.SEVERE, "no such algorithm exception", e);
096: }
097: return hashedPassword;
098: }
099:
100: /**
101: * Convert byte array to hex character string
102: * @param block byte array to convert to hexString
103: * @return String representation of byte arrayf
104: */
105: private static String toHex(byte[] block) {
106: StringBuffer buf = new StringBuffer();
107: for (int i = 0; i < block.length; ++i) {
108: buf.append(hexits.charAt((block[i] >>> 4) & 0xf));
109: buf.append(hexits.charAt(block[i] & 0xf));
110: }
111: return buf + "";
112: }
113:
114: /**
115: * Convert Hex String to byte array
116: * @param s string to convert
117: * @return byte array
118: */
119: private static byte[] fromHex(String s) {
120: s = s.toLowerCase();
121: byte[] b = new byte[(s.length() + 1) / 2];
122: int j = 0;
123: int h;
124: int nybble = -1;
125: for (int i = 0; i < s.length(); ++i) {
126: h = hexits.indexOf(s.charAt(i));
127: if (h >= 0) {
128: if (nybble < 0) {
129: nybble = h;
130: } else {
131: b[j++] = (byte) ((nybble << 4) + h);
132: nybble = -1;
133: }
134: }
135: }
136: if (nybble >= 0) {
137: b[j++] = (byte) (nybble << 4);
138: }
139: if (j < b.length) {
140: byte[] b2 = new byte[j];
141: System.arraycopy(b, 0, b2, 0, j);
142: b = b2;
143: }
144: return b;
145: }
146:
147: }
|