01: /**
02: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
03: * All Rights Reserved.
04: *
05: * This file is part of jWebTk.
06: *
07: * jWebTk is free software; you can redistribute it and/or modify it under
08: * the terms of the GNU General Public License (Version 2) as published by
09: * the Free Software Foundation.
10: *
11: * jWebTk is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with jWebTk; if not, write to the Free Software Foundation,
18: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19: */package jwebtk.password;
20:
21: import java.security.MessageDigest;
22:
23: /**
24: * This class provides a simple one-way encrypted password (converted to printable characters) from a plain text string.
25: */
26:
27: public class Password {
28: /**
29: * This method returns a simple one-way encrypted password (converted to printable characters) from a plain text string.
30: *
31: * @param password a plain text password to be encrypted
32: *
33: * @return a printable plain text one-way encrypted password
34: */
35:
36: public static String getEncryptedPassword(String password)
37: throws PasswordException {
38: MessageDigest md = null;
39:
40: try {
41: md = MessageDigest.getInstance("SHA");
42: md.update(password.getBytes("UTF-8"));
43: } catch (Exception e) {
44: throw new PasswordException(e);
45: }
46:
47: return convertToString(md.digest());
48: }
49:
50: static String convertToString(byte[] byteArray) {
51: String encoding = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", str = "";
52:
53: for (int i = 0; i < byteArray.length; i++) {
54: int index = Math
55: .abs(byteArray[i] % (encoding.length() - 1));
56: str += encoding.charAt(index);
57: }
58:
59: return str;
60: }
61: }
|