Example usage for javax.xml.crypto.dsig SignatureMethod HMAC_SHA1

List of usage examples for javax.xml.crypto.dsig SignatureMethod HMAC_SHA1

Introduction

In this page you can find the example usage for javax.xml.crypto.dsig SignatureMethod HMAC_SHA1.

Prototype

String HMAC_SHA1

To view the source code for javax.xml.crypto.dsig SignatureMethod HMAC_SHA1.

Click Source Link

Document

The <a href="http://www.w3.org/2000/09/xmldsig#hmac-sha1">HMAC-SHA1</a> MAC signature method algorithm URI

Usage

From source file:wssec.TestWSSecurityNew17.java

/**
 * Test signing a message body using a symmetric key with EncryptedKeySHA1
 *///from w w  w .  ja va 2  s.co m
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);
}

From source file:wssec.TestWSSecurityNew17.java

/**
 * Test signing a message body using a symmetric key with Direct Reference to an
 * EncryptedKey//from  w w w.  java 2  s. com
 */
public void testSymmetricSignatureDR() throws Exception {
    SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
    Document doc = unsignedEnvelope.getAsDocument();

    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    WSSecEncryptedKey encrKey = new WSSecEncryptedKey();
    encrKey.setKeyIdentifierType(WSConstants.ISSUER_SERIAL);
    encrKey.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
    encrKey.setKeySize(192);
    encrKey.prepare(doc, crypto);

    WSSecSignature sign = new WSSecSignature();
    sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
    sign.setCustomTokenId(encrKey.getId());
    sign.setSecretKey(encrKey.getEphemeralKey());
    sign.setSignatureAlgorithm(SignatureMethod.HMAC_SHA1);
    sign.setCustomTokenValueType(WSConstants.SOAPMESSAGE_NS11 + "#" + WSConstants.ENC_KEY_VALUE_TYPE);

    Document signedDoc = sign.build(doc, crypto, secHeader);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Signed symmetric message DR:");
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
        LOG.debug(outputString);
    }
}

From source file:wssec.TestWSSecurityNew17.java

/**
 * Test signing a message body using a symmetric key with EncryptedKeySHA1. 
 * The request is generated using WSHandler, instead of coding it.
 *//*from  w ww  .  j  ava  2  s .co m*/
public void testSymmetricSignatureSHA1Handler() throws Exception {
    final WSSConfig cfg = WSSConfig.getNewInstance();
    RequestData reqData = new RequestData();
    reqData.setWssConfig(cfg);
    java.util.Map messageContext = new java.util.TreeMap();
    messageContext.put(WSHandlerConstants.SIG_KEY_ID, "EncryptedKeySHA1");
    messageContext.put(WSHandlerConstants.SIG_ALGO, SignatureMethod.HMAC_SHA1);
    messageContext.put(WSHandlerConstants.PW_CALLBACK_REF, this);
    reqData.setMsgContext(messageContext);
    reqData.setUsername("");

    final java.util.Vector actions = new java.util.Vector();
    actions.add(new Integer(WSConstants.SIGN));

    SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
    Document doc = unsignedEnvelope.getAsDocument();
    MyHandler handler = new MyHandler();
    handler.send(WSConstants.SIGN, doc, reqData, actions, true);

    String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
    if (LOG.isDebugEnabled()) {
        LOG.debug(outputString);
    }

    reqData = new RequestData();
    reqData.setWssConfig(WSSConfig.getNewInstance());
    messageContext = new java.util.TreeMap();
    messageContext.put(WSHandlerConstants.PW_CALLBACK_REF, this);
    reqData.setMsgContext(messageContext);
    reqData.setUsername("");

    handler.receive(WSConstants.SIGN, reqData);

    verify(doc);
}

From source file:wssec.TestWSSecurityNewSCT.java

/**
 * Test signature and verification using a SecurityContextToken directly,
 * rather than using a DerivedKeyToken to point to a SecurityContextToken.
 * See WSS-216 - https://issues.apache.org/jira/browse/WSS-216
 *///  www.j  a  v a 2s  .  com
public void testSCTSign() {
    try {
        SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
        Document doc = unsignedEnvelope.getAsDocument();
        WSSecHeader secHeader = new WSSecHeader();
        secHeader.insertSecurityHeader(doc);

        WSSecSecurityContextToken sctBuilder = new WSSecSecurityContextToken();
        sctBuilder.prepare(doc, crypto);

        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte[] tempSecret = new byte[16];
        random.nextBytes(tempSecret);

        // Store the secret
        this.secrets.put(sctBuilder.getIdentifier(), tempSecret);

        String tokenId = sctBuilder.getSctId();

        WSSecSignature builder = new WSSecSignature();
        builder.setSecretKey(tempSecret);
        builder.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
        builder.setCustomTokenValueType(WSConstants.WSC_SCT);
        builder.setCustomTokenId(tokenId);
        builder.setSignatureAlgorithm(SignatureMethod.HMAC_SHA1);
        builder.build(doc, crypto, secHeader);

        sctBuilder.prependSCTElementToHeader(doc, secHeader);

        if (LOG.isDebugEnabled()) {
            LOG.debug("SCT sign");
            String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
            LOG.debug(outputString);
        }

        verify(doc);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}