本文整理汇总了Java中javax.xml.crypto.dsig.CanonicalizationMethod类的典型用法代码示例。如果您正苦于以下问题:Java CanonicalizationMethod类的具体用法?Java CanonicalizationMethod怎么用?Java CanonicalizationMethod使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CanonicalizationMethod类属于javax.xml.crypto.dsig包,在下文中一共展示了CanonicalizationMethod类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTransformsXsltXpath
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
private List<AlgorithmMethod> getTransformsXsltXpath() {
try {
AlgorithmMethod transformXslt = XmlSignatureHelper.getXslTransform("/org/apache/camel/component/xmlsecurity/xslt_test.xsl");
Map<String, String> namespaceMap = new HashMap<String, String>(1);
namespaceMap.put("n0", "https://org.apache/camel/xmlsecurity/test");
AlgorithmMethod transformXpath = XmlSignatureHelper.getXPathTransform("//n0:XMLSecurity/n0:Content", namespaceMap);
// I removed base 64 transform because the JDK implementation does
// not correctly support this transformation
// AlgorithmMethod transformBase64 = helper.getBase64Transform();
List<AlgorithmMethod> result = new ArrayList<AlgorithmMethod>(3);
result.add(XmlSignatureHelper.getCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE));
result.add(transformXslt);
result.add(transformXpath);
// result.add(transformBase64);
return result;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
示例2: get
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
@Override
public Output get(Input input) throws Exception {
Transform transform = input.getSignatureFactory().newTransform(CanonicalizationMethod.INCLUSIVE, (TransformParameterSpec) null);
Reference ref = input.getSignatureFactory().newReference("#propertiesObject",
input.getSignatureFactory().newDigestMethod(input.getContentDigestAlgorithm(), null), Collections.singletonList(transform),
null, null);
String doc2 = "<ts:timestamp xmlns:ts=\"http:/timestamp\">" + System.currentTimeMillis() + "</ts:timestamp>";
InputStream is = new ByteArrayInputStream(doc2.getBytes("UTF-8"));
Document doc = XmlSignatureHelper.newDocumentBuilder(Boolean.TRUE).parse(is);
DOMStructure structure = new DOMStructure(doc.getDocumentElement());
SignatureProperty prop = input.getSignatureFactory().newSignatureProperty(Collections.singletonList(structure),
input.getSignatureId(), "property");
SignatureProperties properties = input.getSignatureFactory().newSignatureProperties(Collections.singletonList(prop), "properties");
XMLObject propertiesObject = input.getSignatureFactory().newXMLObject(Collections.singletonList(properties), "propertiesObject",
null, null);
XmlSignatureProperties.Output result = new Output();
result.setReferences(Collections.singletonList(ref));
result.setObjects(Collections.singletonList(propertiesObject));
return result;
}
示例3: createReference
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
@Override
protected DSSReference createReference(DSSDocument document, int referenceIndex) {
// <ds:Reference Id="signed-data-ref" Type="http://www.w3.org/2000/09/xmldsig#Object"
// URI="#signed-data-idfc5ff27ee49763d9ba88ba5bbc49f732">
final DSSReference reference = new DSSReference();
reference.setId("r-id-" + referenceIndex);
reference.setContents(document);
reference.setDigestMethodAlgorithm(params.getDigestAlgorithm());
if (params.isManifestSignature()) {
reference.setType(HTTP_WWW_W3_ORG_2000_09_XMLDSIG_MANIFEST);
reference.setUri("#" + ((params.getManifestId() == null) ? "manifest" : params.getManifestId()));
DSSTransform xmlTransform = new DSSTransform();
xmlTransform.setAlgorithm(Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS);
reference.setTransforms(Arrays.asList(xmlTransform));
} else {
reference.setType(HTTP_WWW_W3_ORG_2000_09_XMLDSIG_OBJECT);
reference.setUri("#o-id-" + referenceIndex);
DSSTransform base64Transform = new DSSTransform();
base64Transform.setAlgorithm(CanonicalizationMethod.BASE64);
reference.setTransforms(Arrays.asList(base64Transform));
}
return reference;
}
示例4: init
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
@Before
public void init() throws Exception {
documentToSign = new FileDocument(new File("src/test/resources/sample.xml"));
signatureParameters = new XAdESSignatureParameters();
signatureParameters.bLevel().setSigningDate(new Date());
signatureParameters.setSigningCertificate(getSigningCert());
signatureParameters.setCertificateChain(getCertificateChain());
signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPED);
signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B);
signatureParameters.setSignedInfoCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE);
signatureParameters.setSignedPropertiesCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE);
// Will add the signature within the tr tag
signatureParameters.setXPathLocationString("//*[local-name() = 'tr']");
service = new XAdESService(getCompleteCertificateVerifier());
}
示例5: sign
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
public <T extends Node> T sign(T node) {
checkNotNull(node);
checkArgument(node instanceof Document || node instanceof Element);
try {
Element element = node instanceof Document ? ((Document) node).getDocumentElement() : (Element) node;
DOMSignContext dsc = new DOMSignContext(privateKey, element);
XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM");
List<Transform> transformList = new LinkedList<>();
transformList.add(signatureFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null));
transformList.add(signatureFactory.newTransform(C14N_TRANSFORM_METHOD, (TransformParameterSpec) null));
Node child = findFirstElementChild(element);
((Element) child).setIdAttribute("Id", true);
String id = child.getAttributes().getNamedItem("Id").getNodeValue();
String uri = String.format("#%s", id);
Reference reference = signatureFactory.newReference(uri,
signatureFactory.newDigestMethod(DigestMethod.SHA1, null), transformList, null, null);
SignedInfo signedInfo = signatureFactory.newSignedInfo(signatureFactory.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), signatureFactory
.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(reference));
KeyInfoFactory kif = signatureFactory.getKeyInfoFactory();
X509Data x509Data = kif.newX509Data(Collections.singletonList(certificateChain[0]));
KeyInfo keyInfo = kif.newKeyInfo(Collections.singletonList(x509Data));
XMLSignature xmlSignature = signatureFactory.newXMLSignature(signedInfo, keyInfo);
xmlSignature.sign(dsc);
return node;
}
catch (Exception ex) {
throw new IllegalArgumentException("Erro ao assinar XML.", ex);
}
}
示例6: XmlSignatureHandler
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
public XmlSignatureHandler() throws NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
this.builderFactory = DocumentBuilderFactory.newInstance();
this.builderFactory.setNamespaceAware(true);
this.transformerFactory = TransformerFactory.newInstance();
this.signatureFactory = XMLSignatureFactory.getInstance("DOM");
this.digestMethod = signatureFactory.newDigestMethod(DigestMethod.SHA1, null);
this.transformList = new ArrayList<Transform>(2);
this.transformList.add(
signatureFactory.newTransform(
Transform.ENVELOPED,
(TransformParameterSpec) null));
this.transformList.add(
signatureFactory.newTransform(
"http://www.w3.org/TR/2001/REC-xml-c14n-20010315",
(TransformParameterSpec) null));
this.canonicalizationMethod = this.signatureFactory.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE,
(C14NMethodParameterSpec) null);
this.signatureMethod = this.signatureFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
this.keyInfoFactory = this.signatureFactory.getKeyInfoFactory();
}
示例7: signSamlElement
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
/**
* Sign SAML element.
*
* @param element the element
* @param privKey the priv key
* @param pubKey the pub key
* @return the element
*/
private static org.jdom.Element signSamlElement(final org.jdom.Element element, final PrivateKey privKey, final PublicKey pubKey) {
try {
final String providerName = System.getProperty("jsr105Provider", SIGNATURE_FACTORY_PROVIDER_CLASS);
final XMLSignatureFactory sigFactory = XMLSignatureFactory
.getInstance("DOM", (Provider) Class.forName(providerName).newInstance());
final List<Transform> envelopedTransform = Collections.singletonList(sigFactory.newTransform(Transform.ENVELOPED,
(TransformParameterSpec) null));
final Reference ref = sigFactory.newReference(StringUtils.EMPTY, sigFactory
.newDigestMethod(DigestMethod.SHA1, null), envelopedTransform, null, null);
// Create the SignatureMethod based on the type of key
final SignatureMethod signatureMethod;
final String algorithm = pubKey.getAlgorithm();
switch (algorithm) {
case "DSA":
signatureMethod = sigFactory.newSignatureMethod(SignatureMethod.DSA_SHA1, null);
break;
case "RSA":
signatureMethod = sigFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
break;
default:
throw new RuntimeException("Error signing SAML element: Unsupported type of key");
}
final CanonicalizationMethod canonicalizationMethod = sigFactory
.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
(C14NMethodParameterSpec) null);
// Create the SignedInfo
final SignedInfo signedInfo = sigFactory.newSignedInfo(
canonicalizationMethod, signatureMethod, Collections.singletonList(ref));
// Create a KeyValue containing the DSA or RSA PublicKey
final KeyInfoFactory keyInfoFactory = sigFactory.getKeyInfoFactory();
final KeyValue keyValuePair = keyInfoFactory.newKeyValue(pubKey);
// Create a KeyInfo and add the KeyValue to it
final KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValuePair));
// Convert the JDOM document to w3c (Java XML signature API requires w3c representation)
final Element w3cElement = toDom(element);
// Create a DOMSignContext and specify the DSA/RSA PrivateKey and
// location of the resulting XMLSignature's parent element
final DOMSignContext dsc = new DOMSignContext(privKey, w3cElement);
final Node xmlSigInsertionPoint = getXmlSignatureInsertLocation(w3cElement);
dsc.setNextSibling(xmlSigInsertionPoint);
// Marshal, generate (and sign) the enveloped signature
final XMLSignature signature = sigFactory.newXMLSignature(signedInfo, keyInfo);
signature.sign(dsc);
return toJdom(w3cElement);
} catch (final Exception e) {
throw new RuntimeException("Error signing SAML element: " + e.getMessage(), e);
}
}
示例8: RequestSigner
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
public RequestSigner ( final Configuration configuration ) throws Exception
{
this.fac = XMLSignatureFactory.getInstance ( "DOM" );
this.md = this.fac.newDigestMethod ( configuration.getDigestMethod (), null );
this.kif = this.fac.getKeyInfoFactory ();
this.t = this.fac.newTransform ( Transform.ENVELOPED, (TransformParameterSpec)null );
this.ref = this.fac.newReference ( "", this.md, Collections.singletonList ( this.t ), null, null );
this.cm = this.fac.newCanonicalizationMethod ( CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null );
}
示例9: sign
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
public Document sign(FileInputStream fileStream, KeyPair keyPair)
throws ParserConfigurationException, SAXException, IOException,
NoSuchAlgorithmException, InvalidAlgorithmParameterException,
KeyException, MarshalException, XMLSignatureException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(fileStream);
DOMSignContext signContext = new DOMSignContext(keyPair.getPrivate(),
document.getDocumentElement());
XMLSignatureFactory signFactory = XMLSignatureFactory
.getInstance("DOM");
Reference ref = signFactory.newReference("", signFactory
.newDigestMethod(digestMethod, null), Collections
.singletonList(signFactory.newTransform(Transform.ENVELOPED,
(TransformParameterSpec) null)), null, null);
SignedInfo si = signFactory.newSignedInfo(signFactory
.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
(C14NMethodParameterSpec) null), signFactory
.newSignatureMethod(signatureMethod, null), Collections
.singletonList(ref));
KeyInfoFactory kif = signFactory.getKeyInfoFactory();
KeyValue kv = kif.newKeyValue(keyPair.getPublic());
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
XMLSignature signature = signFactory.newXMLSignature(si, ki);
signature.sign(signContext);
return document;
}
示例10: HMACSignatureAlgorithmTest
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
public HMACSignatureAlgorithmTest() throws Exception {
//
// If the BouncyCastle provider is not installed, then try to load it
// via reflection.
//
if (Security.getProvider("BC") == null) {
Constructor<?> cons = null;
try {
Class<?> c = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
cons = c.getConstructor(new Class[] {});
} catch (Exception e) {
//ignore
}
if (cons != null) {
Provider provider = (Provider)cons.newInstance();
Security.insertProviderAt(provider, 2);
bcInstalled = true;
}
}
db = XMLUtils.createDocumentBuilder(false);
// create common objects
fac = XMLSignatureFactory.getInstance("DOM", new org.apache.jcp.xml.dsig.internal.dom.XMLDSigRI());
withoutComments = fac.newCanonicalizationMethod
(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null);
// Digest Methods
sha1 = fac.newDigestMethod(DigestMethod.SHA1, null);
hmacSha1 = fac.newSignatureMethod("http://www.w3.org/2000/09/xmldsig#hmac-sha1", null);
hmacSha224 = fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#hmac-sha224", null);
hmacSha256 = fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", null);
hmacSha384 = fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#hmac-sha384", null);
hmacSha512 = fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#hmac-sha512", null);
ripemd160 = fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160", null);
sks = new KeySelectors.SecretKeySelector("testkey".getBytes("ASCII"));
}
示例11: testProviderMultipleLoaders
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
@SuppressWarnings("resource")
@org.junit.Test
public void testProviderMultipleLoaders() throws Exception {
String baseDir = System.getProperty("basedir");
String fs = System.getProperty("file.separator");
File file0 = new File(baseDir + fs + "build" + fs + "classes" + fs);
File file1 = new File(baseDir + fs + "build" + fs + "test" + fs);
URL[] urls = new URL[2];
urls[0] = file0.toURI().toURL();
urls[1] = file1.toURI().toURL();
URLClassLoader uc1 = new URLClassLoader
(urls, Thread.currentThread().getContextClassLoader());
//load security provider using current class loader
final Provider provider = new XMLDSigRI();
AccessController.doPrivileged(new java.security.PrivilegedAction<Object>() {
public Object run() {
Security.addProvider(provider);
return null;
}
});
// get the provider from java.security.Security using URLClassLoader.
// Need to use introspection to invoke methods to avoid using the
// current class loader
String factoryName = "javax.xml.crypto.dsig.XMLSignatureFactory";
Class<?> factoryClass = uc1.loadClass(factoryName);
Method factoryMethod = factoryClass.getDeclaredMethod
("getInstance", new Class[]{String.class});
Class<?> methodParameterClass = uc1.loadClass
("javax.xml.crypto.dsig.spec.C14NMethodParameterSpec");
Method canonicalizationMethod = factoryClass.getDeclaredMethod
("newCanonicalizationMethod",
new Class[]{String.class,methodParameterClass});
Object factory = factoryMethod.invoke(null, "DOM");
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
canonicalizationMethod.invoke
(factory, new Object[]{CanonicalizationMethod.EXCLUSIVE,null});
}
long end = System.currentTimeMillis();
long elapsed = end-start;
if (log.isDebugEnabled()) {
log.debug("Elapsed: " + elapsed);
}
}
示例12: dsig
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
public void dsig() throws Exception {
XMLSignatureFactory fac = XMLSignatureFactory.getInstance
("DOM", new org.apache.jcp.xml.dsig.internal.dom.XMLDSigRI());
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
fac.newCanonicalizationMethod
(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null);
}
long end = System.currentTimeMillis();
long elapsed = end - start;
if (log.isDebugEnabled()) {
log.debug("Elapsed: " + elapsed);
log.debug("dsig succeeded");
}
}
示例13: assinarDocumento
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
public String assinarDocumento(final String conteudoXml) throws Exception {
final KeyStore keyStore = KeyStore.getInstance("PKCS12");
try (InputStream certificadoStream = new ByteArrayInputStream(this.config.getCertificado())) {
keyStore.load(certificadoStream, this.config.getCertificadoSenha().toCharArray());
}
final KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(keyStore.aliases().nextElement(), new KeyStore.PasswordProtection(this.config.getCertificadoSenha().toCharArray()));
final XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM");
final List<Transform> transforms = new ArrayList<>(2);
transforms.add(signatureFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null));
transforms.add(signatureFactory.newTransform(AssinaturaDigital.C14N_TRANSFORM_METHOD, (TransformParameterSpec) null));
final KeyInfoFactory keyInfoFactory = signatureFactory.getKeyInfoFactory();
final X509Data x509Data = keyInfoFactory.newX509Data(Collections.singletonList((X509Certificate) keyEntry.getCertificate()));
final KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(x509Data));
final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
try (StringReader stringReader = new StringReader(conteudoXml)) {
final Document document = documentBuilderFactory.newDocumentBuilder().parse(new InputSource(stringReader));
for (final String elementoAssinavel : AssinaturaDigital.ELEMENTOS_ASSINAVEIS) {
final NodeList elements = document.getElementsByTagName(elementoAssinavel);
for (int i = 0; i < elements.getLength(); i++) {
final Element element = (Element) elements.item(i);
final String id = element.getAttribute("Id");
element.setIdAttribute("Id", true);
final Reference reference = signatureFactory.newReference("#" + id, signatureFactory.newDigestMethod(DigestMethod.SHA1, null), transforms, null, null);
final SignedInfo signedInfo = signatureFactory.newSignedInfo(signatureFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), signatureFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(reference));
final XMLSignature signature = signatureFactory.newXMLSignature(signedInfo, keyInfo);
signature.sign(new DOMSignContext(keyEntry.getPrivateKey(), element.getParentNode()));
}
}
return this.converteDocumentParaXml(document);
}
}
示例14: createKeyInfoReference
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
protected Reference createKeyInfoReference(XMLSignatureFactory fac, String keyInfoId, String digestAlgorithm) throws Exception { //NOPMD
if (keyInfoId == null) {
return null;
}
if (getConfiguration().getAddKeyInfoReference() == null) {
return null;
}
if (!getConfiguration().getAddKeyInfoReference()) {
return null;
}
LOG.debug("Creating reference to key info element with Id: {}", keyInfoId);
List<Transform> transforms = new ArrayList<Transform>(1);
Transform transform = fac.newTransform(CanonicalizationMethod.INCLUSIVE, (TransformParameterSpec) null);
transforms.add(transform);
return fac.newReference("#" + keyInfoId, fac.newDigestMethod(digestAlgorithm, null), transforms, null, null);
}
示例15: getTransformsXPath2
import javax.xml.crypto.dsig.CanonicalizationMethod; //导入依赖的package包/类
private List<AlgorithmMethod> getTransformsXPath2() {
List<XPathAndFilter> list = new ArrayList<XPathAndFilter>(3);
XPathAndFilter xpath1 = new XPathAndFilter("//n0:ToBeSigned", XPathType.Filter.INTERSECT.toString());
list.add(xpath1);
XPathAndFilter xpath2 = new XPathAndFilter("//n0:NotToBeSigned", XPathType.Filter.SUBTRACT.toString());
list.add(xpath2);
XPathAndFilter xpath3 = new XPathAndFilter("//n0:ReallyToBeSigned", XPathType.Filter.UNION.toString());
list.add(xpath3);
List<AlgorithmMethod> result = new ArrayList<AlgorithmMethod>(2);
result.add(XmlSignatureHelper.getCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE));
result.add(XmlSignatureHelper.getXPath2Transform(list, getNamespaceMap()));
return result;
}