List of usage examples for java.security Signature update
public final void update(ByteBuffer data) throws SignatureException
From source file:com.dev.cty.utils.googleplay.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 w ww . 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) { Signature sig; try { sig = Signature.getInstance(SIGNATURE_ALGORITHM); sig.initVerify(publicKey); sig.update(signedData.getBytes()); if (!sig.verify(Base64.decode(signature))) { logger.info("Signature verification failed."); return false; } return true; } catch (NoSuchAlgorithmException e) { logger.info("NoSuchAlgorithmException."); } catch (InvalidKeyException e) { logger.info("Invalid key specification."); } catch (SignatureException e) { logger.info("Signature exception."); } catch (Base64DecoderException e) { logger.info("Base64 decoding failed."); } return false; }
From source file:org.syphr.utils.x509.X509Utils.java
/** * Create a signature using the given token and private key. * * @param message/*w ww. j a v a 2s .co m*/ * the message to sign * @param key * the private key to use to create the signature (this must be * PKCS8 encoded) * @param keyAlg * the algorithm used to create the private key * @param sigAlg * the algorithm to use to create the signature * @return the signature * @throws IOException * if there is an error reading the key * @throws InvalidKeySpecException * if the key algorithm is not appropriate for the given private * key * @throws InvalidKeyException * if the given private key is not valid * @throws SignatureException * if there is an error while generating the signature */ public static byte[] sign(String message, InputStream key, KeyAlgorithm keyAlg, SignatureAlgorithm sigAlg) throws IOException, InvalidKeySpecException, InvalidKeyException, SignatureException { KeySpec privateKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(key)); try { KeyFactory keyFactory = KeyFactory.getInstance(keyAlg.getAlgorithm()); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); Signature sig = Signature.getInstance(sigAlg.getAlgorithm()); sig.initSign(privateKey); sig.update(message.getBytes()); return sig.sign(); } catch (NoSuchAlgorithmException e) { /* * This is protected against by enforcing specific algorithm * choices. */ throw new IllegalArgumentException("Unknown algorithm", e); } }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ???//from ww w . j av a2 s . c o m * * @return * @throws Exception */ static void verifyByPublicKey(String target, String sign) throws Exception { PublicKey publicKey = getPublicKey(); Signature signature = Signature.getInstance(ALGORITHM_SIGN); signature.initVerify(publicKey); signature.update(target.getBytes("UTF-8")); if (signature.verify(decodeBase64(sign))) { System.out.println("???"); } else { System.out.println("???"); } }
From source file:acceptable_risk.nik.uniobuda.hu.andrawid.util.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/*w ww . ja va 2 s . c o m*/ * @return true if the data and signature match */ public static boolean verify(PublicKey publicKey, String signedData, String 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:org.dasein.cloud.google.GenerateToken.java
public static String getToken(String iss, String p12File) { String header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"; String claimTemplate = "'{'\"iss\": \"{0}\", \"scope\": \"{1}\", \"aud\": \"{2}\", \"exp\": \"{3}\", \"iat\": \"{4}\"'}'"; try {//from w ww .ja v a 2 s . c o m StringBuffer token = new StringBuffer(); //Encode the JWT Header and add it to our string to sign token.append(Base64.encodeBase64URLSafeString(header.getBytes("UTF-8"))); //Separate with a period token.append("."); //Create the JWT Claims Object String[] claimArray = new String[5]; claimArray[0] = iss; claimArray[1] = "https://www.googleapis.com/auth/compute"; claimArray[2] = "https://accounts.google.com/o/oauth2/token"; claimArray[3] = Long.toString((System.currentTimeMillis() / 1000) + 300); claimArray[4] = Long.toString((System.currentTimeMillis() / 1000)); MessageFormat claims; claims = new MessageFormat(claimTemplate); String payload = claims.format(claimArray); // System.out.println(claimArray[3]); // System.out.println(claimArray[4]); //Add the encoded claims object token.append(Base64.encodeBase64URLSafeString(payload.getBytes("UTF-8"))); char[] password = "notasecret".toCharArray(); FileInputStream fin = new FileInputStream(new File(p12File)); KeyStore store = KeyStore.getInstance("PKCS12"); try { store.load(fin, password); } finally { try { fin.close(); } catch (IOException e) { } } String alias = ""; // KeyStore keystore = getKeyStore(password); Enumeration<String> enum1 = store.aliases(); // List the aliases while (enum1.hasMoreElements()) { String keyStoreAlias = enum1.nextElement().toString(); if (store.isKeyEntry(keyStoreAlias)) { //Does alias refer to a private key? alias = keyStoreAlias; break; } } PrivateKey privateKey = (PrivateKey) store.getKey(alias, password); //Sign the JWT Header + "." + JWT Claims Object Signature signature = Signature.getInstance("SHA256withRSA"); signature.initSign(privateKey); signature.update(token.toString().getBytes("UTF-8")); String signedPayload = Base64.encodeBase64URLSafeString(signature.sign()); //Separate with a period token.append("."); //Add the encoded signature token.append(signedPayload); // System.out.println(token.toString()); return token.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:me.disconnect.mobile.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// w w w .jav a2s .c o m * @return true if the data and signature match */ public static boolean verify(PublicKey publicKey, String signedData, String 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."); } catch (RuntimeException e) { Log.e(TAG, "RuntimeException in Security.verify():"); e.printStackTrace(); } return false; }
From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java
private static byte[] getSignature(byte[] bytes, PrivateKey receiverServiceKey) throws SignatureException, InvalidKeyException, NoSuchAlgorithmException { Signature signature = Signature.getInstance("MD5WithRSA"); signature.initSign(receiverServiceKey); signature.update(bytes);//from w ww . jav a 2 s . c om return (signature.sign()); }
From source file:org.carewebframework.api.security.CipherUtil.java
/** * Returns the digital signature for the specified content. * //from w w w .j a va2 s .c o m * @param key The private key to sign the content. * @param content The content to sign. * @return The digital signature. * @throws Exception Unspecified exception. */ public static String sign(PrivateKey key, String content) throws Exception { Signature signature = Signature.getInstance(SIGN_ALGORITHM); signature.initSign(key); signature.update(content.getBytes()); return Base64.encodeBase64String(signature.sign()); }
From source file:org.wso2.carbon.connector.CreateJWT.java
/** * The method to sign the byte array of data using the private key. * * @param data the byte array of data to generate the signature * @param privateKey the private key to sign the byte array * @return the signed signature/*from www. jav a 2 s. c o m*/ * @throws InvalidKeyException * @throws SignatureException * @throws NoSuchAlgorithmException */ public static byte[] signData(byte[] data, PrivateKey privateKey) throws InvalidKeyException, SignatureException, NoSuchAlgorithmException { Signature signature = Signature.getInstance(JWTConstant.SIGNATURE); signature.initSign(privateKey); signature.update(data); return signature.sign(); }
From source file:com.easarrive.aws.plugins.common.util.SNSUtil.java
public static boolean isMessageSignatureValid(SNSMessage msg) { try {/*from w ww. j a va 2 s .c o m*/ URL url = new URL(msg.getSigningCertURL()); InputStream inStream = url.openStream(); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream); inStream.close(); Signature sig = Signature.getInstance("SHA1withRSA"); sig.initVerify(cert.getPublicKey()); sig.update(getMessageBytesToSign(msg)); return sig.verify(Base64.decodeBase64(msg.getSignature())); } catch (Exception e) { throw new SecurityException("Verify method failed.", e); } }