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(Throwable cause) 

Source Link

Document

Creates a SignatureException with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

/**
 *  See above.//from w w  w .  j a  v  a 2  s .  c om
 *  Only supports sigs up to about 252 bytes. See code to fix BER encoding for bigger than that.
 *
 *  @return len bytes
 *  @since 0.8.7, moved to SigUtil in 0.9.9
 */
private static byte[] aSN1ToSigBytes(byte[] asn, int len) throws SignatureException {
    //System.out.println("pre from asn1 len=" + len + "\n" + net.i2p.util.HexDump.dump(asn));
    if (asn[0] != 0x30)
        throw new SignatureException("asn[0] = " + (asn[0] & 0xff));
    // handles total len > 127
    int idx = 2;
    if ((asn[1] & 0x80) != 0)
        idx += asn[1] & 0x7f;
    if (asn[idx] != 0x02)
        throw new SignatureException("asn[2] = " + (asn[idx] & 0xff));
    byte[] rv = new byte[len];
    int sublen = len / 2;
    int rlen = asn[++idx];
    if ((rlen & 0x80) != 0)
        throw new SignatureException("FIXME R length > 127");
    if ((asn[++idx] & 0x80) != 0)
        throw new SignatureException("R is negative");
    if (rlen > sublen + 1)
        throw new SignatureException("R too big " + rlen);
    if (rlen == sublen + 1)
        System.arraycopy(asn, idx + 1, rv, 0, sublen);
    else
        System.arraycopy(asn, idx, rv, sublen - rlen, rlen);
    idx += rlen;
    int slenloc = idx + 1;
    if (asn[idx] != 0x02)
        throw new SignatureException("asn[s] = " + (asn[idx] & 0xff));
    int slen = asn[slenloc];
    if ((slen & 0x80) != 0)
        throw new SignatureException("FIXME S length > 127");
    if ((asn[slenloc + 1] & 0x80) != 0)
        throw new SignatureException("S is negative");
    if (slen > sublen + 1)
        throw new SignatureException("S too big " + slen);
    if (slen == sublen + 1)
        System.arraycopy(asn, slenloc + 2, rv, sublen, sublen);
    else
        System.arraycopy(asn, slenloc + 1, rv, len - slen, slen);
    //System.out.println("post from asn1\n" + net.i2p.util.HexDump.dump(rv));
    return rv;
}

From source file:Main.java

private static Node getSignatureNode(final Element rootElement) throws SignatureException {
    final NodeList nl = rootElement.getElementsByTagNameNS(XMLSignature.XMLNS, NODE_SIGNATURE);
    if (nl.getLength() == 0) {
        throw new SignatureException("Cannot find Signature element");
    }/*from w  w  w  .  java2s .com*/
    return nl.item(0);
}

From source file:Main.java

/***
 * Computes RFC 2104-compliant HMAC signature. This can be used to sign the Amazon S3
 * request urls//from   ww w .ja  v a  2  s. co m
 * 
 * @param data The data to be signed.
 * @param key The signing key.
 * @return The Base64-encoded RFC 2104-compliant HMAC signature.
 * @throws SignatureException when signature generation fails
 */
public static String getHMac(String data, String key) throws SignatureException {

    if (data == null) {
        throw new NullPointerException("Data to be signed cannot be null");
    }

    String result = null;
    try {

        final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

        // 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 &
        // initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

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

        if (digest != null) {
            // Base 64 Encode the results
            result = Base64.encodeToString(digest, Base64.NO_WRAP);
        }

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }

    return result;
}

From source file:de.desy.dcache.s3.Signature.java

/**
* Computes RFC 2104-compliant HMAC signature.
* * @param data/*from  w w  w. j av  a 2s  .  com*/
* 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 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.getMessage());
    }
    return result;
}

From source file:com.spectralogic.ds3client.utils.Signature.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 * * @param data/*from   w  w  w.ja  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 calculateRFC2104HMAC(final String data, final String key)
        throws java.security.SignatureException {
    LOG.debug("String to sign: {}", data.replace("\n", "\\n"));
    final String result;
    try {
        // get an hmac_sha1 key from the raw key bytes
        final SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")),
                HMAC_SHA1_ALGORITHM);

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

        // compute the hmac on input data bytes
        final byte[] rawHmac = mac.doFinal(data.getBytes(Charset.forName("UTF-8")));
        result = Base64.encodeBase64String(rawHmac);
    } catch (final Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result.trim();
}

From source file:com.vab.iflex.gateway.paybillservice.Stub.java

private static String calculateRFC2104HMAC(String data, String key) throws Exception {
    String result;/*  ww w . j av  a2  s  .  com*/
    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 BASE64Encoder().encode(rawHmac);
        result = new Base64().encodeAsString(rawHmac);

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
}

From source file:org.xdi.oxauth.model.jws.RSASigner.java

@Override
public String generateSignature(String signingInput) throws SignatureException {
    if (getSignatureAlgorithm() == null) {
        throw new SignatureException("The signature algorithm is null");
    }//ww w .  j a  v a  2  s.  co  m
    if (rsaPrivateKey == null) {
        throw new SignatureException("The RSA private key is null");
    }
    if (signingInput == null) {
        throw new SignatureException("The signing input is null");
    }

    try {
        RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
                rsaPrivateKey.getPrivateExponent());

        KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
        PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);

        Signature signature = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        signature.initSign(privateKey);
        signature.update(signingInput.getBytes(Util.UTF8_STRING_ENCODING));

        return JwtUtil.base64urlencode(signature.sign());
    } catch (InvalidKeySpecException e) {
        throw new SignatureException(e);
    } catch (InvalidKeyException e) {
        throw new SignatureException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SignatureException(e);
    } catch (NoSuchProviderException e) {
        throw new SignatureException(e);
    } catch (SignatureException e) {
        throw new SignatureException(e);
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException(e);
    } catch (Exception e) {
        throw new SignatureException(e);
    }
}

From source file:com.lehman.ic9.common.base64.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 * @param data Is a String with the data to encode.
 * @param key Is a String with the key.//from  w  w w  .  ja  v a2s.c  o  m
 * @return A string with the encoded signature.
 * @throws SignatureException Exception
 */
public static String encodeHmac(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.getMessage());
    }
    return result;
}

From source file:org.apache.camel.component.crypto.processor.VerifyingProcessor.java

public void process(Exchange exchange) throws Exception {
    Signature signer = createSignatureService();
    Certificate cert = getCertificate(exchange);
    if (cert == null) {
        PublicKey pk = getPublicKeyOrCertificateFromHeader(exchange, PublicKey.class, config.getPublicKey());
        if (pk == null) {
            throw new IllegalStateException(String.format(
                    "Cannot verify signature as no Public Key or Certificate has been supplied."
                            + " Either supply one in the route definition or via the message header '%s'",
                    DigitalSignatureConstants.SIGNATURE_PUBLIC_KEY_OR_CERT));
        }/*from w w  w . jav a2s. c  o  m*/
        signer.initVerify(pk);
    } else {
        signer.initVerify(cert);
    }

    calculateSignature(exchange, signer);

    byte[] signature = getSignatureFromExchange(exchange);
    if (!signer.verify(signature)) {
        throw new SignatureException("Cannot verify signature of exchange");
    }
    clearMessageHeaders(exchange.getIn());
}

From source file:com.adyen.Util.HMACValidator.java

public String calculateHMAC(String data, String key) throws java.security.SignatureException {
    try {/*w w w  .  j  a  v  a2s . co m*/
        byte[] rawKey = Hex.decodeHex(key.toCharArray());
        // Create an hmac_sha256 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(rawKey, HMAC_SHA256_ALGORITHM);

        // Get an hmac_sha256 Mac instance and initialize with the signing
        // key
        Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);

        mac.init(signingKey);

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

        // Base64-encode the hmac
        return new String(Base64.encodeBase64(rawHmac));

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
}