Source Code Cross Referenced for MD5Token.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » auth » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » JGroups 2.4.1 sp3 » org.jgroups.auth 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jgroups.auth;
002:
003:        import org.jgroups.util.Util;
004:        import org.jgroups.util.HashUtils;
005:        import org.jgroups.Message;
006:
007:        import java.io.DataOutputStream;
008:        import java.io.IOException;
009:        import java.io.DataInputStream;
010:        import java.util.HashMap;
011:        import java.util.Properties;
012:        import java.security.NoSuchAlgorithmException;
013:        import java.security.MessageDigest;
014:
015:        /**
016:         * <p>
017:         * This is an example of using a preshared token that is encrypted using an MD5/SHA hash for authentication purposes.  All members of the group have to have the same string value in the JGroups config.
018:         *</p>
019:         * <p>
020:         * Configuration parameters for this example are shown below:
021:         * </p>
022:         * <ul>
023:         *  <li>token_hash (required) = MD5(default)/SHA</li>
024:         *  <li>auth_value (required) = the string to encrypt</li>
025:         * </ul>
026:         * @see org.jgroups.auth.AuthToken
027:         * @author Chris Mills
028:         */
029:        public class MD5Token extends AuthToken {
030:
031:            public static final String TOKEN_ATTR = "auth_value";
032:            public static final String TOKEN_TYPE = "token_hash";
033:
034:            private String token = null;
035:            private String hash_type = "MD5";
036:
037:            public MD5Token() {
038:                //need an empty constructor
039:            }
040:
041:            public MD5Token(String token) {
042:                this .token = hash(token);
043:            }
044:
045:            public MD5Token(String token, String hash_type) {
046:                this .token = hash(token);
047:                this .hash_type = hash_type;
048:            }
049:
050:            public void setValue(Properties properties) {
051:                this .token = hash((String) properties.get(MD5Token.TOKEN_ATTR));
052:                properties.remove(MD5Token.TOKEN_ATTR);
053:
054:                if (properties.containsKey(MD5Token.TOKEN_TYPE)) {
055:                    hash_type = (String) properties.get(MD5Token.TOKEN_TYPE);
056:                    properties.remove(MD5Token.TOKEN_TYPE);
057:                }
058:            }
059:
060:            public String getName() {
061:                return "org.jgroups.auth.MD5Token";
062:            }
063:
064:            /**
065:             * Called during setup to hash the auth_value string in to an MD5/SHA hash
066:             * @param token the string to hash
067:             * @return the hashed version of the string
068:             */
069:            private String hash(String token) {
070:                //perform the hashing of the token key
071:                String hashedToken = null;
072:
073:                if (hash_type.equalsIgnoreCase("SHA")) {
074:                    hashedToken = HashUtils.sha(token);
075:                } else {
076:                    hashedToken = HashUtils.md5(token);
077:                }
078:
079:                if (hashedToken == null) {
080:                    //failed to encrypt
081:                    if (log.isWarnEnabled()) {
082:                        log
083:                                .warn("Failed to hash token - sending in clear text");
084:                    }
085:                    return token;
086:                }
087:                return hashedToken;
088:            }
089:
090:            public boolean authenticate(AuthToken token, Message msg) {
091:
092:                if ((token != null) && (token instanceof  MD5Token)) {
093:                    //Found a valid Token to authenticate against
094:                    MD5Token serverToken = (MD5Token) token;
095:
096:                    if ((this .token != null) && (serverToken.token != null)
097:                            && (this .token.equalsIgnoreCase(serverToken.token))) {
098:                        //validated
099:                        if (log.isDebugEnabled()) {
100:                            log.debug("MD5Token match");
101:                        }
102:                        return true;
103:                    } else {
104:                        if (log.isWarnEnabled()) {
105:                            log.warn("Authentication failed on MD5Token");
106:                        }
107:                        return false;
108:                    }
109:                }
110:
111:                if (log.isWarnEnabled()) {
112:                    log.warn("Invalid AuthToken instance - wrong type or null");
113:                }
114:                return false;
115:            }
116:
117:            public void writeTo(DataOutputStream out) throws IOException {
118:                if (log.isDebugEnabled()) {
119:                    log.debug("MD5Token writeTo()");
120:                }
121:                Util.writeString(this .token, out);
122:            }
123:
124:            public void readFrom(DataInputStream in) throws IOException,
125:                    IllegalAccessException, InstantiationException {
126:                if (log.isDebugEnabled()) {
127:                    log.debug("MD5Token readFrom()");
128:                }
129:                this.token = Util.readString(in);
130:            }
131:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.