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


Java SignatureMethod類代碼示例

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


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

示例1: algEquals

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
static boolean algEquals ( final String algURI, final String algName )
{
    if ( algName.equalsIgnoreCase ( "DSA" ) && algURI.equalsIgnoreCase ( SignatureMethod.DSA_SHA1 ) )
    {
        return true;
    }
    else if ( algName.equalsIgnoreCase ( "RSA" ) && algURI.equalsIgnoreCase ( SignatureMethod.RSA_SHA1 ) )
    {
        return true;
    }
    else
    {
        logger.warn ( "Failed to check key - algUri: {}, algName: {}", algURI, algName );
        return false;
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:17,代碼來源:KeyValueKeySelector.java

示例2: algEquals

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
static boolean algEquals ( final String algURI, final String algName )
{
    if ( algName.equalsIgnoreCase ( "DSA" ) && algURI.equalsIgnoreCase ( SignatureMethod.DSA_SHA1 ) )
    {
        return true;
    }
    else if ( algName.equalsIgnoreCase ( "RSA" ) && algURI.equalsIgnoreCase ( SignatureMethod.RSA_SHA1 ) )
    {
        return true;
    }
    else
    {
        logger.trace ( "Failed to check key - algUri: {}, algName: {}", algURI, algName );
        return false;
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:17,代碼來源:X509KeySelector.java

示例3: fromAlg

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
private String fromAlg ( final String alg )
{
    if ( "DSA".equals ( alg ) )
    {
        return SignatureMethod.DSA_SHA1;
    }
    else if ( "RSA".equals ( alg ) )
    {
        return SignatureMethod.RSA_SHA1;
    }
    else if ( "HMAC".equals ( alg ) )
    {
        return SignatureMethod.HMAC_SHA1;
    }
    else
    {
        throw new IllegalArgumentException ( String.format ( "Key algorithm '%s' is not supported", alg ) );
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:20,代碼來源:RequestSigner.java

示例4: select_publicKey_exception

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
@Test()
public void select_publicKey_exception() throws Exception {
    // given
    selector = spy(new X509KeySelector(keystore));
    KeyInfo keyinfo = mock(KeyInfo.class);
    ArrayList<XMLStructure> list = new ArrayList<XMLStructure>();
    X509Data x509Data = mock(X509Data.class);
    list.add(x509Data);
    doReturn(list).when(keyinfo).getContent();
    ArrayList<Object> x509DataContent = new ArrayList<Object>();
    x509DataContent.add(mock(X509Certificate.class));
    doReturn(x509DataContent).when(x509Data).getContent();
    doThrow(new KeyStoreException("key exception")).when(selector)
            .getPublicKeyFromKeystore(any(X509Certificate.class),
                    any(SignatureMethod.class));

    // when
    try {
        selector.select(keyinfo, null, null, null);
        fail();
    } catch (KeySelectorException e) {
        assertTrue(e.getCause().getMessage().contains("key exception"));
    }
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:25,代碼來源:X509KeySelectorTest.java

示例5: getSignatureMethod

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
private String getSignatureMethod(DigestAlgo digestAlgo) {
	if (null == digestAlgo) {
		throw new RuntimeException("digest algo is null");
	}

	switch (digestAlgo) {

	case SHA1:
		return SignatureMethod.RSA_SHA1;
	case SHA256:
		return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256;
	case SHA512:
		return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512;
	}

	// TODO: complete me
	// if ("SHA-384".equals(digestAlgo)) {
	// return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384;
	// }
	// if ("RIPEMD160".equals(digestAlgo)) {
	// return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160;
	// }
	throw new RuntimeException("unsupported sign algo: " + digestAlgo);
}
 
開發者ID:e-Contract,項目名稱:eid-applet,代碼行數:25,代碼來源:AbstractXmlSignatureService.java

示例6: getSignatureMethod

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
private String getSignatureMethod(String digestAlgo) {
	if (null == digestAlgo) {
		throw new RuntimeException("digest algo is null");
	}
	if ("SHA-1".equals(digestAlgo)) {
		return SignatureMethod.RSA_SHA1;
	}
	if ("SHA-256".equals(digestAlgo)) {
		return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256;
	}
	if ("SHA-512".equals(digestAlgo)) {
		return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512;
	}
	if ("SHA-384".equals(digestAlgo)) {
		return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384;
	}
	if ("RIPEMD160".equals(digestAlgo)) {
		return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160;
	}
	throw new RuntimeException("unsupported sign algo: " + digestAlgo);
}
 
開發者ID:e-Contract,項目名稱:eid-applet,代碼行數:22,代碼來源:XmlSignatureServiceBean.java

示例7: validate_dsa_publicKey

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
@Test
public void validate_dsa_publicKey() throws Exception {
    // given
    XMLSignatureBuilder builder = new XMLSignatureBuilder(
            SignatureMethod.DSA_SHA1);
    KeyPair keyPair = generateKeyPair("DSA");
    FileInputStream in = null;
    Document document = null;
    try {
        in = new FileInputStream(FILE_UNSIGNED_ASSERTION);
        document = builder.sign(in, keyPair);
    } finally {
        if (in != null) {
            in.close();
        }
    }
    NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
            "Signature");

    // when
    boolean valid = validator.validate(nl.item(0), keyPair.getPublic());

    // then
    assertTrue(valid);
}
 
開發者ID:servicecatalog,項目名稱:development,代碼行數:26,代碼來源:DigitalSignatureValidatorTest.java

示例8: validate_dsa_keySelector

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
@Test
public void validate_dsa_keySelector() throws Exception {
    // given
    XMLSignatureBuilder builder = new XMLSignatureBuilder(
            SignatureMethod.DSA_SHA1);
    KeyPair keyPair = generateKeyPair("DSA");
    FileInputStream in = null;
    Document document = null;
    try {
        in = new FileInputStream(FILE_UNSIGNED_ASSERTION);
        document = builder.sign(in, keyPair);
    } finally {
        if (in != null) {
            in.close();
        }
    }
    NodeList nl = document.getElementsByTagNameNS(XMLSignature.XMLNS,
            "Signature");

    // when
    boolean valid = validator.validate(nl.item(0));

    // then
    assertTrue(valid);
}
 
開發者ID:servicecatalog,項目名稱:development,代碼行數:26,代碼來源:DigitalSignatureValidatorTest.java

示例9: sign

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的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

示例10: testSymmetricSignatureSHA1

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
/**
 * Test signing a message body using a symmetric key with EncryptedKeySHA1
 */
public void testSymmetricSignatureSHA1() throws Exception {
    SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
    Document doc = unsignedEnvelope.getAsDocument();
    
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);
    
    WSSecSignature sign = new WSSecSignature();
    sign.setKeyIdentifierType(WSConstants.ENCRYPTED_KEY_SHA1_IDENTIFIER);
    sign.setSecretKey(keyData);
    sign.setSignatureAlgorithm(SignatureMethod.HMAC_SHA1);

    Document signedDoc = sign.build(doc, crypto, secHeader);
    
    if (LOG.isDebugEnabled()) {
        LOG.debug("Signed symmetric message SHA1:");
        String outputString = 
            org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
        LOG.debug(outputString);
    }
    
    verify(signedDoc);
}
 
開發者ID:wso2,項目名稱:wso2-wss4j,代碼行數:27,代碼來源:TestWSSecurityNew17.java

示例11: certSelect

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
/**
 * Searches the specified keystore for a certificate that matches the
 * specified X509Certificate and contains a public key that is compatible
 * with the specified SignatureMethod.
 *
 * @return a KeySelectorResult containing the cert's public key if there
 *         is a match; otherwise null
 */
private KeySelectorResult certSelect(X509Certificate xcert,
                                     SignatureMethod sm) throws KeyStoreException {
    // skip non-signer certs
    boolean[] keyUsage = xcert.getKeyUsage();
    if (!keyUsage[0]) {
        return null;
    }
    String alias = ks.getCertificateAlias(xcert);
    if (alias != null) {
        PublicKey pk = ks.getCertificate(alias).getPublicKey();
        // make sure algorithm is compatible with method
        if (algEquals(sm.getAlgorithm(), pk.getAlgorithm())) {
            return new SimpleKeySelectorResult(pk);
        }
    }
    return null;
}
 
開發者ID:inbloom,項目名稱:secure-data-service,代碼行數:26,代碼來源:X509KeySelector.java

示例12: XmlSignatureHandler

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的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();

}
 
開發者ID:EixoX,項目名稱:jetfuel,代碼行數:28,代碼來源:XmlSignatureHandler.java

示例13: signSamlElement

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的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

示例14: select

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
@Override
public KeySelectorResult select ( final KeyInfo keyInfo, final KeySelector.Purpose purpose, final AlgorithmMethod method, final XMLCryptoContext context ) throws KeySelectorException
{
    if ( keyInfo == null )
    {
        throw new KeySelectorException ( "Null KeyInfo object!" );
    }

    final SignatureMethod sm = (SignatureMethod)method;
    final List<?> list = keyInfo.getContent ();

    for ( int i = 0; i < list.size (); i++ )
    {
        final XMLStructure xmlStructure = (XMLStructure)list.get ( i );
        if ( xmlStructure instanceof KeyValue )
        {
            try
            {
                final PublicKey pk = ( (KeyValue)xmlStructure ).getPublicKey ();
                // make sure algorithm is compatible with method
                if ( algEquals ( sm.getAlgorithm (), pk.getAlgorithm () ) )
                {
                    return new SimpleKeySelectorResult ( pk );
                }
            }
            catch ( final KeyException ke )
            {
                throw new KeySelectorException ( ke );
            }

        }
    }
    throw new KeySelectorException ( "No KeyValue element found!" );
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:35,代碼來源:KeyValueKeySelector.java

示例15: select

import javax.xml.crypto.dsig.SignatureMethod; //導入依賴的package包/類
@Override
public KeySelectorResult select ( final KeyInfo keyInfo, final KeySelector.Purpose purpose, final AlgorithmMethod method, final XMLCryptoContext context ) throws KeySelectorException
{
    if ( keyInfo == null )
    {
        throw new KeySelectorException ( "Null KeyInfo object!" );
    }

    final SignatureMethod sm = (SignatureMethod)method;
    final List<?> list = keyInfo.getContent ();

    for ( final Object l : list )
    {
        final XMLStructure xmlStructure = (XMLStructure)l;
        if ( xmlStructure instanceof X509Data )
        {
            for ( final Object o : ( (X509Data)xmlStructure ).getContent () )
            {
                KeySelectorResult result = null;
                if ( o instanceof X509Certificate )
                {
                    result = findPublicKey ( (X509Certificate)o, sm );
                }

                if ( result != null )
                {
                    return result;
                }
            }
        }
    }
    throw new KeySelectorException ( "No KeyValue element found!" );
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:34,代碼來源:X509KeySelector.java


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