1 16 package org.apache.cocoon.components.modules.input; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.thread.ThreadSafe; 21 22 import org.apache.cocoon.util.HashMap; 23 24 import java.net.URLEncoder ; 25 import java.security.MessageDigest ; 26 import java.security.NoSuchAlgorithmException ; 27 import java.security.NoSuchProviderException ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.io.UnsupportedEncodingException ; 31 32 45 public class DigestMetaModule extends AbstractMetaModule implements ThreadSafe { 46 47 private String defaultAlgorithm = "SHA"; 48 private String defaultProvider = null; 49 private String defaultSalt = "salt"; 50 private String defaultEncode = "false"; 51 52 53 static final int ENCODING_NONE = 0; 54 55 static final int ENCODING_STR = 1; 56 57 static final int ENCODING_URL = 2; 58 59 static final int ENCODING_HEX = 3; 60 61 private static final HashMap encodingNames; 62 63 static { 64 HashMap names = new HashMap(); 65 names.put("false", new Integer (ENCODING_NONE)); 66 names.put("no", new Integer (ENCODING_NONE)); 67 names.put("none", new Integer (ENCODING_NONE)); 68 names.put("string", new Integer (ENCODING_STR)); 69 names.put("yes", new Integer (ENCODING_URL)); 70 names.put("true", new Integer (ENCODING_URL)); 71 names.put("hex", new Integer (ENCODING_HEX)); 72 encodingNames = names; 73 names = null; 74 } 75 76 77 public void configure(Configuration config) throws ConfigurationException { 78 79 this.inputConf = config.getChild("input-module"); 80 this.defaultInput = this.inputConf.getAttribute("name", this.defaultInput); 81 82 this.defaultAlgorithm = this.inputConf.getAttribute("algorithm",this.defaultAlgorithm); 83 this.defaultProvider = this.inputConf.getAttribute("provider",this.defaultProvider); 84 this.defaultSalt = this.inputConf.getAttribute("salt",this.defaultSalt); 85 this.defaultEncode = this.inputConf.getAttribute("encode","false"); 86 87 this.defaultAlgorithm = config.getChild("algorithm").getValue(this.defaultAlgorithm); 89 this.defaultProvider = config.getChild("provider").getValue(this.defaultProvider); 90 this.defaultSalt = config.getChild("salt").getValue(this.defaultSalt); 91 this.defaultEncode = config.getChild("encode").getValue(this.defaultEncode); 92 93 if (encodingNames.get(this.defaultEncode) == null) { 94 if (getLogger().isErrorEnabled()) 95 getLogger().error("Requested encoding is unknown: "+this.defaultEncode); 96 this.defaultEncode="false"; 97 } 98 } 99 100 101 public Object getAttribute( String name, Configuration modeConf, Map objectModel ) 102 throws ConfigurationException { 103 104 if (!this.initialized) { 105 this.lazy_initialize(); 106 } 107 if (this.defaultInput == null) { 108 if (getLogger().isWarnEnabled()) 109 getLogger().warn("No input module given. FAILING"); 110 return null; 111 } 112 113 Configuration inputConfig = null; 116 String inputName=null; 117 String algorithm = this.defaultAlgorithm; 118 String provider = this.defaultProvider; 119 String salt = this.defaultSalt; 120 int encode = ((Integer ) encodingNames.get(this.defaultEncode)).intValue(); 121 if (modeConf!=null) { 122 inputName = modeConf.getChild("input-module").getAttribute("name",null); 123 if (inputName != null) { 124 inputConfig = modeConf.getChild("input-module"); 125 } 126 algorithm = modeConf.getAttribute("algorithm", algorithm); 128 provider = modeConf.getAttribute("provider", provider); 129 salt = modeConf.getAttribute("salt", salt); 130 encode = ((Integer ) encodingNames.get(modeConf.getAttribute("encode", this.defaultEncode))).intValue(); 131 132 algorithm = modeConf.getChild("algorithm").getValue(algorithm); 134 provider = modeConf.getChild("provider").getValue(provider); 135 salt = modeConf.getChild("salt").getValue(salt); 136 encode = ((Integer ) encodingNames.get(modeConf.getChild("encode").getValue(this.defaultEncode))).intValue(); 137 } 138 139 140 Object value = getValue(name, objectModel, 141 this.input, this.defaultInput, this.inputConf, 142 null, inputName, inputConfig); 143 144 if (value != null) 145 try { 146 MessageDigest md = (provider==null ? MessageDigest.getInstance(algorithm) : 147 MessageDigest.getInstance(algorithm,provider)); 148 149 md.update((salt+(value instanceof String ? (String )value : value.toString())).getBytes()); 150 return encodeByteArray(md.digest(),encode); 151 152 } catch (NoSuchAlgorithmException nsae) { 153 if (getLogger().isWarnEnabled()) 154 getLogger().warn("A problem occurred acquiring digest algorithm '" + algorithm 155 + (provider==null?"":"' from '"+provider) +"': " + nsae.getMessage()); 156 } catch (NoSuchProviderException nspe) { 157 if (getLogger().isWarnEnabled()) 158 getLogger().warn("A problem occurred acquiring digest algorithm '" + algorithm 159 + (provider==null?"":"' from '"+provider) +"': " + nspe.getMessage()); 160 } 161 162 return null; 163 } 164 165 166 public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) 167 throws ConfigurationException { 168 169 if (!this.initialized) { 170 this.lazy_initialize(); 171 } 172 if (this.defaultInput == null) { 173 if (getLogger().isWarnEnabled()) 174 getLogger().warn("No input module given. FAILING"); 175 return null; 176 } 177 178 Configuration inputConfig = null; 181 String inputName=null; 182 if (modeConf!=null) { 183 inputName = modeConf.getChild("input-module").getAttribute("name",null); 184 if (inputName != null) { 185 inputConfig = modeConf.getChild("input-module"); 186 } 187 } 188 189 Iterator names = getNames(objectModel, 190 this.input, this.defaultInput, this.inputConf, 191 null, inputName, inputConfig); 192 193 return names; 194 195 } 196 197 198 public Object [] getAttributeValues( String name, Configuration modeConf, Map objectModel ) 199 throws ConfigurationException { 200 201 if (!this.initialized) { 202 this.lazy_initialize(); 203 } 204 if (this.defaultInput == null) { 205 if (getLogger().isWarnEnabled()) 206 getLogger().warn("No input module given. FAILING"); 207 return null; 208 } 209 210 Configuration inputConfig = null; 213 String inputName=null; 214 String algorithm = this.defaultAlgorithm; 215 String provider = this.defaultProvider; 216 String salt = this.defaultSalt; 217 int encode = ((Integer ) encodingNames.get(this.defaultEncode)).intValue(); 218 if (modeConf!=null) { 219 inputName = modeConf.getChild("input-module").getAttribute("name",null); 220 if (inputName != null) { 221 inputConfig = modeConf.getChild("input-module"); 222 } 223 algorithm = modeConf.getAttribute("algorithm", algorithm); 225 provider = modeConf.getAttribute("provider" , provider ); 226 salt = modeConf.getAttribute("salt" , salt ); 227 encode = ((Integer ) encodingNames.get(modeConf.getAttribute("encode" , this.defaultEncode))).intValue(); 228 229 algorithm = modeConf.getChild("algorithm").getValue(algorithm); 231 provider = modeConf.getChild("provider").getValue(provider); 232 salt = modeConf.getChild("salt").getValue(salt); 233 encode = ((Integer ) encodingNames.get(modeConf.getChild("encode").getValue(this.defaultEncode))).intValue(); 234 } 235 236 Object [] values = getValues(name, objectModel, 237 this.input, this.defaultInput, this.inputConf, 238 null, inputName, inputConfig); 239 Object [] result = null; 240 241 if (values != null) { 242 try { 243 MessageDigest md = (provider==null ? MessageDigest.getInstance(algorithm) : 244 MessageDigest.getInstance(algorithm,provider)); 245 246 result = new Object [values.length]; 247 for (int i=0; i<values.length; i++) { 248 md.update((salt + (values[i] instanceof String ? (String )values[i] : 249 values[i].toString())).getBytes()); 250 result[i] = encodeByteArray(md.digest(), encode); 251 } 252 return result; 253 } catch (NoSuchAlgorithmException nsae) { 254 if (getLogger().isWarnEnabled()) 255 getLogger().warn("A problem occurred acquiring digest algorithm '" + algorithm 256 + (provider==null?"":"' from '"+provider) +"': " + nsae.getMessage()); 257 } catch (NoSuchProviderException nspe) { 258 if (getLogger().isWarnEnabled()) 259 getLogger().warn("A problem occurred acquiring digest algorithm '" + algorithm 260 + (provider==null?"":"' from '"+provider) +"': " + nspe.getMessage()); 261 } 262 } 263 return result; 264 } 265 266 267 273 Object encodeByteArray(byte[] b, int encode) { 274 Object result = null; 275 switch(encode) { 276 case ENCODING_HEX: 277 result = byte2Hex(b); 278 break; 279 case ENCODING_STR: 280 try { 281 result = new String (b, "UTF-8"); 282 } catch (UnsupportedEncodingException uee) { 283 if (getLogger().isErrorEnabled()) 284 getLogger().error("UTF-8 not supported -- cannot convert message digest to String."); 285 } 286 break; 287 case ENCODING_URL: 288 try { 289 String str = new String (b, "UTF-8"); 290 result = URLEncoder.encode(str); 291 } catch (UnsupportedEncodingException uee) { 292 if (getLogger().isErrorEnabled()) 293 getLogger().error("UTF-8 not supported -- cannot convert message digest to String."); 294 } 295 break; 296 case ENCODING_NONE: 297 break; 299 default: 300 } 302 return result; 303 } 304 305 311 static String byte2Hex ( byte[] b ) { 312 StringBuffer sb = new StringBuffer ( b.length * 2 ); 313 for ( int i=0 ; i < b.length ; i++ ) { 314 sb.append( hexChar [ ( b[i] & 0xf0 ) >>> 4 ] ) ; 315 sb.append( hexChar [ ( b[i] & 0x0f ) ] ) ; 316 } 317 return sb.toString() ; 318 } 319 320 321 324 static char[] hexChar = { 325 '0' , '1' , '2' , '3' , 326 '4' , '5' , '6' , '7' , 327 '8' , '9' , 'a' , 'b' , 328 'c' , 'd' , 'e' , 'f' 329 }; 330 331 } 332 | Popular Tags |