Example usage for java.security Signature initVerify

List of usage examples for java.security Signature initVerify

Introduction

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

Prototype

public final void initVerify(Certificate certificate) throws InvalidKeyException 

Source Link

Document

Initializes this object for verification, using the public key from the given certificate.

Usage

From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.VirtualFirealarmSecurityManager.java

public static boolean verifySignature(String data, String signedData, PublicKey verificationKey)
        throws VirtualFirealarmDeviceMgtPluginException {

    Signature signature;
    boolean verified;

    try {/*from w w w .  jav a 2 s. c o m*/
        signature = Signature.getInstance(SHA_512);
        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 [" + SHA_512 + "]";
        log.error(errorMsg);
        throw new VirtualFirealarmDeviceMgtPluginException(errorMsg, e);
    } catch (SignatureException e) {
        String errorMsg = "Signature exception occurred for Signature instance of [" + SHA_512 + "]";
        log.error(errorMsg);
        throw new VirtualFirealarmDeviceMgtPluginException(errorMsg, e);
    } catch (InvalidKeyException e) {
        String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + verificationKey + "\n]\n";
        log.error(errorMsg);
        throw new VirtualFirealarmDeviceMgtPluginException(errorMsg, e);
    }

    return verified;
}

From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.SecurityManager.java

public static boolean verifySignature(String data, String signedData, PublicKey verificationKey)
        throws VirtualFireAlarmException {

    Signature signature;
    boolean verified;

    try {//  w ww.j  av  a  2s  . c o m
        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 VirtualFireAlarmException(errorMsg, e);
    } catch (SignatureException e) {
        String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]";
        log.error(errorMsg);
        throw new VirtualFireAlarmException(errorMsg, e);
    } catch (InvalidKeyException e) {
        String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + verificationKey + "\n]\n";
        log.error(errorMsg);
        throw new VirtualFireAlarmException(errorMsg, e);
    }

    return verified;
}

From source file:jfabrix101.billing.BillingSecurity.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/* ww  w .  j  av a  2 s  .  c o  m*/
 * @return true if the data and signature match
 */
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    if (BillingConsts.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))) {
            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 (Exception e) {
        Log.e(TAG, "Base64 decoding failed.");
    }
    return false;
}

From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.util.VirtualFirealarmSecurityManager.java

public static boolean verifySignature(String data, String signedData, PublicKey verificationKey)
        throws VirtualFirealarmDeviceMgtPluginException {

    Signature signature;
    boolean verified;

    try {//from w w w  . ja  v  a  2  s .  c o  m
        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 VirtualFirealarmDeviceMgtPluginException(errorMsg, e);
    } catch (SignatureException e) {
        String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]";
        log.error(errorMsg);
        throw new VirtualFirealarmDeviceMgtPluginException(errorMsg, e);
    } catch (InvalidKeyException e) {
        String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + verificationKey + "\n]\n";
        log.error(errorMsg);
        throw new VirtualFirealarmDeviceMgtPluginException(errorMsg, e);
    }

    return verified;
}

From source file:org.apache.abdera2.common.security.HashHelper.java

public static boolean sigval(PublicKey key, String alg, byte[] mat, byte[] dat) {
    try {/*from w w w  .j  av a2 s. c  o m*/
        Signature sig = Signature.getInstance(alg);
        sig.initVerify(key);
        sig.update(mat);
        return sig.verify(dat);
    } catch (Throwable t) {
        throw ExceptionHelper.propogate(t);
    }
}

From source file:com.vmware.identity.rest.core.util.RequestSigner.java

/**
 * Verify a signed request using a hex-formatted string, the string to sign, and a certificate's public key.
 *
 * @param signedRequestHex a hex-encoded string representing the signed request to verify.
 * @param stringToSign the string that will be signed with the public key for
 * verification purposes.// w  w  w .  j a  v  a 2  s . co  m
 * @param publicKey the public key used for verification.
 * @return true if the signature was verified, false if not.
 * @throws DecoderException if there is an error decoding the hex string.
 * @throws InvalidKeyException if the public key is invalid.
 * @throws SignatureException if the signature algorithm is unable to process the input
 * data provided.
 */
public static boolean verify(String signedRequestHex, String stringToSign, PublicKey publicKey)
        throws DecoderException, InvalidKeyException, SignatureException {
    byte[] signedRequest = Hex.decodeHex(signedRequestHex.toCharArray());

    Signature sig;

    try {
        sig = Signature.getInstance(SHA256_WITH_RSA);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("An error occurred while getting the signature algorithm", e);
    }

    sig.initVerify(publicKey);
    sig.update(stringToSign.getBytes(StandardCharsets.UTF_8));

    return sig.verify(signedRequest);
}

From source file:cn.mrdear.pay.util.RSAUtils.java

/**
 * ???/*from   ww  w .j a  v  a  2  s  .  c  om*/
 * 
 * @param algorithm
 *            ??
 * @param publicKey
 *            
 * @param sign
 *            ??
 * @param data
 *            ?
 * @return ??
 */
public static boolean verify(String algorithm, PublicKey publicKey, byte[] sign, byte[] data) {
    Assert.isNotEmpty(algorithm);
    Assert.notNull(publicKey);
    Assert.notNull(sign);
    Assert.notNull(data);

    try {
        Signature signature = Signature.getInstance(algorithm, PROVIDER);
        signature.initVerify(publicKey);
        signature.update(data);
        return signature.verify(sign);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (SignatureException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:cn.mrdear.pay.util.RSAUtils.java

/**
 * ???/* w w  w  .j  a v  a 2 s.c o m*/
 * 
 * @param algorithm
 *            ??
 * @param certificate
 *            ?
 * @param sign
 *            ??
 * @param data
 *            ?
 * @return ??
 */
public static boolean verify(String algorithm, Certificate certificate, byte[] sign, byte[] data) {
    Assert.isNotEmpty(algorithm);
    Assert.notNull(certificate);
    Assert.notNull(sign);
    Assert.notNull(data);

    try {
        Signature signature = Signature.getInstance(algorithm, PROVIDER);
        signature.initVerify(certificate);
        signature.update(data);
        return signature.verify(sign);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (SignatureException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.glaf.core.security.SecurityUtils.java

/**
 * ???/*from w  w w  .  j  a v a  2  s  .c  o  m*/
 * 
 * @param ctx
 *            
 * @param source
 *            
 * @param signed
 *            ???
 * @param pubKey
 *            
 * @return boolean
 */
public static boolean verify(SecurityContext ctx, byte[] source, byte[] signed, PublicKey publicKey) {
    try {
        boolean verify = false;
        Signature sign = Signature.getInstance(ctx.getSignatureAlgorithm(), ctx.getJceProvider());
        sign.initVerify(publicKey);
        sign.update(source);
        verify = sign.verify(signed);
        return verify;
    } catch (Exception ex) {
        throw new SecurityException(ex);
    }
}

From source file:de.ub0r.android.lib.DonationHelper.java

/**
 * Check for signature updates./* www . jav  a2  s .  c  o m*/
 * 
 * @param context
 *            {@link Context}
 * @param s
 *            signature
 * @param h
 *            hash
 * @return true if ads should be hidden
 */
public static boolean checkSig(final Context context, final String s, final String h) {
    Log.d(TAG, "checkSig(ctx, " + s + ", " + h + ")");
    boolean ret = false;
    try {
        final byte[] publicKey = Base64Coder.decode(KEY);
        final KeyFactory keyFactory = KeyFactory.getInstance(ALGO);
        PublicKey pk = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));
        Log.d(TAG, "hash: " + h);
        final String cs = s.replaceAll(" |\n|\t", "");
        Log.d(TAG, "read sig: " + cs);
        try {
            byte[] signature = Base64Coder.decode(cs);
            Signature sig = Signature.getInstance(SIGALGO);
            sig.initVerify(pk);
            sig.update(h.getBytes());
            ret = sig.verify(signature);
            Log.d(TAG, "ret: " + ret);
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "error reading signature", e);
        }
    } catch (Exception e) {
        Log.e(TAG, "error reading signatures", e);
    }
    if (!ret) {
        Log.i(TAG, "sig: " + s);
    }
    return ret;
}