1 16 package org.roller.presentation.atomapi; 17 18 import java.io.IOException ; 19 import java.io.UnsupportedEncodingException ; 20 import java.security.MessageDigest ; 21 import java.security.NoSuchAlgorithmException ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Date ; 24 25 import org.apache.commons.codec.binary.Base64; 26 27 31 public class WSSEUtilities { 32 public static synchronized String generateDigest( 33 byte[] nonce, byte[] created, byte[] password) { 34 String result = null; 35 try { 36 MessageDigest 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 (base64Encode(digest)); 43 } 44 catch (NoSuchAlgorithmException e) { 45 result = null; 46 } 47 return result; 48 } 49 public static byte[] base64Decode(String value) throws IOException { 50 return Base64.decodeBase64(value.getBytes("UTF-8")); 51 } 52 public static String base64Encode(byte[] value) { 53 return new String (Base64.encodeBase64(value)); 54 } 55 public static String generateWSSEHeader(String userName, String password) 56 throws UnsupportedEncodingException { 57 58 byte[] nonceBytes = Long.toString(new Date ().getTime()).getBytes(); 59 String nonce = new String (WSSEUtilities.base64Encode(nonceBytes)); 60 61 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'"); 62 String created = sdf.format(new Date ()); 63 64 String digest = WSSEUtilities.generateDigest( 65 nonceBytes, created.getBytes("UTF-8"), password.getBytes("UTF-8")); 66 67 StringBuffer header = new StringBuffer ("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 |