KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > security > PasswordEncryptor


1 package dinamica.security;
2
3 import java.util.HashMap JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import sun.misc.BASE64Encoder;
6 import dinamica.*;
7
8 /**
9  * This is not a validator. it is used more like
10  * a value transformer. It will transform the parameter
11  * with name "passwd" into a MD5 hash using the following
12  * combination: userlogin:passwd<br>
13  * This validator must be used after all the other validators,
14  * because it does assume that the password and userogin parameters
15  * have been already validated.
16  * <br><br>
17  * Creation date: 10/03/2004<br>
18  * Last Update: 10/03/2004<br>
19  * (c) 2004 Martin Cordova<br>
20  * This code is released under the LGPL license<br>
21  * @author Martin Cordova (dinamica@martincordova.com)
22  * */

23 public class PasswordEncryptor extends AbstractValidator
24 {
25
26     /* (non-Javadoc)
27      * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
28      */

29     public boolean isValid(
30         HttpServletRequest JavaDoc req,
31         Recordset inputParams,
32         HashMap JavaDoc attribs)
33         throws Throwable JavaDoc
34     {
35
36         //retrieve values
37
String JavaDoc userid = inputParams.getString("userlogin");
38         String JavaDoc password = inputParams.getString("passwd");
39
40         //create MD5 hash using the string: userlogin:passwd
41
java.security.MessageDigest JavaDoc md = java.security.MessageDigest.getInstance("MD5");
42         byte[] b = (userid + ":" + password).getBytes();
43         byte[] hash = md.digest(b);
44         String JavaDoc pwd = new BASE64Encoder().encode( hash );
45
46         //set the "passwd" parameter value to the MD5 hash
47
inputParams.setValue("passwd", pwd);
48
49         //always return true
50
return true;
51         
52     }
53
54 }
55
Popular Tags