List of usage examples for java.security Signature update
public final void update(ByteBuffer data) throws SignatureException
From source file:com.glaf.core.security.SecurityUtils.java
/** * ???// w w w .jav a2s .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:org.umit.icm.mobile.utils.RSACrypto.java
/** * Returns an RSA KeyPair generated using * {@link KeyPairGenerator#generateKeyPair()}. * //from w w w. ja v a2 s . com * @return {@link KeyPair} * @see KeyPairGenerator */ public static byte[] Sign(PrivateKey privateKey, byte[] data) throws Exception { if (Constants.DEBUG_MODE) System.out.println("Signing the key inside RSACrypto#Sign"); Signature dsa = Signature.getInstance("SHA1withRSA"); dsa.initSign(privateKey); dsa.update(data); return dsa.sign(); }
From source file:de.ub0r.android.lib.DonationHelper.java
/** * Check for signature updates.// w w w .j a v a 2s . 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; }
From source file:com.eucalyptus.blockstorage.HttpTransfer.java
/** * Calculates and sets the Authorization header value for the request using the EucaRSA-V2 signing algorithm * Algorithm Overview:/* ww w . j a v a 2 s .co m*/ * * 1. Generate the canonical Request * a.) CanonicalRequest = * HTTPRequestMethod + '\n' + * CanonicalURI + '\n' + * CanonicalQueryString + '\n' + * CanonicalHeaders + '\n' + * SignedHeaders * b.) Where CanonicalURI = * c.) Where CanonicalQueryString = * d.) Where CanonicalHeaders = sorted (by lowercased header name) ';' delimited list of <lowercase(headername)>:<value> items * e.) Where SignedHeaders = sorted, ';' delimited list of headers in CanonicalHeaders * * 2. Signature = RSA(privkey, SHA256(CanonicalRequest)) * * 3. Add an Authorization HTTP header to the request that contains the following strings, separated by spaces: * EUCA2-RSA-SHA256 * The lower-case hexadecimal encoding of the component's X.509 certificate's md5 fingerprint * The SignedHeaders list calculated in Task 1 * The Base64 encoding of the Signature calculated in Task 2 * * @param httpBaseRequest -- the request, the 'Authorization' header will be added to the request */ public static void signEucaInternal(HttpMethodBase httpBaseRequest) { StringBuilder canonicalRequest = new StringBuilder(); String canonicalURI = null; String verb = httpBaseRequest.getName(); canonicalURI = httpBaseRequest.getPath(); String canonicalQuery = calcCanonicalQuery(httpBaseRequest); String[] processedHeaders = getCanonicalAndSignedHeaders(httpBaseRequest); String canonicalHeaders = processedHeaders[0]; String signedHeaders = processedHeaders[1]; canonicalRequest.append(verb).append('\n'); canonicalRequest.append(canonicalURI).append('\n'); canonicalRequest.append(canonicalQuery).append('\n'); canonicalRequest.append(canonicalHeaders).append('\n'); canonicalRequest.append(signedHeaders); StringBuilder authHeader = new StringBuilder(EUCA2_AUTH_ID); String signature = null; String fingerprint = null; try { Credentials ccCreds = SystemCredentials.lookup(Storage.class); PrivateKey ccPrivateKey = ccCreds.getPrivateKey(); fingerprint = ccCreds.getCertFingerprint(); Signature sign = Signature.getInstance("SHA256withRSA"); sign.initSign(ccPrivateKey); LOG.debug("Signing canonical request: " + canonicalRequest.toString()); sign.update(canonicalRequest.toString().getBytes()); byte[] sig = sign.sign(); signature = new String(Base64.encode(sig)); } catch (Exception ex) { LOG.error("Signing error while signing request", ex); } authHeader.append(" ").append(fingerprint.toLowerCase()).append(" ").append(signedHeaders.toString()) .append(" ").append(signature); httpBaseRequest.addRequestHeader(EUCA2_AUTH_HEADER_NAME, authHeader.toString()); }
From source file:com.glaf.core.security.SecurityUtils.java
/** * ?????????//from ww w . j av a2 s. co m * * @param ctx * * @param content * ?? * @param privateKey * ? * @return byte[] ??? */ public static byte[] sign(SecurityContext ctx, byte[] content, Key privateKey) { try { Signature sign = Signature.getInstance(ctx.getSignatureAlgorithm(), ctx.getJceProvider()); PrivateKey pk = (PrivateKey) privateKey; sign.initSign(pk); sign.update(content); byte[] signed = sign.sign(); return signed; } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:ai.susi.tools.JsonSignature.java
public static boolean verify(Map<String, byte[]> obj, PublicKey key) throws SignatureException, InvalidKeyException { if (!obj.containsKey(signatureString)) throw new SignatureException("No signature supplied"); Signature signature; try {/*from www . j a v a 2 s . co m*/ signature = Signature.getInstance("SHA256withRSA"); } catch (NoSuchAlgorithmException e) { return false; //does not happen } byte[] sigString = obj.get(signatureString); byte[] sig = Base64.getDecoder().decode(sigString); obj.remove(signatureString); signature.initVerify(key); signature.update(obj.toString().getBytes(StandardCharsets.UTF_8)); boolean res = signature.verify(sig); obj.put(signatureString, sigString); return res; }
From source file:gemlite.core.util.RSAUtils.java
/** * <p>/* w w w .j a v a2 s . c o m*/ * ????? * </p> * * @param data * ? * @param privateKey * ?(BASE64?) * * @return * @throws Exception */ public static String sign(byte[] data, String privateKey) throws Exception { byte[] keyBytes = Base64Utils.decode(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(privateK); signature.update(data); return Base64Utils.encode(signature.sign()); }
From source file:gemlite.core.util.RSAUtils.java
/** * <p>//from www.jav a 2s.c o m * ?? * </p> * * @param data * ? * @param publicKey * (BASE64?) * @param sign * ?? * * @return * @throws Exception * */ public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { byte[] keyBytes = Base64Utils.decode(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey publicK = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(publicK); signature.update(data); return signature.verify(Base64Utils.decode(sign)); }
From source file:ai.susi.tools.JsonSignature.java
/** * Verfies if the signature of a JSONObject is valid * @param obj the JSONObject/*from w w w. ja va 2 s . c o m*/ * @param key the public key of the signature issuer * @return true if the signature is valid * @throws SignatureException if the JSONObject does not have a signature or something with the JSONObject is bogus * @throws InvalidKeyException if the key is not valid (for example not RSA) */ public static boolean verify(JSONObject obj, PublicKey key) throws SignatureException, InvalidKeyException { if (!obj.has(signatureString)) throw new SignatureException("No signature supplied"); Signature signature; try { signature = Signature.getInstance("SHA256withRSA"); } catch (NoSuchAlgorithmException e) { return false; //does not happen } String sigString = obj.getString(signatureString); byte[] sig = Base64.getDecoder().decode(sigString); obj.remove(signatureString); signature.initVerify(key); signature.update(obj.toString().getBytes(StandardCharsets.UTF_8)); boolean res = signature.verify(sig); obj.put(signatureString, sigString); return res; }
From source file:org.jets3t.service.security.EncryptionUtil.java
/** * Generate an RSA SHA1 signature of the given data using the given private * key DER certificate./*from ww w . j av a 2 s . c om*/ * * Based on example code from: * http://www.java2s.com/Tutorial/Java/0490__Security/RSASignatureGeneration.htm * http://forums.sun.com/thread.jspa?threadID=5175986 * * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws SignatureException * @throws InvalidKeySpecException * @throws NoSuchProviderException */ public static byte[] signWithRsaSha1(byte[] derPrivateKeyBytes, byte[] dataToSign) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, InvalidKeySpecException, NoSuchProviderException { // Build an RSA private key from private key data PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(derPrivateKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec); // Sign data Signature signature = Signature.getInstance("SHA1withRSA", "BC"); signature.initSign(privateKey, new SecureRandom()); signature.update(dataToSign); byte[] signatureBytes = signature.sign(); return signatureBytes; }