KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > Password


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: Password.java 43240 2004-08-16 16:21:22Z andreas $ */
19
20 package org.apache.lenya.ac;
21
22 import java.security.MessageDigest JavaDoc;
23
24 import org.apache.log4j.Category;
25
26 /**
27  * Encrypt plain text password
28  * Example: "message digest" becomes "f96b697d7cb7938d525a2f31aaf161d0" (hexadecimal notation (32 characters))
29  */

30 public class Password {
31     private static Category log = Category.getInstance(Password.class);
32
33     /**
34      * CLI
35      *
36      * @param args plain text password
37      */

38     public static void main(String JavaDoc[] args) {
39         if (args.length != 1) {
40             System.out.println("Usage: plain-text-password");
41
42             return;
43         }
44
45         try {
46             System.out.println(Password.encrypt(args[0]));
47         } catch (Exception JavaDoc e) {
48             System.err.println(e);
49         }
50     }
51
52     /**
53      * Encrypt plain text password
54      *
55      * @param plain plain text password
56      * @return encrypted password
57      */

58     public static String JavaDoc encrypt(String JavaDoc plain) {
59         return getMD5(plain);
60     }
61
62     /**
63      * Returns the MD5 representation of a string.
64      * @param plain The plain string.
65      * @return A string.
66      */

67     public static String JavaDoc getMD5(String JavaDoc plain) {
68         MessageDigest JavaDoc md = null;
69         try {
70             md = MessageDigest.getInstance("MD5");
71         } catch (java.security.NoSuchAlgorithmException JavaDoc e) {
72             log.error(e);
73         }
74         return stringify(md.digest(plain.getBytes()));
75     }
76
77     /**
78      * Converts a byte buffer to a string.
79      * @param buf The buffer.
80      * @return A string.
81      */

82     private static String JavaDoc stringify(byte[] buf) {
83         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2 * buf.length);
84
85         for (int i = 0; i < buf.length; i++) {
86             int h = (buf[i] & 0xf0) >> 4;
87             int l = (buf[i] & 0x0f);
88             sb.append(new Character JavaDoc((char) ((h > 9) ? (('a' + h) - 10) : ('0' + h))));
89             sb.append(new Character JavaDoc((char) ((l > 9) ? (('a' + l) - 10) : ('0' + l))));
90         }
91
92         return sb.toString();
93     }
94 }
95
Popular Tags