Source Code Cross Referenced for DigestClientAuthenticationMethod.java in  » 6.0-JDK-Modules » Java-Advanced-Imaging » examples » authorization » 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 » 6.0 JDK Modules » Java Advanced Imaging » examples.authorization 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DigestClientAlgorithm.java
003:         *
004:         * Created on January 7, 2003, 10:45 AM
005:         */
006:
007:        package examples.authorization;
008:
009:        import java.security.*;
010:
011:        /**
012:         * Get this interface from the nist-sip IM
013:         * @author  olivier deruelle
014:         */
015:        public class DigestClientAuthenticationMethod implements 
016:                ClientAuthenticationMethod {
017:
018:            private String realm;
019:            private String userName;
020:            private String uri;
021:            private String nonce;
022:            private String password;
023:            private String method;
024:            private String cnonce;
025:            private MessageDigest messageDigest;
026:
027:            /**
028:             * to hex converter
029:             */
030:            private static final char[] toHex = { '0', '1', '2', '3', '4', '5',
031:                    '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
032:
033:            /**
034:             * convert an array of bytes to an hexadecimal string
035:             * @return a string
036:             * @param b bytes array to convert to a hexadecimal
037:             * string
038:             */
039:
040:            public static String toHexString(byte b[]) {
041:                int pos = 0;
042:                char[] c = new char[b.length * 2];
043:                for (int i = 0; i < b.length; i++) {
044:                    c[pos++] = toHex[(b[i] >> 4) & 0x0F];
045:                    c[pos++] = toHex[b[i] & 0x0f];
046:                }
047:                return new String(c);
048:            }
049:
050:            public void initialize(String realm, String userName, String uri,
051:                    String nonce, String password, String method,
052:                    String cnonce, String algorithm) throws Exception {
053:                if (realm == null)
054:                    throw new Exception("The realm parameter is null");
055:                this .realm = realm;
056:                if (userName == null)
057:                    throw new Exception("The userName parameter is null");
058:                this .userName = userName;
059:                if (uri == null)
060:                    throw new Exception("The uri parameter is null");
061:                this .uri = uri;
062:                if (nonce == null)
063:                    throw new Exception("The nonce parameter is null");
064:                this .nonce = nonce;
065:                if (password == null)
066:                    throw new Exception("The password parameter is null");
067:                this .password = password;
068:                if (method == null)
069:                    throw new Exception("The method parameter is null");
070:                this .method = method;
071:                this .cnonce = cnonce;
072:                if (algorithm == null)
073:                    throw new Exception("The algorithm parameter is null");
074:                try {
075:                    messageDigest = MessageDigest.getInstance(algorithm);
076:                } catch (NoSuchAlgorithmException ex) {
077:                    System.out
078:                            .println("DEBUG, DigestClientAuthenticationMethod, initialize(): "
079:                                    + "ERROR: Digest algorithm does not exist.");
080:                    throw new Exception(
081:                            "ERROR: Digest algorithm does not exist.");
082:                }
083:            }
084:
085:            /** 
086:             * generate the response
087:             */
088:            public String generateResponse() {
089:                if (userName == null) {
090:                    System.out
091:                            .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
092:                                    + "ERROR: no userName parameter");
093:                    return null;
094:                }
095:                if (realm == null) {
096:                    System.out
097:                            .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
098:                                    + "ERROR: no realm parameter");
099:                    return null;
100:                }
101:
102:                System.out
103:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
104:                                + "Trying to generate a response for the user: "
105:                                + userName + " , with " + "the realm: " + realm);
106:
107:                if (password == null) {
108:                    System.out
109:                            .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
110:                                    + "ERROR: no password parameter");
111:                    return null;
112:                }
113:                if (method == null) {
114:                    System.out
115:                            .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
116:                                    + "ERROR: no method parameter");
117:                    return null;
118:                }
119:                if (uri == null) {
120:                    System.out
121:                            .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
122:                                    + "ERROR: no uri parameter");
123:                    return null;
124:                }
125:                if (nonce == null) {
126:                    System.out
127:                            .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
128:                                    + "ERROR: no nonce parameter");
129:                    return null;
130:                }
131:                if (messageDigest == null) {
132:                    System.out
133:                            .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(): "
134:                                    + "ERROR: the algorithm is not set");
135:                    return null;
136:                }
137:
138:                /*******    GENERATE RESPONSE      ************************************/
139:                System.out
140:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), userName:"
141:                                + userName + "!");
142:                System.out
143:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), realm:"
144:                                + realm + "!");
145:                System.out
146:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), password:"
147:                                + password + "!");
148:                System.out
149:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), uri:"
150:                                + uri + "!");
151:                System.out
152:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), nonce:"
153:                                + nonce + "!");
154:                System.out
155:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), method:"
156:                                + method + "!");
157:                // A1
158:                String A1 = userName + ":" + realm + ":" + password;
159:                byte mdbytes[] = messageDigest.digest(A1.getBytes());
160:                String HA1 = toHexString(mdbytes);
161:                System.out
162:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), HA1:"
163:                                + HA1 + "!");
164:                //A2
165:                String A2 = method.toUpperCase() + ":" + uri;
166:                mdbytes = messageDigest.digest(A2.getBytes());
167:                String HA2 = toHexString(mdbytes);
168:                System.out
169:                        .println("DEBUG, DigestClientAuthenticationMethod, generateResponse(), HA2:"
170:                                + HA2 + "!");
171:                //KD
172:                String KD = HA1 + ":" + nonce;
173:                if (cnonce != null) {
174:                    if (cnonce.length() > 0)
175:                        KD += ":" + cnonce;
176:                }
177:                KD += ":" + HA2;
178:                mdbytes = messageDigest.digest(KD.getBytes());
179:                String response = toHexString(mdbytes);
180:
181:                System.out
182:                        .println("DEBUG, DigestClientAlgorithm, generateResponse():"
183:                                + " response generated: " + response);
184:
185:                return response;
186:            }
187:
188:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.