List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:com.monarchapis.client.authentication.HawkV1RequestProcessor.java
private String getHawkHeader(BaseClient<?> client, String accessToken, String payloadHash, String extData) { try {//from w w w. java2 s . c om StringBuilder sb = new StringBuilder(); long ts = System.currentTimeMillis() / 1000; String nonce = RandomStringUtils.randomAlphanumeric(6); URI uri = URI.create(client.getUrl()); sb.append("hawk.1.header\n"); sb.append(ts); sb.append("\n"); sb.append(nonce); sb.append("\n"); sb.append(client.getMethod()); sb.append("\n"); sb.append(uri.getRawPath()); sb.append("\n"); sb.append(uri.getHost()); sb.append("\n"); sb.append(uri.getPort()); sb.append("\n"); if (payloadHash != null) { sb.append(payloadHash); } sb.append("\n"); if (extData != null) { sb.append(extData); } sb.append("\n"); if (accessToken != null) { sb.append(apiKey); sb.append("\n"); } String stringData = sb.toString(); String algo = HmacUtils.getHMacAlgorithm(algorithm); byte[] key = sharedSecret.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(key, algo); Mac mac256 = Mac.getInstance(algo); mac256.init(signingKey); // compute the hmac on input data bytes byte[] hash = mac256.doFinal(stringData.getBytes("UTF-8")); String mac = Base64.encodeBase64String(hash); return "Hawk id=\"" + (accessToken != null ? accessToken : apiKey) + "\", ts=\"" + ts + "\", nonce=\"" + nonce + "\"" + (payloadHash != null ? ", hash=\"" + payloadHash + "\"" : "") + (extData != null ? ", ext=\"" + extData + "\"," : "") + ", mac=\"" + mac + "\"" + (accessToken != null ? ", app=\"" + apiKey + "\"" : ""); } catch (Exception e) { throw new RuntimeException("Could not create hawk header", e); } }
From source file:ch.cyberduck.core.openstack.SwiftUrlProvider.java
protected String sign(final String secret, final String body) { try {// w w w .j a v a 2s.c o m // Acquire an HMAC/SHA1 from the raw key bytes. final SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(Charset.forName("UTF-8")), Constants.HMAC_SHA1_ALGORITHM); // Acquire the MAC instance and initialize with the signing key. final Mac mac = Mac.getInstance(Constants.HMAC_SHA1_ALGORITHM); mac.init(signingKey); return Hex.encodeHexString(mac.doFinal(body.getBytes(Charset.forName("UTF-8")))); } catch (NoSuchAlgorithmException | InvalidKeyException e) { log.error(String.format("Error signing %s %s", body, e.getMessage())); return null; } }
From source file:org.jupyterkernel.kernel.MessageObject.java
private byte[] computeSignature(byte[] header, byte[] parent, byte[] meta, byte[] content) { byte[][] data = { header, parent, meta, content }; try {/* w w w. j a v a 2 s. co m*/ SecretKeySpec keySpec = new SecretKeySpec(key, "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(keySpec); for (int i = 0; i < 4; i++) { mac.update(data[i]); } return mac.doFinal(); } catch (InvalidKeyException | NoSuchAlgorithmException e) { System.out.println(e.getMessage()); } return null; }
From source file:fitmon.DietAPI.java
public ArrayList<Food> getFood(String foodID) throws ClientProtocolException, IOException, NoSuchAlgorithmException, InvalidKeyException, SAXException, ParserConfigurationException { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://platform.fatsecret.com/rest/server.api&" + "oauth_consumer_key=5f2d9f7c250c4d75b9807a4f969363a7&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1408230438&" + "oauth_nonce=abc&oauth_version=1.0&method=food.get&food_id=33691"); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String base = URLEncoder.encode("GET") + "&"; base += "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&"; String params;/*ww w .ja va 2s . c o m*/ params = "food_id=" + foodID + "&"; params += "method=food.get&"; params += "oauth_consumer_key=5f2d9f7c250c4d75b9807a4f969363a7&"; // ur consumer key params += "oauth_nonce=123&"; params += "oauth_signature_method=HMAC-SHA1&"; Date date = new java.util.Date(); Timestamp ts = new Timestamp(date.getTime()); params += "oauth_timestamp=" + ts.getTime() + "&"; params += "oauth_version=1.0"; //params += "search_expression=apple"; String params2 = URLEncoder.encode(params); base += params2; System.out.println(base); String line = ""; String secret = "76172de2330a4e55b90cbd2eb44f8c63&"; Mac sha256_HMAC = Mac.getInstance("HMACSHA1"); SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HMACSHA1"); sha256_HMAC.init(secret_key); String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(base.getBytes())); //$url = "http://platform.fatsecret.com/rest/server.api?".$params."&oauth_signature=".rawurlencode($sig); String url = "http://platform.fatsecret.com/rest/server.api?" + params + "&oauth_signature=" + URLEncoder.encode(hash); System.out.println(url); xmlParser xParser = new xmlParser(); ArrayList<Food> foodList = xParser.Parser(url); return foodList; //while ((line = rd.readLine()) != null) { // System.out.println(line); //} }
From source file:com.chbase.android.simplexml.SodaAuthenticator.java
/** * Sign content./*from w w w.java 2s . c o m*/ * * @param content the content * * @return the byte[] */ private byte[] signContent(byte[] content) { try { SecretKeySpec sks = new SecretKeySpec(authenticationSecret, "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(sks); return mac.doFinal(content); } catch (Exception e) { throw new HVSystemException("Could not sign request", e); } }
From source file:org.akvo.flow.api.FlowApi.java
private String getAuthorization(String query) { String authorization = null;// w w w . j a va2s . co m try { SecretKeySpec signingKey = new SecretKeySpec(API_KEY.getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); byte[] rawHmac = mac.doFinal(query.getBytes()); authorization = Base64.encodeToString(rawHmac, Base64.DEFAULT); } catch (NoSuchAlgorithmException e) { Log.e(TAG, e.getMessage()); } catch (InvalidKeyException e) { Log.e(TAG, e.getMessage()); } return authorization; }
From source file:lumbermill.internal.aws.AWSV4SignerImpl.java
private byte[] hmacSHA256(String data, byte[] key) { try {/* w ww . j a v a 2 s . com*/ final Mac mac = Mac.getInstance(HMAC_SHA256); mac.init(new SecretKeySpec(key, HMAC_SHA256)); return mac.doFinal(data.getBytes(Charsets.UTF_8)); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw Throwables.propagate(e); } }
From source file:org.broadleafcommerce.vendor.authorizenet.service.payment.AuthorizeNetCheckoutServiceImpl.java
@Override public String createTamperProofSeal(String customerId, String orderId) throws NoSuchAlgorithmException, InvalidKeyException { String transactionKey = configuration.getTransactionKey(); Base64 encoder = new Base64(); Mac sha1Mac = Mac.getInstance("HmacSHA1"); SecretKeySpec publicKeySpec = new SecretKeySpec(transactionKey.getBytes(), "HmacSHA1"); sha1Mac.init(publicKeySpec); String customerOrderString = customerId + orderId; byte[] publicBytes = sha1Mac.doFinal(customerOrderString.getBytes()); String publicDigest = encoder.encodeToString(publicBytes); return publicDigest.replaceAll("\\r|\\n", ""); }
From source file:mecard.security.PAPISecurity.java
String getHash(String accessKey, String data) { String result = ""; // Get an hmac_sha1 key from the raw key bytes byte[] secretBytes = accessKey.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(secretBytes, HMAC_SHA1_ALGORITHM); // Get an hmac_sha1 Mac instance and initialize with the signing key try {/*from w w w .j a v a 2 s . c o m*/ Mac mac; mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); // Convert raw bytes to Hex result = Base64.encodeBase64String(rawHmac); } catch (NoSuchAlgorithmException | InvalidKeyException e1) { System.out.println(new Date() + e1.getMessage()); throw new ConfigurationException("The user key is invalid."); } return result; }
From source file:edu.ucsb.eucalyptus.admin.server.extensions.store.SignatureGenerator.java
public String getSignature(String secretKey) { Mac mac; try {/*from w ww . jav a2s. c o m*/ mac = Mac.getInstance(ALGORITHM); mac.init(new SecretKeySpec(secretKey.getBytes(), ALGORITHM)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } mac.update(method.getBytes()); mac.update((byte) '\n'); mac.update(host.getBytes()); mac.update((byte) '\n'); mac.update(path.getBytes()); mac.update((byte) '\n'); boolean addAmpersand = false; for (Map.Entry<String, List<String>> entry : parameters.entrySet()) { byte[] nameBytes = encodeString(entry.getKey()); List<String> values = entry.getValue(); Collections.sort(values); for (String value : values) { if (addAmpersand) { mac.update((byte) '&'); } else { addAmpersand = true; } byte[] valueBytes = encodeString(value); mac.update(nameBytes); mac.update((byte) '='); mac.update(valueBytes); } } byte[] digest = mac.doFinal(); return new String(Base64.encodeBase64(digest)); }