KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > WSSEUtilities


1 /*
2  * Copyright 2005, Dave Johnson
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 package org.apache.roller.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.security.MessageDigest JavaDoc;
21 import java.security.NoSuchAlgorithmException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import org.apache.commons.codec.binary.Base64;
26
27 /**
28  * Utilties to support WSSE authentication.
29  * @author Dave Johnson
30  */

31 public class WSSEUtilities {
32     public static synchronized String JavaDoc generateDigest(
33             byte[] nonce, byte[] created, byte[] password) {
34         String JavaDoc result = null;
35         try {
36             MessageDigest JavaDoc digester = MessageDigest.getInstance("SHA");
37             digester.reset();
38             digester.update(nonce);
39             digester.update(created);
40             digester.update(password);
41             byte[] digest = digester.digest();
42             result = new String JavaDoc(base64Encode(digest));
43         }
44         catch (NoSuchAlgorithmException JavaDoc e) {
45             result = null;
46         }
47         return result;
48     }
49     public static byte[] base64Decode(String JavaDoc value) throws IOException JavaDoc {
50         return Base64.decodeBase64(value.getBytes("UTF-8"));
51     }
52     public static String JavaDoc base64Encode(byte[] value) {
53         return new String JavaDoc(Base64.encodeBase64(value));
54     }
55     public static String JavaDoc generateWSSEHeader(String JavaDoc userName, String JavaDoc password)
56     throws UnsupportedEncodingException JavaDoc {
57        
58         byte[] nonceBytes = Long.toString(new Date JavaDoc().getTime()).getBytes();
59         String JavaDoc nonce = new String JavaDoc(WSSEUtilities.base64Encode(nonceBytes));
60         
61         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss'Z'");
62         String JavaDoc created = sdf.format(new Date JavaDoc());
63         
64         String JavaDoc digest = WSSEUtilities.generateDigest(
65                 nonceBytes, created.getBytes("UTF-8"), password.getBytes("UTF-8"));
66         
67         StringBuffer JavaDoc header = new StringBuffer JavaDoc("UsernameToken Username=\"");
68         header.append(userName);
69         header.append("\", ");
70         header.append("PasswordDigest=\"");
71         header.append(digest);
72         header.append("\", ");
73         header.append("Nonce=\"");
74         header.append(nonce);
75         header.append("\", ");
76         header.append("Created=\"");
77         header.append(created);
78         header.append("\"");
79         return header.toString();
80     }
81 }
82
Popular Tags