List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:io.pivotal.cla.security.GitHubSignature.java
private byte[] sign(String body, String token) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(token.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey);/*from w w w . j av a 2 s. c o m*/ byte[] expectedBytes = mac.doFinal(body.getBytes()); return expectedBytes; }
From source file:Pusher.java
/** * Returns a HMAC/SHA256 representation of the given string * @param data/*from w ww .java2 s . com*/ * @return */ private static String hmacsha256Representation(String data) { try { // Create the HMAC/SHA256 key from application secret final SecretKeySpec signingKey = new SecretKeySpec(pusherApplicationSecret.getBytes(), "HmacSHA256"); // Create the message authentication code (MAC) final Mac mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); //Process and return data byte[] digest = mac.doFinal(data.getBytes("UTF-8")); digest = mac.doFinal(data.getBytes()); //Convert to string BigInteger bigInteger = new BigInteger(1, digest); return String.format("%0" + (digest.length << 1) + "x", bigInteger); } catch (NoSuchAlgorithmException nsae) { //We should never come here, because GAE has HMac SHA256 throw new RuntimeException("No HMac SHA256 algorithm"); } catch (UnsupportedEncodingException e) { //We should never come here, because UTF-8 should be available throw new RuntimeException("No UTF-8"); } catch (InvalidKeyException e) { throw new RuntimeException("Invalid key exception while converting to HMac SHA256"); } }
From source file:com.alibaba.openapi.client.util.SignatureUtil.java
public static byte[] hmacSha1(String path, List<NameValuePair> parameters, SecretKeySpec signingKey) { Mac mac;// w w w. j ava 2s . c o m try { mac = Mac.getInstance(HMAC_SHA1); mac.init(signingKey); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new IllegalStateException(e.getMessage(), e); } mac.update(path.getBytes(CHARSET_UTF8)); Collections.sort(parameters, new NameValuePairComparator<NameValuePair>()); for (NameValuePair parameter : parameters) { mac.update(parameter.getName().getBytes(CHARSET_UTF8)); mac.update(parameter.getValue().getBytes(CHARSET_UTF8)); } return mac.doFinal(); }
From source file:me.lazerka.gae.jersey.oauth2.facebook.TokenVerifierFacebookSignedRequest.java
public TokenVerifierFacebookSignedRequest(URLFetchService urlFetchService, ObjectMapper jackson, String appId, String appSecret, String redirectUri) { this.jackson = jackson; this.redirectUri = redirectUri; this.fetcher = new FacebookFetcher(appId, appSecret, jackson, urlFetchService); try {/*w ww. jav a 2 s . c o m*/ SecretKeySpec signingKey = new SecretKeySpec(appSecret.getBytes(UTF_8), "HmacSHA1"); hmac = Mac.getInstance("HmacSHA256"); hmac.init(signingKey); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw Throwables.propagate(e); } }
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;/*from w ww . j av a2 s. c om*/ 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:net.datacrow.onlinesearch.amazon.SignedRequestsHelper.java
public SignedRequestsHelper(String awsAccessKeyId, String awsSecretKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { this.awsAccessKeyId = awsAccessKeyId; this.awsSecretKey = awsSecretKey; byte[] secretyKeyBytes = this.awsSecretKey.getBytes(UTF8_CHARSET); this.secretKeySpec = new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM); this.mac = Mac.getInstance(HMAC_SHA256_ALGORITHM); this.mac.init(secretKeySpec); }
From source file:net.unicon.cas.chalkwire.servlet.CasChalkWireHttpServlet.java
private static Mac setKey(final String sharedSecret) throws IOException { try {/*w ww.ja v a2 s. c om*/ final Mac mac = Mac.getInstance("HmacSHA1"); final byte[] keyBytes = sharedSecret.getBytes("UTF8"); final SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); mac.init(signingKey); return mac; } catch (Exception e) { throw new IOException(e); } }
From source file:com.skplanet.syruppay.token.tav.TransactionAuthenticationValue.java
public String getChecksumBy(final String key) throws IOException, InvalidKeyException, NoSuchAlgorithmException { final Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), mac.getAlgorithm())); mac.update((cardToken + mctTransAuthId + ocTransAuthId + new ObjectMapper().writeValueAsString(paymentAuthenticationDetail)).getBytes("UTF-8")); return Base64.encodeBase64URLSafeString(mac.doFinal()); }
From source file:com.github.tojo.session.cookies.SignatureStrategyDefaultImpl.java
@Override public byte[] sign(byte[] sessionData) { assertNotNullAndEmpty(sessionData);/*from w w w. j a v a 2 s . c o m*/ byte[] signature = null; try { Mac mac = Mac.getInstance(HMAC_SHA256); mac.init(key); signature = mac.doFinal(sessionData); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new InitializationException(e); } byte[] signedSessionData = ArrayUtils.addAll(signature, sessionData); return signedSessionData; }
From source file:com.livily.pusher.Pusher.java
/** * Returns a HMAC/SHA256 representation of the given string * //w w w. j av a2s . c o m * @param data * @return */ private static String hmacsha256Representation(String data) { try { // Create the HMAC/SHA256 key from application secret final SecretKeySpec signingKey = new SecretKeySpec(pusherApplicationSecret.getBytes(), "HmacSHA256"); // Create the message authentication code (MAC) final Mac mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); // Process and return data byte[] digest = mac.doFinal(data.getBytes("UTF-8")); digest = mac.doFinal(data.getBytes()); // Convert to string BigInteger bigInteger = new BigInteger(1, digest); return String.format("%0" + (digest.length << 1) + "x", bigInteger); } catch (NoSuchAlgorithmException nsae) { // We should never come here, because GAE has HMac SHA256 throw new RuntimeException("No HMac SHA256 algorithm"); } catch (UnsupportedEncodingException e) { // We should never come here, because UTF-8 should be available throw new RuntimeException("No UTF-8"); } catch (InvalidKeyException e) { throw new RuntimeException("Invalid key exception while converting to HMac SHA256"); } }