本文整理匯總了Java中javax.xml.crypto.dsig.XMLSignatureFactory.newDigestMethod方法的典型用法代碼示例。如果您正苦於以下問題:Java XMLSignatureFactory.newDigestMethod方法的具體用法?Java XMLSignatureFactory.newDigestMethod怎麽用?Java XMLSignatureFactory.newDigestMethod使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.crypto.dsig.XMLSignatureFactory
的用法示例。
在下文中一共展示了XMLSignatureFactory.newDigestMethod方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addDigestInfosAsReferences
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
private void addDigestInfosAsReferences(List<DigestInfo> digestInfos, XMLSignatureFactory signatureFactory,
List<Reference> references)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, MalformedURLException {
if (null == digestInfos) {
return;
}
for (DigestInfo digestInfo : digestInfos) {
byte[] documentDigestValue = digestInfo.digestValue;
DigestMethod digestMethod = signatureFactory.newDigestMethod(getXmlDigestAlgo(digestInfo.digestAlgo), null);
String uri = FilenameUtils.getName(new File(digestInfo.description).toURI().toURL().getFile());
Reference reference = signatureFactory.newReference(uri, digestMethod, null, null, null,
documentDigestValue);
references.add(reference);
}
}
示例2: preSign
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
public void preSign(XMLSignatureFactory signatureFactory, Document document, String signatureId,
List<X509Certificate> signingCertificateChain, List<Reference> references, List<XMLObject> objects)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
DigestMethod digestMethod = signatureFactory.newDigestMethod(this.digestAlgo.getXmlAlgoId(), null);
List<Transform> transforms = new LinkedList<Transform>();
Map<String, String> xpathNamespaceMap = new HashMap<String, String>();
xpathNamespaceMap.put("ds", "http://www.w3.org/2000/09/xmldsig#");
// XPath v1 - slow...
// Transform envelopedTransform = signatureFactory.newTransform(
// CanonicalizationMethod.XPATH, new XPathFilterParameterSpec(
// "not(ancestor-or-self::ds:Signature)",
// xpathNamespaceMap));
// XPath v2 - fast...
List<XPathType> types = new ArrayList<XPathType>(1);
types.add(new XPathType("/descendant::*[name()='ds:Signature']", XPathType.Filter.SUBTRACT, xpathNamespaceMap));
Transform envelopedTransform = signatureFactory.newTransform(CanonicalizationMethod.XPATH2,
new XPathFilter2ParameterSpec(types));
transforms.add(envelopedTransform);
Transform exclusiveTransform = signatureFactory.newTransform(CanonicalizationMethod.EXCLUSIVE,
(TransformParameterSpec) null);
transforms.add(exclusiveTransform);
Reference reference = signatureFactory.newReference("", digestMethod, transforms, null, this.dsReferenceId);
references.add(reference);
}
示例3: preSign
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
public void preSign(XMLSignatureFactory signatureFactory, Document document, String signatureId,
List<X509Certificate> signingCertificateChain, List<Reference> references, List<XMLObject> objects)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
DigestMethod digestMethod = signatureFactory.newDigestMethod(this.digestAlgo.getXmlAlgoId(), null);
List<Transform> transforms = new LinkedList<Transform>();
Transform envelopedTransform = signatureFactory.newTransform(CanonicalizationMethod.ENVELOPED,
(TransformParameterSpec) null);
transforms.add(envelopedTransform);
Transform exclusiveTransform = signatureFactory.newTransform(CanonicalizationMethod.EXCLUSIVE,
(TransformParameterSpec) null);
transforms.add(exclusiveTransform);
Reference reference = signatureFactory.newReference("", digestMethod, transforms, null, null);
references.add(reference);
}
示例4: preSign
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
public void preSign(XMLSignatureFactory signatureFactory, Document document, String signatureId,
List<X509Certificate> signingCertificateChain, List<Reference> references, List<XMLObject> objects)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
LOG.debug("pre sign");
Element dateElement = document.createElementNS("", "dc:date");
dateElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:dc", "http://purl.org/dc/elements/1.1/");
DateTime dateTime = new DateTime(DateTimeZone.UTC);
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
String now = fmt.print(dateTime);
now = now.substring(0, now.indexOf("Z"));
LOG.debug("now: " + now);
dateElement.setTextContent(now);
String signaturePropertyId = "sign-prop-" + UUID.randomUUID().toString();
List<XMLStructure> signaturePropertyContent = new LinkedList<XMLStructure>();
signaturePropertyContent.add(new DOMStructure(dateElement));
SignatureProperty signatureProperty = signatureFactory.newSignatureProperty(signaturePropertyContent,
"#" + signatureId, signaturePropertyId);
List<XMLStructure> objectContent = new LinkedList<XMLStructure>();
List<SignatureProperty> signaturePropertiesContent = new LinkedList<SignatureProperty>();
signaturePropertiesContent.add(signatureProperty);
SignatureProperties signatureProperties = signatureFactory.newSignatureProperties(signaturePropertiesContent,
null);
objectContent.add(signatureProperties);
objects.add(signatureFactory.newXMLObject(objectContent, null, null, null));
DigestMethod digestMethod = signatureFactory.newDigestMethod(this.digestAlgo.getXmlAlgoId(), null);
Reference reference = signatureFactory.newReference("#" + signaturePropertyId, digestMethod);
references.add(reference);
}
示例5: addManifestObject
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
private void addManifestObject(XMLSignatureFactory signatureFactory, Document document, String signatureId,
List<Reference> references, List<XMLObject> objects)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
Manifest manifest = constructManifest(signatureFactory, document);
String objectId = "idPackageObject"; // really has to be this value.
List<XMLStructure> objectContent = new LinkedList<XMLStructure>();
objectContent.add(manifest);
addSignatureTime(signatureFactory, document, signatureId, objectContent);
objects.add(signatureFactory.newXMLObject(objectContent, objectId, null, null));
DigestMethod digestMethod = signatureFactory.newDigestMethod(this.digestAlgo.getXmlAlgoId(), null);
Reference reference = signatureFactory.newReference("#" + objectId, digestMethod, null,
"http://www.w3.org/2000/09/xmldsig#Object", null);
references.add(reference);
}
示例6: addSignatureInfo
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
private void addSignatureInfo(XMLSignatureFactory signatureFactory, Document document, String signatureId,
List<Reference> references, List<XMLObject> objects)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
List<XMLStructure> objectContent = new LinkedList<XMLStructure>();
Element signatureInfoElement = document.createElementNS(OFFICE_DIGSIG_NS, "SignatureInfoV1");
signatureInfoElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", OFFICE_DIGSIG_NS);
Element manifestHashAlgorithmElement = document.createElementNS(OFFICE_DIGSIG_NS, "ManifestHashAlgorithm");
manifestHashAlgorithmElement.setTextContent("http://www.w3.org/2000/09/xmldsig#sha1");
signatureInfoElement.appendChild(manifestHashAlgorithmElement);
List<XMLStructure> signatureInfoContent = new LinkedList<XMLStructure>();
signatureInfoContent.add(new DOMStructure(signatureInfoElement));
SignatureProperty signatureInfoSignatureProperty = signatureFactory.newSignatureProperty(signatureInfoContent,
"#" + signatureId, "idOfficeV1Details");
List<SignatureProperty> signaturePropertyContent = new LinkedList<SignatureProperty>();
signaturePropertyContent.add(signatureInfoSignatureProperty);
SignatureProperties signatureProperties = signatureFactory.newSignatureProperties(signaturePropertyContent,
null);
objectContent.add(signatureProperties);
String objectId = "idOfficeObject";
objects.add(signatureFactory.newXMLObject(objectContent, objectId, null, null));
DigestMethod digestMethod = signatureFactory.newDigestMethod(this.digestAlgo.getXmlAlgoId(), null);
Reference reference = signatureFactory.newReference("#" + objectId, digestMethod, null,
"http://www.w3.org/2000/09/xmldsig#Object", null);
references.add(reference);
}
示例7: preSign
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
public void preSign(XMLSignatureFactory signatureFactory, Document document, String signatureId,
List<X509Certificate> signingCertificateChain, List<Reference> references, List<XMLObject> objects)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
DigestMethod digestMethod = signatureFactory.newDigestMethod(DigestMethod.SHA1, null);
for (String uri : this.uris) {
Reference reference = signatureFactory.newReference(uri, digestMethod);
references.add(reference);
}
}
示例8: testJsr105ReferenceUri
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
@Test
public void testJsr105ReferenceUri() throws Exception {
String uri = FilenameUtils.getName(new File("foo bar.txt").toURI().toURL().getFile());
KeyPair keyPair = generateKeyPair();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM", new XMLDSigRI());
XMLSignContext signContext = new DOMSignContext(keyPair.getPrivate(), document);
byte[] externalDocument = "hello world".getBytes();
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
messageDigest.update(externalDocument);
byte[] documentDigestValue = messageDigest.digest();
DigestMethod digestMethod = signatureFactory.newDigestMethod(DigestMethod.SHA1, null);
Reference reference = signatureFactory.newReference(uri, digestMethod, null, null, null, documentDigestValue);
SignatureMethod signatureMethod = signatureFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
CanonicalizationMethod canonicalizationMethod = signatureFactory.newCanonicalizationMethod(
CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null);
javax.xml.crypto.dsig.SignedInfo signedInfo = signatureFactory.newSignedInfo(canonicalizationMethod,
signatureMethod, Collections.singletonList(reference));
javax.xml.crypto.dsig.XMLSignature xmlSignature = signatureFactory.newXMLSignature(signedInfo, null);
xmlSignature.sign(signContext);
}
示例9: sign
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
private void sign(KeyStore keyStore, KeyPair keyPair, String alias, Document document, List<EbMSDataSource> dataSources) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException, KeyException, MarshalException, XMLSignatureException, KeyStoreException
{
//XMLSignatureFactory signFactory = XMLSignatureFactory.getInstance("DOM");
XMLSignatureFactory signFactory = XMLSignatureFactory.getInstance();
DigestMethod sha1DigestMethod = signFactory.newDigestMethod(DigestMethod.SHA1,null);
List<Transform> transforms = new ArrayList<Transform>();
transforms.add(signFactory.newTransform(Transform.ENVELOPED,(TransformParameterSpec)null));
Map<String,String> m = new HashMap<String,String>();
m.put("soap","http://schemas.xmlsoap.org/soap/envelope/");
transforms.add(signFactory.newTransform(Transform.XPATH,new XPathFilterParameterSpec("not(ancestor-or-self::node()[@soap:actor=\"urn:oasis:names:tc:ebxml-msg:service:nextMSH\"]|ancestor-or-self::node()[@soap:actor=\"http://schemas.xmlsoap.org/soap/actor/next\"])",m)));
transforms.add(signFactory.newTransform(CanonicalizationMethod.INCLUSIVE,(TransformParameterSpec)null));
List<Reference> references = new ArrayList<Reference>();
references.add(signFactory.newReference("",sha1DigestMethod,transforms,null,null));
for (EbMSDataSource dataSource : dataSources)
references.add(signFactory.newReference("cid:" + dataSource.getContentId(),sha1DigestMethod,Collections.emptyList(),null,null,DigestUtils.sha(IOUtils.toByteArray(dataSource.getInputStream()))));
SignedInfo signedInfo = signFactory.newSignedInfo(signFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,(C14NMethodParameterSpec)null),signFactory.newSignatureMethod(SignatureMethod.RSA_SHA1,null),references);
List<XMLStructure> keyInfoElements = new ArrayList<XMLStructure>();
KeyInfoFactory keyInfoFactory = signFactory.getKeyInfoFactory();
keyInfoElements.add(keyInfoFactory.newKeyValue(keyPair.getPublic()));
Certificate[] certificates = keyStore.getCertificateChain(alias);
//keyInfoElements.add(keyInfoFactory.newX509Data(Arrays.asList(certificates)));
keyInfoElements.add(keyInfoFactory.newX509Data(Collections.singletonList(certificates[0])));
KeyInfo keyInfo = keyInfoFactory.newKeyInfo(keyInfoElements);
XMLSignature signature = signFactory.newXMLSignature(signedInfo,keyInfo);
Element soapHeader = getFirstChildElement(document.getDocumentElement());
DOMSignContext signContext = new DOMSignContext(keyPair.getPrivate(),soapHeader);
signContext.putNamespacePrefix(XMLSignature.XMLNS,"ds");
signature.sign(signContext);
}
示例10: preSign
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
public void preSign(XMLSignatureFactory signatureFactory, Document document, String signatureId,
List<X509Certificate> signingCertificateChain, List<Reference> references, List<XMLObject> objects)
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
// construct identity document
IdentityType identity = this.objectFactory.createIdentityType();
String identityId = "identity-" + UUID.randomUUID().toString();
identity.setId(identityId);
if (null != this.identityDTO) {
identity.setFirstName(this.identityDTO.firstName);
identity.setName(this.identityDTO.name);
identity.setMiddleName(this.identityDTO.middleName);
GenderType gender;
if (this.identityDTO.male) {
gender = GenderType.MALE;
} else {
gender = GenderType.FEMALE;
}
identity.setGender(gender);
}
if (null != this.photoData) {
PhotoType photo = this.objectFactory.createPhotoType();
photo.setValue(this.photoData);
photo.setMimeType("image/jpeg");
identity.setPhoto(photo);
}
// marshalling
Node marshallNode = document.createElement("marshall-node");
try {
this.marshaller.marshal(this.objectFactory.createIdentity(identity), marshallNode);
} catch (JAXBException e) {
throw new RuntimeException("JAXB error: " + e.getMessage(), e);
}
Node identityNode = marshallNode.getFirstChild();
// ds:Object
String objectId = "identity-object-" + UUID.randomUUID().toString();
List<XMLStructure> identityObjectContent = new LinkedList<XMLStructure>();
identityObjectContent.add(new DOMStructure(identityNode));
XMLObject identityObject = signatureFactory.newXMLObject(identityObjectContent, objectId, null, null);
objects.add(identityObject);
// ds:Reference
DigestMethod digestMethod = signatureFactory.newDigestMethod(this.digestAlgo.getXmlAlgoId(), null);
List<Transform> transforms = new LinkedList<Transform>();
Transform exclusiveTransform = signatureFactory.newTransform(CanonicalizationMethod.INCLUSIVE,
(TransformParameterSpec) null);
transforms.add(exclusiveTransform);
Reference reference = signatureFactory.newReference("#" + objectId, digestMethod, transforms, REFERENCE_TYPE,
null);
references.add(reference);
}
示例11: testJsr105Signature
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
@Test
public void testJsr105Signature() throws Exception {
KeyPair keyPair = PkiTestUtils.generateKeyPair();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElementNS("urn:test", "tns:root");
rootElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", "urn:test");
document.appendChild(rootElement);
Element dataElement = document.createElementNS("urn:test", "tns:data");
dataElement.setAttributeNS(null, "Id", "id-1234");
dataElement.setIdAttribute("Id", true);
dataElement.setTextContent("data to be signed");
rootElement.appendChild(dataElement);
XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM", new XMLDSigRI());
XMLSignContext signContext = new DOMSignContext(keyPair.getPrivate(), document.getDocumentElement());
signContext.putNamespacePrefix(javax.xml.crypto.dsig.XMLSignature.XMLNS, "ds");
DigestMethod digestMethod = signatureFactory.newDigestMethod(DigestMethod.SHA1, null);
Reference reference = signatureFactory.newReference("#id-1234", digestMethod);
DOMReference domReference = (DOMReference) reference;
assertNull(domReference.getCalculatedDigestValue());
assertNull(domReference.getDigestValue());
SignatureMethod signatureMethod = signatureFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
CanonicalizationMethod canonicalizationMethod = signatureFactory.newCanonicalizationMethod(
CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null);
SignedInfo signedInfo = signatureFactory.newSignedInfo(canonicalizationMethod, signatureMethod,
Collections.singletonList(reference));
javax.xml.crypto.dsig.XMLSignature xmlSignature = signatureFactory.newXMLSignature(signedInfo, null);
DOMXMLSignature domXmlSignature = (DOMXMLSignature) xmlSignature;
domXmlSignature.marshal(document.getDocumentElement(), "ds", (DOMCryptoContext) signContext);
domReference.digest(signContext);
// xmlSignature.sign(signContext);
// LOG.debug("signed document: " + toString(document));
Element nsElement = document.createElement("ns");
nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS);
Node digestValueNode = XPathAPI.selectSingleNode(document, "//ds:DigestValue", nsElement);
assertNotNull(digestValueNode);
String digestValueTextContent = digestValueNode.getTextContent();
LOG.debug("digest value text content: " + digestValueTextContent);
assertFalse(digestValueTextContent.isEmpty());
}
示例12: testJsr105SignatureExternalXML
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
@Test
public void testJsr105SignatureExternalXML() throws Exception {
KeyPair keyPair = PkiTestUtils.generateKeyPair();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElementNS("urn:test", "tns:root");
rootElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", "urn:test");
document.appendChild(rootElement);
Element dataElement = document.createElementNS("urn:test", "tns:data");
dataElement.setAttributeNS(null, "Id", "id-1234");
dataElement.setTextContent("data to be signed");
rootElement.appendChild(dataElement);
XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM", new XMLDSigRI());
XMLSignContext signContext = new DOMSignContext(keyPair.getPrivate(), document.getDocumentElement());
signContext.setURIDereferencer(new MyURIDereferencer());
signContext.putNamespacePrefix(javax.xml.crypto.dsig.XMLSignature.XMLNS, "ds");
DigestMethod digestMethod = signatureFactory.newDigestMethod(DigestMethod.SHA1, null);
List<Transform> transforms = new LinkedList<Transform>();
Transform transform = signatureFactory.newTransform(CanonicalizationMethod.INCLUSIVE,
(TransformParameterSpec) null);
transforms.add(transform);
Reference reference = signatureFactory.newReference("/helloworld.xml", digestMethod, transforms, null, null);
DOMReference domReference = (DOMReference) reference;
assertNull(domReference.getCalculatedDigestValue());
assertNull(domReference.getDigestValue());
SignatureMethod signatureMethod = signatureFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
CanonicalizationMethod canonicalizationMethod = signatureFactory.newCanonicalizationMethod(
CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null);
SignedInfo signedInfo = signatureFactory.newSignedInfo(canonicalizationMethod, signatureMethod,
Collections.singletonList(reference));
javax.xml.crypto.dsig.XMLSignature xmlSignature = signatureFactory.newXMLSignature(signedInfo, null);
DOMXMLSignature domXmlSignature = (DOMXMLSignature) xmlSignature;
domXmlSignature.marshal(document.getDocumentElement(), "ds", (DOMCryptoContext) signContext);
domReference.digest(signContext);
// xmlSignature.sign(signContext);
// LOG.debug("signed document: " + toString(document));
Element nsElement = document.createElement("ns");
nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS);
Node digestValueNode = XPathAPI.selectSingleNode(document, "//ds:DigestValue", nsElement);
assertNotNull(digestValueNode);
String digestValueTextContent = digestValueNode.getTextContent();
LOG.debug("digest value text content: " + digestValueTextContent);
assertFalse(digestValueTextContent.isEmpty());
}
示例13: testJsr105SignatureExternalXMLWithDTD
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
@Test
public void testJsr105SignatureExternalXMLWithDTD() throws Exception {
KeyPair keyPair = PkiTestUtils.generateKeyPair();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElementNS("urn:test", "tns:root");
rootElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", "urn:test");
document.appendChild(rootElement);
Element dataElement = document.createElementNS("urn:test", "tns:data");
dataElement.setAttributeNS(null, "Id", "id-1234");
dataElement.setTextContent("data to be signed");
rootElement.appendChild(dataElement);
XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM", new XMLDSigRI());
XMLSignContext signContext = new DOMSignContext(keyPair.getPrivate(), document.getDocumentElement());
signContext.setURIDereferencer(new MyURIDereferencer());
signContext.putNamespacePrefix(javax.xml.crypto.dsig.XMLSignature.XMLNS, "ds");
DigestMethod digestMethod = signatureFactory.newDigestMethod(DigestMethod.SHA1, null);
List<Transform> transforms = new LinkedList<Transform>();
Transform transform = signatureFactory.newTransform(CanonicalizationMethod.INCLUSIVE,
(TransformParameterSpec) null);
LOG.debug("transform type: " + transform.getClass().getName());
transforms.add(transform);
Reference reference = signatureFactory.newReference("/bookstore.xml", digestMethod, transforms, null, null);
DOMReference domReference = (DOMReference) reference;
assertNull(domReference.getCalculatedDigestValue());
assertNull(domReference.getDigestValue());
SignatureMethod signatureMethod = signatureFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
CanonicalizationMethod canonicalizationMethod = signatureFactory.newCanonicalizationMethod(
CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null);
SignedInfo signedInfo = signatureFactory.newSignedInfo(canonicalizationMethod, signatureMethod,
Collections.singletonList(reference));
javax.xml.crypto.dsig.XMLSignature xmlSignature = signatureFactory.newXMLSignature(signedInfo, null);
DOMXMLSignature domXmlSignature = (DOMXMLSignature) xmlSignature;
domXmlSignature.marshal(document.getDocumentElement(), "ds", (DOMCryptoContext) signContext);
domReference.digest(signContext);
// xmlSignature.sign(signContext);
// LOG.debug("signed document: " + toString(document));
Element nsElement = document.createElement("ns");
nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS);
Node digestValueNode = XPathAPI.selectSingleNode(document, "//ds:DigestValue", nsElement);
assertNotNull(digestValueNode);
String digestValueTextContent = digestValueNode.getTextContent();
LOG.debug("digest value text content: " + digestValueTextContent);
assertFalse(digestValueTextContent.isEmpty());
}
示例14: sign
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
private static Document sign(Document doc) throws NoSuchAlgorithmException,
InvalidAlgorithmParameterException, KeyStoreException,
CertificateException, FileNotFoundException, IOException,
UnrecoverableEntryException, javax.xml.crypto.MarshalException,
XMLSignatureException, TransformerException {
// Create a DOM XMLSignatureFactory that will be used to
// generate the enveloped signature.
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
// Create a Reference to the enveloped document (in this case,
// you are signing the whole document, so a URI of "" signifies
// that, and also specify the SHA1 digest algorithm and
// the ENVELOPED Transform.
Transform transform = fac.newTransform(Transform.ENVELOPED,
(TransformParameterSpec) null);
DigestMethod digestMethod = fac
.newDigestMethod(DigestMethod.SHA1, null);
Reference ref = fac.newReference("", digestMethod,
Collections.singletonList(transform), null, null);
// Create the SignedInfo.
CanonicalizationMethod canonicalizationMethod = fac
.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE,
(C14NMethodParameterSpec) null);
SignatureMethod signatureMethod = fac.newSignatureMethod(
SignatureMethod.RSA_SHA1, null);
SignedInfo si = fac.newSignedInfo(canonicalizationMethod,
signatureMethod, Collections.singletonList(ref));
// Load the KeyStore and get the signing key and certificate.
String password = "123456";
String keyAlias = "1";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new FileInputStream("UDIR.PAS2.keystore"),password.toCharArray());
KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks
.getEntry(keyAlias,
new KeyStore.PasswordProtection(password.toCharArray()));
X509Certificate cert = (X509Certificate) keyEntry.getCertificate();
// Create the KeyInfo containing the X509Data.
KeyInfoFactory kif = fac.getKeyInfoFactory();
List x509Content = new ArrayList();
x509Content.add(cert);
X509Data xd = kif.newX509Data(x509Content);
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
// Create a DOMSignContext and specify the RSA PrivateKey and
// location of the resulting XMLSignature's parent element.
DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(),
doc.getDocumentElement());
// Create the XMLSignature, but don't sign it yet.
XMLSignature signature = fac.newXMLSignature(si, ki);
// Marshal, generate, and sign the enveloped signature.
signature.sign(dsc);
// Output the resulting document.
OutputStream os = new FileOutputStream("xmlOut.xml");
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(os));
return doc;
}
示例15: createDigestMethod
import javax.xml.crypto.dsig.XMLSignatureFactory; //導入方法依賴的package包/類
@Nonnull
@OverrideOnDemand
protected DigestMethod createDigestMethod (@Nonnull final XMLSignatureFactory aSignatureFactory) throws Exception
{
return aSignatureFactory.newDigestMethod (DigestMethod.SHA1, (DigestMethodParameterSpec) null);
}