當前位置: 首頁>>代碼示例>>Java>>正文


Java DOMSignContext類代碼示例

本文整理匯總了Java中javax.xml.crypto.dsig.dom.DOMSignContext的典型用法代碼示例。如果您正苦於以下問題:Java DOMSignContext類的具體用法?Java DOMSignContext怎麽用?Java DOMSignContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


DOMSignContext類屬於javax.xml.crypto.dsig.dom包,在下文中一共展示了DOMSignContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: test_create_signature_with_empty_id

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
static void test_create_signature_with_empty_id() throws Exception {
    System.out.println("* Generating signature-with-empty-id.xml");

    // create references
    List<Reference> refs = Collections.singletonList
        (fac.newReference("#", sha1));

    // create SignedInfo
    SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs);

    // create object with empty id
    Document doc = db.newDocument();
    XMLObject obj = fac.newXMLObject(Collections.singletonList
        (new DOMStructure(doc.createTextNode("I am the text."))),
        "", "text/plain", null);

    // create XMLSignature
    XMLSignature sig = fac.newXMLSignature(si, rsa,
                                           Collections.singletonList(obj),
                                           "signature", null);
    DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc);
    sig.sign(dsc);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:24,代碼來源:GenerationTests.java

示例2: testSignWithEmptyNSPrefix

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
@org.junit.Test
    public void testSignWithEmptyNSPrefix() throws Exception {
        SignedInfo si = createSignedInfo(SIG_METHODS[1]);
        KeyInfo	ki = kifac.newKeyInfo(Collections.singletonList
                    (kifac.newKeyValue((PublicKey) VALIDATE_KEYS[1])));
        XMLSignature sig = fac.newXMLSignature(si, ki, objs, id, sigValueId);
        Document doc = TestUtils.newDocument();
        XMLSignContext signContext = new DOMSignContext(SIGN_KEYS[1], doc);
        signContext.putNamespacePrefix(XMLSignature.XMLNS, "");
        signContext.setURIDereferencer(ud);
        sig.sign(signContext);
        TestUtils.validateSecurityOrEncryptionElement(doc.getDocumentElement());
/*
        StringWriter sw = new StringWriter();
        dumpDocument(doc, sw);
        System.out.println(sw);
*/
    }
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:19,代碼來源:XMLSignatureTest.java

示例3: testCreateDSA2048Signature

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
@org.junit.Test
public void testCreateDSA2048Signature() throws Exception {

    // check if SHA256withDSA is supported
    boolean gotSHA256withDSA = false;
    try {
        Signature.getInstance("SHA256withDSA");
        gotSHA256withDSA = true;
    } catch (NoSuchAlgorithmException e) {}
    org.junit.Assume.assumeTrue(gotSHA256withDSA);

    SignatureMethod sm = fac.newSignatureMethod(DSA_SHA256, null);
    SignedInfo si = createSignedInfo(sm);
    KeyInfo ki = kifac.newKeyInfo(Collections.singletonList
        (kifac.newKeyValue((PublicKey)TestUtils.getPublicKey("DSA", 2048))));
    XMLSignature sig = fac.newXMLSignature(si, ki, objs, id, sigValueId);
    Document doc = TestUtils.newDocument();
    XMLSignContext signContext =
        new DOMSignContext(TestUtils.getPrivateKey("DSA", 2048), doc);
    signContext.setURIDereferencer(ud);
    sig.sign(signContext);
    XMLValidateContext validateContext = new DOMValidateContext
        (TestUtils.getPublicKey("DSA", 2048), doc.getDocumentElement());
    validateContext.setURIDereferencer(ud);
    assertTrue(sig.validate(validateContext));
}
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:27,代碼來源:XMLSignatureTest.java

示例4: sign

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的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);
	}
}
 
開發者ID:yanaga,項目名稱:opes,代碼行數:39,代碼來源:CertificadoDigital.java

示例5: sign

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
public synchronized void sign()
		throws MarshalException,
		XMLSignatureException,
		KeyException {

	if (this.document == null)
		throw new RuntimeException("Can't sign a NULL document");

	Reference reference = this.signatureFactory.newReference(
			referenceUri,
			this.digestMethod,
			this.transformList,
			null,
			null);

	SignedInfo signedInfo = this.signatureFactory.newSignedInfo(
			this.canonicalizationMethod,
			this.signatureMethod,
			Collections.singletonList(reference));

	// Create the KeyInfo containing the X509Data.
	X509Data xd = this.keyInfoFactory.newX509Data(
			Collections.singletonList(this.certificateWithKey.certificate));

	KeyInfo keyInfo = this.keyInfoFactory.newKeyInfo(Collections.singletonList(xd));

	XMLSignature signature = this.signatureFactory.newXMLSignature(
			signedInfo,
			keyInfo);

	DOMSignContext signingContext = new DOMSignContext(
			this.certificateWithKey.privateKey,
			document.getDocumentElement());

	signature.sign(signingContext);
}
 
開發者ID:EixoX,項目名稱:jetfuel,代碼行數:37,代碼來源:XmlSignatureHandler.java

示例6: signSamlElement

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的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);
    }
}
 
開發者ID:mrluo735,項目名稱:cas-5.1.0,代碼行數:71,代碼來源:AbstractSamlObjectBuilder.java

示例7: sign

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
synchronized void sign ( final Key privateKey, final PublicKey publicKey, final Certificate cert, final Document doc ) throws Exception
{
    final DOMSignContext dsc = new DOMSignContext ( privateKey, doc.getDocumentElement () );

    final SignatureMethod sm = this.fac.newSignatureMethod ( fromAlg ( privateKey.getAlgorithm () ), null );

    final SignedInfo si = this.fac.newSignedInfo ( this.cm, sm, Collections.singletonList ( this.ref ) );

    final List<Object> data = new LinkedList<Object> ();

    if ( cert != null )
    {
        data.add ( this.kif.newKeyValue ( cert.getPublicKey () ) );
        data.add ( this.kif.newX509Data ( Collections.singletonList ( cert ) ) );
    }
    else
    {
        data.add ( this.kif.newKeyValue ( publicKey ) );
    }

    final KeyInfo ki = this.kif.newKeyInfo ( data );

    final XMLSignature signature = this.fac.newXMLSignature ( si, ki );

    // finally sign
    signature.sign ( dsc );
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:28,代碼來源:RequestSigner.java

示例8: sign

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的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;
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:36,代碼來源:XMLSignatureBuilder.java

示例9: internalMarshal

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
private void internalMarshal(javax.xml.crypto.dom.DOMStructure parent, XMLCryptoContext context)
        throws MarshalException {
    Node pNode = parent.getNode();
    String dsPrefix = DOMUtils.getSignaturePrefix(context);

    Node nextSibling = null;
    if (context instanceof DOMSignContext) {
        nextSibling = ((DOMSignContext)context).getNextSibling();
    }

    XmlWriterToTree xwriter = new XmlWriterToTree(Marshaller.getMarshallers(), pNode, nextSibling);
    marshalInternal(xwriter, this, dsPrefix, context, true);
}
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:14,代碼來源:DOMKeyInfo.java

示例10: testSignTemplateWithObjectNSDefs

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
@org.junit.Test
public void testSignTemplateWithObjectNSDefs() throws Exception {
    String base = System.getProperty("basedir") == null ? "./"
                  : System.getProperty("basedir");

    File f = new File(base + "/src/test/resources/javax/xml/crypto/dsig/" +
        "signature-enveloping-rsa-template.xml");

    Document doc = XMLUtils.createDocumentBuilder(false).parse(new FileInputStream(f));

    // Find Signature element
    NodeList nl =
        doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
    if (nl.getLength() == 0) {
        throw new Exception("Cannot find Signature element");
    }
    DOMStructure domSignature = new DOMStructure(nl.item(0));
    // unmarshal the XMLSignature
    XMLSignature signature = fac.unmarshalXMLSignature(domSignature);

    // create copy of Signature
    XMLSignature newSignature = fac.newXMLSignature
        (signature.getSignedInfo(), null, signature.getObjects(),
         signature.getId(), signature.getSignatureValue().getId());

    // Sign the template
    Node parent = domSignature.getNode().getParentNode();
    DOMSignContext signContext = new DOMSignContext(SIGN_KEYS[0], parent);
    // remove the signature node (since it will get recreated)
    parent.removeChild(domSignature.getNode());
    newSignature.sign(signContext);
    TestUtils.validateSecurityOrEncryptionElement(parent.getLastChild());

    // check that Object element retained namespace definitions
    Element objElem = (Element)parent.getFirstChild().getLastChild();
    Attr a = objElem.getAttributeNode("xmlns:test");
    if (!a.getValue().equals("http://www.example.org/ns")) {
        throw new Exception("Object namespace definition not retained");
    }
}
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:41,代碼來源:XMLSignatureTest.java

示例11: testCreateSignatureWithEmptyId

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
@org.junit.Test
public void testCreateSignatureWithEmptyId() throws Exception {
    // create references
    DigestMethod dm = fac.newDigestMethod(DigestMethod.SHA1, null);
    List<Reference> refs = Collections.singletonList
        (fac.newReference("#", dm));

    // create SignedInfo
    CanonicalizationMethod cm = fac.newCanonicalizationMethod
        (CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null);
    SignedInfo si = fac.newSignedInfo(cm, SIG_METHODS[1], refs);

    // create object with empty id
    Document doc = TestUtils.newDocument();
    XMLObject obj = fac.newXMLObject(Collections.singletonList
        (new DOMStructure(doc.createTextNode("I am the text."))),
        "", "text/plain", null);

    KeyInfo	ki = kifac.newKeyInfo(Collections.singletonList
                (kifac.newKeyValue((PublicKey) VALIDATE_KEYS[1])));

    // create XMLSignature
    XMLSignature sig = fac.newXMLSignature(si, ki,
                                           Collections.singletonList(obj),
                                           "signature", null);
    DOMSignContext dsc = new DOMSignContext(SIGN_KEYS[1], doc);
    sig.sign(dsc);
}
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:29,代碼來源:XMLSignatureTest.java

示例12: test_create_signature_enveloping

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
private void test_create_signature_enveloping(
    SignatureMethod sm, DigestMethod dm, KeyInfo ki, Key signingKey, KeySelector ks
) throws Exception {

    // create reference
    Reference ref = fac.newReference("#DSig.Object_1", dm, null,
                                     XMLObject.TYPE, null);

    // create SignedInfo
    SignedInfo si = fac.newSignedInfo(withoutComments, sm,
                                      Collections.singletonList(ref));

    Document doc = db.newDocument();
    // create Objects
    Element webElem = doc.createElementNS(null, "Web");
    Text text = doc.createTextNode("up up and away");
    webElem.appendChild(text);
    XMLObject obj = fac.newXMLObject(Collections.singletonList
                                     (new DOMStructure(webElem)), "DSig.Object_1", "text/xml", null);

    // create XMLSignature
    XMLSignature sig = fac.newXMLSignature
    (si, ki, Collections.singletonList(obj), null, null);

    DOMSignContext dsc = new DOMSignContext(signingKey, doc);
    dsc.setDefaultNamespacePrefix("dsig");

    sig.sign(dsc);
    TestUtils.validateSecurityOrEncryptionElement(doc.getDocumentElement());

    // XMLUtils.outputDOM(doc.getDocumentElement(), System.out);

    DOMValidateContext dvc = new DOMValidateContext
    (ks, doc.getDocumentElement());
    XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);

    assertTrue(sig.equals(sig2));
    assertTrue(sig2.validate(dvc));
}
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:40,代碼來源:HMACSignatureAlgorithmTest.java

示例13: test_create_signature_enveloped_dsa

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
@org.junit.Test
public void test_create_signature_enveloped_dsa() throws Exception {
    // create SignedInfo
    SignedInfo si = fac.newSignedInfo
        (withoutComments, dsaSha1, Collections.singletonList
            (fac.newReference
                ("", sha1, Collections.singletonList
                    (fac.newTransform(Transform.ENVELOPED,
                     (TransformParameterSpec) null)),
             null, null)));

    // create XMLSignature
    XMLSignature sig = fac.newXMLSignature(si, dsa);

    Document doc = db.newDocument();
    Element envelope = doc.createElementNS
        ("http://example.org/envelope", "Envelope");
    envelope.setAttributeNS
        (Constants.NamespaceSpecNS, "xmlns", "http://example.org/envelope");
    doc.appendChild(envelope);

    DOMSignContext dsc = new DOMSignContext(signingKey, envelope);

    sig.sign(dsc);
    TestUtils.validateSecurityOrEncryptionElement(envelope.getFirstChild());

    DOMValidateContext dvc = new DOMValidateContext
        (kvks, envelope.getFirstChild());
    XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);

    assertTrue(sig.equals(sig2));

    assertTrue(sig2.validate(dvc));
}
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:35,代碼來源:CreateBaltimore23Test.java

示例14: if

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
private void test_create_signature_enveloping
    (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks,
    boolean b64) throws Exception {

    // create reference
    Reference ref;
    if (b64) {
        ref = fac.newReference("#object", sha1, Collections.singletonList
            (fac.newTransform(Transform.BASE64,
             (TransformParameterSpec) null)), null, null);
    } else {
        ref = fac.newReference("#object", sha1);
    }

    // create SignedInfo
    SignedInfo si = fac.newSignedInfo(withoutComments, sm,
        Collections.singletonList(ref));

    Document doc = db.newDocument();
    // create Objects
    XMLObject obj = fac.newXMLObject(Collections.singletonList
        (new DOMStructure(doc.createTextNode("some text"))),
        "object", null, null);

    // create XMLSignature
    XMLSignature sig = fac.newXMLSignature
        (si, ki, Collections.singletonList(obj), null, null);

    DOMSignContext dsc = new DOMSignContext(signingKey, doc);

    sig.sign(dsc);
    TestUtils.validateSecurityOrEncryptionElement(doc.getDocumentElement());

    DOMValidateContext dvc = new DOMValidateContext
        (ks, doc.getDocumentElement());
    XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);

    assertTrue(sig.equals(sig2));
    assertTrue(sig2.validate(dvc));
}
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:41,代碼來源:CreateBaltimore23Test.java

示例15: XMLSignContextTest

import javax.xml.crypto.dsig.dom.DOMSignContext; //導入依賴的package包/類
public XMLSignContextTest() throws Exception {
    // set up the signingKeys
    KEYS = new Key[3];
    KEYS[0] = TestUtils.getPrivateKey("DSA");
    KEYS[1] = TestUtils.getPrivateKey("RSA");
    KEYS[2] = new SecretKeySpec(new byte[16], "HmacSHA1");
    // set up the default XMLSignContext
    SecretKey sk = new SecretKeySpec(new byte[8], "DES");
    doc = TestUtils.newDocument();
    defContext = new DOMSignContext(sk, doc);
}
 
開發者ID:Legostaev,項目名稱:xmlsec-gost,代碼行數:12,代碼來源:XMLSignContextTest.java


注:本文中的javax.xml.crypto.dsig.dom.DOMSignContext類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。