List of usage examples for java.security Signature update
public final void update(ByteBuffer data) throws SignatureException
From source file:ch.hsr.challp.and4.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/* ww w. j a va 2 s . c o m*/ * @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))) { 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:com.sammyun.util.RSAUtils.java
/** * RSA??// w w w . j ava 2s . com * * @param content ??? * @return */ public static String encryptContent(String content, String ali_public_key) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64Util.decode(ali_public_key); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); signature.update(content.getBytes("utf-8")); byte[] signed = signature.sign(); return Base64Util.encode(signed); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return ""; } }
From source file:com.sammyun.util.RSAUtils.java
/** * RSA??/*from www . jav a 2s . c om*/ * * @param content ??? * @param sign ?? * @param ali_public_key ? * @param input_charset ?? * @return */ public static boolean verify(String content, String sign, String ali_public_key, String input_charset) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64Util.decode(ali_public_key); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); signature.update(content.getBytes(input_charset)); boolean bverify = signature.verify(Base64Util.decode(sign)); return bverify; } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.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 av a 2s . c om * * @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(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 TransportHandlerException(errorMsg, e); } catch (SignatureException e) { String errorMsg = "Signature exception occurred for Signature instance of [" + SHA_512 + "]"; 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:com.github.aynu.yukar.framework.util.SecurityHelper.java
/** * ????//from w w w.j a va2 s .c o m * <dl> * <dt>? * <dd>NONEwithECDSA??????????????? * </dl> * @param key ? * @param data * @return ?? */ public static byte[] sign(final PrivateKey key, final byte[] data) { try { final Signature sign = Signature.getInstance("NONEwithECDSA"); sign.initSign(key); sign.update(data); return sign.sign(); } catch (final NoSuchAlgorithmException | InvalidKeyException | SignatureException e) { throw new StandardRuntimeException(e); } }
From source file:org.wso2.carbon.device.mgt.iot.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./*w w w. j av 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 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.virtualfirealarm.agent.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 . 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(SHA_512); 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 [" + SHA_512 + "]"; log.error(errorMsg); throw new TransportHandlerException(errorMsg, e); } catch (SignatureException e) { String errorMsg = "Signature exception occurred for Signature instance of [" + SHA_512 + "]"; 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:com.soomla.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 . ja va2 s . c om * @return true if the data and signature match */ public static boolean verify(PublicKey publicKey, String signedData, String signature) { StoreUtils.LogDebug(TAG, "signature: " + signature); Signature sig; try { sig = Signature.getInstance(SIGNATURE_ALGORITHM); sig.initVerify(publicKey); sig.update(signedData.getBytes()); if (!sig.verify(Base64.decode(signature))) { StoreUtils.LogError(TAG, "Signature verification failed."); return false; } return true; } catch (NoSuchAlgorithmException e) { StoreUtils.LogError(TAG, "NoSuchAlgorithmException."); } catch (InvalidKeyException e) { StoreUtils.LogError(TAG, "Invalid key specification."); } catch (SignatureException e) { StoreUtils.LogError(TAG, "Signature exception."); } catch (Base64DecoderException e) { StoreUtils.LogError(TAG, "Base64 decoding failed."); } return false; }
From source file:com.parking.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// ww w.ja v a 2s . c o m * @return true if the data and signature match */ public static boolean verify(PublicKey publicKey, String signedData, String signature) { if (BillingConstants.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 (Base64DecoderException e) { Log.e(TAG, "Base64 decoding failed."); } return false; }
From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java
/** * ???/*from w w w . j a va 2 s .com*/ * <dl> * <dt>? * <dd>NONEwithECDSA?????????????? * </dl> * @param publicKey ? * @param message * @param signature ?? * @return ? */ public static boolean verify(final PublicKey publicKey, final byte[] signature, final byte[] message) { try { final Signature sign = Signature.getInstance("NONEwithECDSA"); sign.initVerify(publicKey); sign.update(message); return sign.verify(signature); } catch (final NoSuchAlgorithmException | InvalidKeyException | SignatureException e) { throw new StandardRuntimeException(e); } }