Example usage for java.security Signature update

List of usage examples for java.security Signature update

Introduction

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

Prototype

public final void update(ByteBuffer data) throws SignatureException 

Source Link

Document

Updates the data to be signed or verified using the specified ByteBuffer.

Usage

From source file:org.wso2.carbon.device.mgt.iot.agent.firealarm.transport.CommunicationUtils.java

/**
 * Verifies some signed-data against the a Public-Key to ensure that it was produced by the holder of the
 * corresponding Private Key.//from   w  w w. j a  v  a2s .c o  m
 *
 * @param data            the actual payoad which was signed by some Private Key.
 * @param signedData      the signed data produced by signing the payload using a Private Key.
 * @param verificationKey the corresponding Public Key which is an exact pair of the Private-Key with we expect
 *                        the data to be signed by.
 * @return true if the signed data verifies to be signed by the corresponding Private Key.
 * @throws AgentCoreOperationException if some error occurs with the verification process which may be related to
 *                                     the signature algorithm used or the key used for signing.
 */
public static boolean verifySignature(String data, String signedData, PublicKey verificationKey)
        throws AgentCoreOperationException {

    Signature signature;
    boolean verified;

    try {
        signature = Signature.getInstance(SIGNATURE_ALG);
        signature.initVerify(verificationKey);
        signature.update(Base64.decodeBase64(data));

        verified = signature.verify(Base64.decodeBase64(signedData));

    } catch (NoSuchAlgorithmException e) {
        String errorMsg = "Algorithm not found exception occurred for Signature instance of [" + SIGNATURE_ALG
                + "]";
        log.error(errorMsg);
        throw new AgentCoreOperationException(errorMsg, e);
    } catch (SignatureException e) {
        String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]";
        log.error(errorMsg);
        throw new AgentCoreOperationException(errorMsg, e);
    } catch (InvalidKeyException e) {
        String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + verificationKey + "\n]\n";
        log.error(errorMsg);
        throw new AgentCoreOperationException(errorMsg, e);
    }

    return verified;
}

From source file:org.wso2.carbon.device.mgt.iot.transport.CommunicationUtils.java

/**
 * Signed a given message using the PrivateKey that's passes in.
 *
 * @param message      the message to be signed. Ideally some encrypted payload.
 * @param signatureKey the PrivateKey with which the message is to be signed.
 * @return the Base64Encoded String of the signed payload.
 * @throws TransportHandlerException if some error occurs with the signing process which may be related to the
 *                                   signature algorithm used or the key used for signing.
 *//*from   w  w w .  ja v a  2  s  .co m*/
public static String signMessage(String message, PrivateKey signatureKey) throws TransportHandlerException {

    Signature signature;
    String signedEncodedString;

    try {
        signature = Signature.getInstance(SIGNATURE_ALG);
        signature.initSign(signatureKey);
        signature.update(Base64.decodeBase64(message));

        byte[] signatureBytes = signature.sign();
        signedEncodedString = Base64.encodeBase64String(signatureBytes);

    } catch (NoSuchAlgorithmException e) {
        String errorMsg = "Algorithm not found exception occurred for Signature instance of [" + SIGNATURE_ALG
                + "]";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    } catch (SignatureException e) {
        String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    } catch (InvalidKeyException e) {
        String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + signatureKey + "\n]\n";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    }
    return signedEncodedString;
}

From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.advanced.transport.CommunicationUtils.java

/**
 * Verifies some signed-data against the a Public-Key to ensure that it was produced by the holder of the
 * corresponding Private Key.//from w w  w  .ja  v  a 2s  .  c o  m
 *
 * @param data            the actual payoad which was signed by some Private Key.
 * @param signedData      the signed data produced by signing the payload using a Private Key.
 * @param verificationKey the corresponding Public Key which is an exact pair of the Private-Key with we expect
 *                        the data to be signed by.
 * @return true if the signed data verifies to be signed by the corresponding Private Key.
 * @throws TransportHandlerException if some error occurs with the verification process which may be related to
 *                                   the signature algorithm used or the key used for signing.
 */
public static boolean verifySignature(String data, String signedData, PublicKey verificationKey)
        throws TransportHandlerException {

    Signature signature;
    boolean verified;

    try {
        signature = Signature.getInstance(SIGNATURE_ALG);
        signature.initVerify(verificationKey);
        signature.update(Base64.decodeBase64(data));

        verified = signature.verify(Base64.decodeBase64(signedData));

    } catch (NoSuchAlgorithmException e) {
        String errorMsg = "Algorithm not found exception occurred for Signature instance of [" + SIGNATURE_ALG
                + "]";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    } catch (SignatureException e) {
        String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    } catch (InvalidKeyException e) {
        String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + verificationKey + "\n]\n";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    }

    return verified;
}

From source file:org.wso2.carbon.device.mgt.iot.agent.firealarm.transport.CommunicationUtils.java

/**
 * Signed a given message using the PrivateKey that's passes in.
 *
 * @param message      the message to be signed. Ideally some encrypted payload.
 * @param signatureKey the PrivateKey with which the message is to be signed.
 * @return the Base64Encoded String of the signed payload.
 * @throws AgentCoreOperationException if some error occurs with the signing process which may be related to the
 *                                     signature algorithm used or the key used for signing.
 *//*from  w  w  w  .ja va  2  s .c  o  m*/
public static String signMessage(String message, PrivateKey signatureKey) throws AgentCoreOperationException {

    Signature signature;
    String signedEncodedString;

    try {
        signature = Signature.getInstance(SIGNATURE_ALG);
        signature.initSign(signatureKey);
        signature.update(Base64.decodeBase64(message));

        byte[] signatureBytes = signature.sign();
        signedEncodedString = Base64.encodeBase64String(signatureBytes);

    } catch (NoSuchAlgorithmException e) {
        String errorMsg = "Algorithm not found exception occurred for Signature instance of [" + SIGNATURE_ALG
                + "]";
        log.error(errorMsg);
        throw new AgentCoreOperationException(errorMsg, e);
    } catch (SignatureException e) {
        String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]";
        log.error(errorMsg);
        throw new AgentCoreOperationException(errorMsg, e);
    } catch (InvalidKeyException e) {
        String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + signatureKey + "\n]\n";
        log.error(errorMsg);
        throw new AgentCoreOperationException(errorMsg, e);
    }

    return signedEncodedString;
}

From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.advanced.transport.CommunicationUtils.java

/**
 * Signed a given message using the PrivateKey that's passes in.
 *
 * @param message      the message to be signed. Ideally some encrypted payload.
 * @param signatureKey the PrivateKey with which the message is to be signed.
 * @return the Base64Encoded String of the signed payload.
 * @throws TransportHandlerException if some error occurs with the signing process which may be related to the
 *                                   signature algorithm used or the key used for signing.
 *//* w  w  w . j a v a 2 s .  c  o  m*/
public static String signMessage(String message, PrivateKey signatureKey) throws TransportHandlerException {

    Signature signature;
    String signedEncodedString;

    try {
        signature = Signature.getInstance(SIGNATURE_ALG);
        signature.initSign(signatureKey);
        signature.update(Base64.decodeBase64(message));

        byte[] signatureBytes = signature.sign();
        signedEncodedString = Base64.encodeBase64String(signatureBytes);

    } catch (NoSuchAlgorithmException e) {
        String errorMsg = "Algorithm not found exception occurred for Signature instance of [" + SIGNATURE_ALG
                + "]";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    } catch (SignatureException e) {
        String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    } catch (InvalidKeyException e) {
        String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + signatureKey + "\n]\n";
        log.error(errorMsg);
        throw new TransportHandlerException(errorMsg, e);
    }

    return signedEncodedString;
}

From source file:dk.itst.oiosaml.sp.service.util.Utils.java

/**
 * Check if a SAML HTTP Redirect has been signed by the expected certificate
 * /*from www .  ja va2  s . com*/
 * @param data
 *            The query parameters in the HTTP Redirect, which has been
 *            signed
 * @param key
 *            The public key of the certificate from the expected sender
 * @param sig
 *            The signature generated by the sender after it has been base64
 *            decoded
 * @return true, if the signature is valid, otherwise false
 */
public static boolean verifySignature(byte[] data, PublicKey key, byte[] sig) {

    if (log.isDebugEnabled())
        log.debug("data...:" + new String(data));
    if (log.isDebugEnabled())
        log.debug("sig....:" + new String(sig));
    if (log.isDebugEnabled())
        log.debug("key....:" + key.toString());

    try {
        Signature signer = Signature.getInstance(OIOSAMLConstants.SHA1_WITH_RSA);
        signer.initVerify(key);
        signer.update(data);
        return signer.verify(sig);
    } catch (InvalidKeyException e) {
        throw new WrappedException(Layer.CLIENT, e);
    } catch (NoSuchAlgorithmException e) {
        throw new WrappedException(Layer.CLIENT, e);
    } catch (SignatureException e) {
        throw new WrappedException(Layer.CLIENT, e);
    }
}

From source file:org.cprados.wificellmanager.billing.Security.java

/**
 * Verifies that the signature from the server matches the computed
 * signature on the data.  Returns true if the data is correctly signed.
 *
 * @param publicKey public key associated with the developer account
 * @param signedData signed data from server
 * @param signature server signature/*from  ww  w .  j  a  v a 2  s. c  om*/
 * @return true if the data and signature match
 */
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    if (Consts.DEBUG) {
        Log.i(TAG, "signature: " + signature);
    }
    Signature sig;
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        //if (!sig.verify(Base64.decode(signature))) {
        if (!sig.verify(Base64.decode(signature, Base64.DEFAULT))) {
            Log.e(TAG, "Signature verification failed.");
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "NoSuchAlgorithmException.");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "Invalid key specification.");
    } catch (SignatureException e) {
        Log.e(TAG, "Signature exception.");
    } // catch (Base64DecoderException e) {
      //    Log.e(TAG, "Base64 decoding failed.");
      //}
    return false;
}

From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java

public static String generateSAMLRequestSignature(String urlEncodedString, PrivateKey signingKey)
        throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, UnsupportedEncodingException {
    if (signingKey == null) {
        return urlEncodedString;
    }/*from  ww w  .j a  v a 2s.  co  m*/
    String url = urlEncodedString + "&SigAlg="
            + URLEncoder.encode(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1, HttpUtils.UTF_8);
    Signature signature = Signature.getInstance("SHA1withRSA");
    signature.initSign(signingKey);
    signature.update(url.getBytes());
    String signatureString = Base64.encodeBytes(signature.sign(), Base64.DONT_BREAK_LINES);
    if (signatureString != null) {
        return url + "&Signature=" + URLEncoder.encode(signatureString, HttpUtils.UTF_8);
    }
    return url;
}

From source file:org.bankinterface.util.Utils.java

/**
 * SHA1withRSA???,??// w w w  .j a v  a 2 s.c o m
 * 
 * @param data
 * @param charset
 * @param certFilePath
 * @param privateKeyAlias
 * @param code
 * @return
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws UnsupportedEncodingException
 * @throws SignatureException
 */
public static String signSHA1withRSA(String data, String charset, String certFilePath, String privateKeyAlias,
        String code)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
    PrivateKey privateKey = KeyStoreUtil.getPrivateKey(certFilePath, privateKeyAlias);
    Signature signature = Signature.getInstance(ALGORITHM_SHA1WITHRSA);
    signature.initSign(privateKey);
    signature.update(getBytes(data, charset));
    byte[] bytes = signature.sign();
    return Utils.encode(bytes, code);
}

From source file:org.bankinterface.util.Utils.java

/**
 * SHA1withRSA???/*www  .  j  a  v  a2 s  .co m*/
 * 
 * @param sourceData
 * @param signData
 * @param certFilePath
 * @param publicKeyAlias
 * @return
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws UnsupportedEncodingException
 * @throws SignatureException
 */
public static boolean verifySHA1withRSA(String sourceData, String signData, String charset, String certFilePath,
        String publicKeyAlias)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
    PublicKey publicKey = KeyStoreUtil.getPublicKey(certFilePath, publicKeyAlias);
    Signature signature = Signature.getInstance(ALGORITHM_SHA1WITHRSA);
    signature.initVerify(publicKey);
    signature.update(getBytes(sourceData, charset));
    return signature.verify(getBytes(signData, charset));
}