Example usage for java.security SignatureException SignatureException

List of usage examples for java.security SignatureException SignatureException

Introduction

In this page you can find the example usage for java.security SignatureException SignatureException.

Prototype

public SignatureException(String message, Throwable cause) 

Source Link

Document

Creates a SignatureException with the specified detail message and cause.

Usage

From source file:Main.java

public static boolean validateDocumentByKey(final Document document, Key validatingKey)
        throws SignatureException {
    final DOMValidateContext valContext = new DOMValidateContext(validatingKey,
            getSignatureNode(document.getDocumentElement()));
    try {/*from w w w.  j  a v  a2  s .co m*/
        final XMLSignature signature = getXMLSignatureFactory().unmarshalXMLSignature(valContext);
        return signature.validate(valContext);
    } catch (final Exception e) {
        throw new SignatureException("Signature verification error", e);
    }
}

From source file:Main.java

private static SignedInfo createSignedInfo(final String algorithm,
        final SignatureMethodParameterSpec methodParamSpec, final String signatureId, final String referenceUri)
        throws SignatureException {
    try {/*from w  w w.  ja v  a 2 s .c  o  m*/
        final CanonicalizationMethod canonicalizationMethod = getXMLSignatureFactory()
                .newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (XMLStructure) null);
        final SignatureMethod signatureMethod = getXMLSignatureFactory().newSignatureMethod(algorithm,
                methodParamSpec);
        return getXMLSignatureFactory().newSignedInfo(canonicalizationMethod, signatureMethod,
                Collections.singletonList(createReference(referenceUri)), signatureId);
    } catch (final Exception e) {
        throw new SignatureException("Error creating signed info", e);
    }
}

From source file:Main.java

private static Reference createReference(final String uri) throws SignatureException {
    try {//from w w w .  j a  v  a2 s . co m
        final List<Transform> transforms = new ArrayList<>();
        transforms
                .add(getXMLSignatureFactory().newTransform(Transform.ENVELOPED, (TransformParameterSpec) null));
        transforms.add(getXMLSignatureFactory().newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE,
                (XMLStructure) null));
        final DigestMethod digestMethod = getXMLSignatureFactory().newDigestMethod(DigestMethod.SHA1, null);
        String referenceUri = "";
        if (uri != null) {
            referenceUri = uri;
        }
        return getXMLSignatureFactory().newReference(referenceUri, digestMethod, transforms, null, null);
    } catch (final Exception e) {
        throw new SignatureException("Error creating reference", e);
    }
}

From source file:org.sakaiproject.nakamura.util.Signature.java

public static String calculateRFC2104HMACWithEncoding(String data, String key, boolean urlSafe)
        throws java.security.SignatureException {
    if (data == null) {
        throw new IllegalArgumentException("String data == null");
    }//from w ww  .  ja v a2 s  .  co m
    if (key == null) {
        throw new IllegalArgumentException("String key == null");
    }
    try {
        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = key.getBytes("UTF-8");
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1_ALGORITHM);

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));

        // Convert raw bytes to encoding
        byte[] base64Bytes = Base64.encodeBase64(rawHmac, false, urlSafe);
        String result = new String(base64Bytes, "UTF-8");

        return result;

    } catch (NoSuchAlgorithmException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage(), e);
    }
}

From source file:signature.common.signature.SignatureCalculator.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 * /* w  w w .j a  va2s .c o m*/
 * @param data
 *            The data to be signed.
 * @param key
 *            The signing key, a.k.a. the AWS secret key.
 * @return The base64-encoded RFC 2104-compliant HMAC signature.
 * @throws java.security.SignatureException
 *             when signature generation fails
 */
public byte[] calculateRFC2104HMAC(byte[] data, String key) throws SignatureException {
    byte[] result = null;

    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        mac.update(data);
        byte[] rawHmac = mac.doFinal();

        // base64-encode the hmac
        result = Base64.encodeBase64(rawHmac);
    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC: ", e);
    }

    return result;
}

From source file:org.mule.module.fws.api.PortProvider.java

private String sign(String data, String key) throws SignatureException {
    byte[] signature;
    try {/*from   w ww  .j av a2 s.co m*/
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(key.getBytes(), "HmacSHA1"));
        signature = Base64.encodeBase64(mac.doFinal(data.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate signature: " + e.getMessage(), e);
    }
    return new String(signature);
}

From source file:org.apache.hadoop.mapred.PriorityAuthorization.java

/**
 * Adapted from AWS Query Authentication cookbook:
 * Computes RFC 2104-compliant HMAC signature.
 *
 * @param data//from  w  ww. j  a  v a  2s.co m
 *     The data to be signed.
 * @param key
 *     The signing key.
 * @return
 *     The base64-encoded RFC 2104-compliant HMAC signature.
 * @throws
 *     java.security.SignatureException when signature generation fails
 */
public static String hmac(String data, String key) throws java.security.SignatureException {
    String result;
    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());

        // base64-encode the hmac
        result = new String(Base64.encodeBase64(rawHmac));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e, e);
    }
    return result;
}

From source file:org.teatrove.teaapps.contexts.CryptoContext.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 * //from  w  w  w  .j  ava2 s. c o m
 * @param data The data to be signed.
 * @param key The signing key.
 * 
 * @return The Base64-encoded RFC 2104-compliant HMAC signature.
 * 
 * @throws java.security.SignatureException when signature generation fails
 */
public String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException {

    String result;
    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());

        // base64-encode the hmac
        result = new String(Base64.encodeBase64(rawHmac));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC", e);
    }

    return result;
}

From source file:org.innobuilt.fincayra.fps.SignatureUtils.java

public static String sign(String data, String key, String signatureMethod) throws SignatureException {
    String signature = "";
    try {/* w w  w  . j  a va2 s.c  om*/
        Mac mac = Mac.getInstance(signatureMethod);
        mac.init(new SecretKeySpec(key.getBytes(), signatureMethod));
        signature = new String(Base64.encodeBase64(mac.doFinal(data.getBytes(UTF_8_Encoding))));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate signature: " + e.getMessage(), e);
    }
    return signature;
}

From source file:org.panbox.core.crypto.CryptCore.java

public static boolean verifySignature(Signable s, byte[] signature, PublicKey key) throws SignatureException {
    try {//  w w w. ja va2s.  c om
        return SignatureHelper.verify(s, signature, key);
    } catch (Exception e) {
        throw new SignatureException("Could not verify signature", e);
    }
}