1 17 package com.sun.org.apache.xml.internal.security.algorithms; 18 19 20 21 import java.security.MessageDigest ; 22 import java.security.NoSuchProviderException ; 23 24 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; 25 import com.sun.org.apache.xml.internal.security.utils.Constants; 26 import com.sun.org.apache.xml.internal.security.utils.EncryptionConstants; 27 import org.w3c.dom.Document ; 28 29 30 38 public class MessageDigestAlgorithm extends Algorithm { 39 40 41 static java.util.logging.Logger log = 42 java.util.logging.Logger.getLogger( 43 MessageDigestAlgorithm.class.getName()); 44 45 46 public static final String ALGO_ID_DIGEST_NOT_RECOMMENDED_MD5 = Constants.MoreAlgorithmsSpecNS + "md5"; 47 48 public static final String ALGO_ID_DIGEST_SHA1 = Constants.SignatureSpecNS + "sha1"; 49 50 public static final String ALGO_ID_DIGEST_SHA256 = EncryptionConstants.EncryptionSpecNS + "sha256"; 51 52 public static final String ALGO_ID_DIGEST_SHA384 = Constants.MoreAlgorithmsSpecNS + "sha384"; 53 54 public static final String ALGO_ID_DIGEST_SHA512 = EncryptionConstants.EncryptionSpecNS + "sha512"; 55 56 public static final String ALGO_ID_DIGEST_RIPEMD160 = EncryptionConstants.EncryptionSpecNS + "ripemd160"; 57 58 59 java.security.MessageDigest algorithm = null; 60 61 67 private MessageDigestAlgorithm(Document doc, MessageDigest messageDigest, 68 String algorithmURI) { 69 70 super(doc, algorithmURI); 71 72 this.algorithm = messageDigest; 73 } 74 75 83 public static MessageDigestAlgorithm getInstance( 84 Document doc, String algorithmURI) throws XMLSignatureException { 85 86 String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI); 87 88 if (algorithmID == null) { 89 Object [] exArgs = { algorithmURI }; 90 throw new XMLSignatureException("algorithms.NoSuchMap", exArgs); 91 } 92 93 MessageDigest md; 94 String provider=JCEMapper.getProviderId(); 95 try { 96 if (provider==null) { 97 md = MessageDigest.getInstance(algorithmID); 98 } else { 99 md = MessageDigest.getInstance(algorithmID,provider); 100 } 101 } catch (java.security.NoSuchAlgorithmException ex) { 102 Object [] exArgs = { algorithmID, 103 ex.getLocalizedMessage() }; 104 105 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 106 } catch (NoSuchProviderException ex) { 107 Object [] exArgs = { algorithmID, 108 ex.getLocalizedMessage() }; 109 110 throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs); 111 } 112 return new MessageDigestAlgorithm(doc, md, algorithmURI); 113 } 114 115 120 public java.security.MessageDigest getAlgorithm() { 121 return this.algorithm; 122 } 123 124 132 public static boolean isEqual(byte[] digesta, byte[] digestb) { 133 return java.security.MessageDigest.isEqual(digesta, digestb); 134 } 135 136 142 public byte[] digest() { 143 return this.algorithm.digest(); 144 } 145 146 153 public byte[] digest(byte input[]) { 154 return this.algorithm.digest(input); 155 } 156 157 167 public int digest(byte buf[], int offset, int len) 168 throws java.security.DigestException { 169 return this.algorithm.digest(buf, offset, len); 170 } 171 172 178 public String getJCEAlgorithmString() { 179 return this.algorithm.getAlgorithm(); 180 } 181 182 188 public java.security.Provider getJCEProvider() { 189 return this.algorithm.getProvider(); 190 } 191 192 198 public int getDigestLength() { 199 return this.algorithm.getDigestLength(); 200 } 201 202 207 public void reset() { 208 this.algorithm.reset(); 209 } 210 211 217 public void update(byte[] input) { 218 this.algorithm.update(input); 219 } 220 221 227 public void update(byte input) { 228 this.algorithm.update(input); 229 } 230 231 239 public void update(byte buf[], int offset, int len) { 240 this.algorithm.update(buf, offset, len); 241 } 242 243 244 public String getBaseNamespace() { 245 return Constants.SignatureSpecNS; 246 } 247 248 249 public String getBaseLocalName() { 250 return Constants._TAG_DIGESTMETHOD; 251 } 252 } 253 | Popular Tags |