List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:com.streamreduce.util.AWSClient.java
private String calculateS3Signature(String rawUrl, String key, String date) { StringBuilder canonical = new StringBuilder(); try {//w w w .j av a 2 s . co m canonical.append("GET").append("\n"); canonical.append("\n"); canonical.append("\n"); canonical.append(date).append("\n"); canonical.append(rawUrl); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1")); byte[] sign = mac.doFinal(canonical.toString().getBytes("UTF-8")); return new String(Base64.encodeBase64(sign)); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.streamreduce.util.AWSClient.java
private String calculateSignature(String rawUrl, String key, Map<String, String> params) { StringBuilder canonical = new StringBuilder(); try {//from www . j ava2 s . com canonical.append("GET").append("\n"); canonical.append(new URL(rawUrl).getHost()).append("\n"); canonical.append("/").append("\n"); SortedMap<String, String> sorted = new TreeMap<>(params); canonical.append(getQueryString(sorted)); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256")); byte[] sign = mac.doFinal(canonical.toString().getBytes("UTF-8")); return new String(Base64.encodeBase64(sign)); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.flozano.socialauth.util.OAuthConsumer.java
private String getHMACSHA1(final String method, final String url, final Map<String, String> args, final AccessGrant token) throws Exception { if (config.get_consumerSecret().length() == 0) { throw new SignatureException("Please check consumer secret"); }/*from w ww . jav a2s. c o m*/ boolean valid = MethodType.GET.toString().equals(method) || MethodType.PUT.toString().equals(method) || MethodType.POST.toString().equals(method); if (!valid) { throw new SignatureException("Invalid method type :" + method); } if (url.length() == 0) { throw new SignatureException("Please check URL"); } String key = HttpUtil.encodeURIComponent(config.get_consumerSecret()) + "&"; if (token != null && token.getSecret() != null) { key += HttpUtil.encodeURIComponent(token.getSecret()); } try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), "HMAC-SHA1"); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); String data = HttpUtil.encodeURIComponent(method) + "&" + HttpUtil.encodeURIComponent(url) + "&" + HttpUtil.encodeURIComponent(HttpUtil.buildParams(args)); LOG.debug("Signature data : " + data); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8")); // base64-encode the hmac LOG.debug("Encoding raw HMAC to Base64"); String sig = Base64.encodeBytes(rawHmac); return sig; } catch (Exception e) { throw new SignatureException("Unable to generate HMAC-SHA1", e); } }
From source file:jp.co.yahoo.yconnect.core.oidc.JWTVerification.java
/** * HMAC-SHA256????????????// ww w. ja va 2 s .c o m * * @return ????? */ private String generateSignature() { // mac??? Mac sha256_HMAC = null; try { sha256_HMAC = Mac.getInstance("HmacSHA256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } // secret_key?? SecretKeySpec secret_key = null; try { secret_key = new SecretKeySpec(this.clientSecret.getBytes("UTF-8"), "HmacSHA256"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // mac?? try { sha256_HMAC.init(secret_key); } catch (InvalidKeyException e) { e.printStackTrace(); } String[] idTokenArray = this.idTokenString.split("\\."); this.dataPart = idTokenArray[0] + "." + idTokenArray[1]; String hash = null; try { hash = Base64.encodeBase64String(sha256_HMAC.doFinal(this.dataPart.getBytes("UTF-8"))); } catch (IllegalStateException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // URLSafe?Base64?? hash = hash.replace("=", ""); hash = hash.replace("+", "-"); hash = hash.replace("/", "_"); return hash; }
From source file:org.instagram4j.DefaultInstagramClient.java
private void setEnforceHeader(HttpRequestBase method) { if (!isSignedHeaderEnabled()) return;// w w w . j av a 2 s .c o m if (clientSecret == null) throw new IllegalStateException("Client secret it required to use signed header"); if (clientIps == null || clientIps.length() == 0) throw new IllegalStateException("Client IP(s) required to use signed header"); try { SecretKeySpec signingKey = new SecretKeySpec(getClientSecret().getBytes(), HMAC_SHA256_ALGO); Mac mac = Mac.getInstance(HMAC_SHA256_ALGO); mac.init(signingKey); // Compute the hmac on IP address. byte[] rawHmac = mac.doFinal(clientIps.getBytes()); String digest = Hex.encodeHexString(rawHmac); method.setHeader("X-Insta-Forwarded-For", String.format("%s|%s", clientIps, digest)); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Unexpected error creating signed header using HMAC-SHA256", e); } catch (InvalidKeyException e) { throw new IllegalStateException("Unexpected error creating signed header using HMAC-SHA256", e); } }
From source file:TimestreamsTests.java
/** * Generate an HMAC Based on example://from w w w. j av a 2s .c o m * http://stackoverflow.com/questions/6312544 * /hmac-sha1-how-to-do-it-properly-in-java * * @param value * is a String to hash * @param key * is the private key to hash with# * @param type * is the Mac format to use such as HmacSHA256 * @return The hmac */ private String hmacString(String value, String key, String type) { try { byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, type); // Get a Mac instance and initialize with the signing key Mac mac = Mac.getInstance(type); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { fail = e.getLocalizedMessage(); return null; } }
From source file:org.dasein.cloud.skeleton.RESTMethod.java
private @Nonnull String getSignature(@Nonnull byte[] accessKey, @Nonnull byte[] privateKey, @Nonnull String method, @Nonnull String resource, @Nullable String id, long timestamp) throws InternalException { // TODO: implement this for real; this is the signature code for the enStratus API (sorta) try {//from w w w . j a v a 2s. com String stringToSign = (new String(accessKey, "utf-8")) + ":" + method; if (resource.startsWith("/")) { stringToSign = stringToSign + resource; } else { stringToSign = stringToSign + "/" + resource; } if (id != null) { if (id.startsWith("/")) { stringToSign = stringToSign + id; } else { stringToSign = stringToSign + "/" + id; } } stringToSign = stringToSign + ":" + timestamp + ":Dasein Cloud"; try { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(privateKey, "HmacSHA256")); return new String(Base64.encodeBase64(mac.doFinal(stringToSign.getBytes("utf-8")))); } catch (NoSuchAlgorithmException e) { throw new InternalException(e); } catch (InvalidKeyException e) { throw new InternalException(e); } catch (UnsupportedEncodingException e) { throw new InternalException(e); } } catch (UnsupportedEncodingException e) { throw new InternalException(e); } }
From source file:com.lili.ylpay.sdk.SecureUtil.java
/** * MAC/* ww w . j av a2 s . com*/ * * @param inputByte * ? * @param inputkey * * @param inputmac * MAC * @return * @throws Exception */ public boolean checkmac(byte[] inputByte, byte[] inputkey, String inputmac) throws Exception { try { Mac mac = Mac.getInstance("HmacMD5"); SecretKey key = new SecretKeySpec(inputkey, "DES"); mac.init(key); byte[] macCode = mac.doFinal(inputByte); String strMacCode = this.byte2hex(macCode); return strMacCode.equals(inputmac); } catch (Exception ex) { throw ex; } }
From source file:acp.sdk.SecureUtil.java
/** * MAC/*from ww w .ja v a 2 s . c om*/ * * @param inputByte * ? * @param inputkey * * @return MAC * @throws Exception */ public String genmac(byte[] inputByte, byte[] inputkey) throws Exception { try { Mac mac = Mac.getInstance("HmacMD5"); SecretKey key = new SecretKeySpec(inputkey, "DES"); mac.init(key); byte[] macCode = mac.doFinal(inputByte); String strMac = this.byte2hex(macCode); return strMac; } catch (Exception ex) { ex.printStackTrace(); throw ex; } }
From source file:acp.sdk.SecureUtil.java
/** * MAC// w w w . j a v a 2 s. c o m * * @param inputByte * ? * @param inputkey * * @param inputmac * MAC * @return * @throws Exception */ public boolean checkmac(byte[] inputByte, byte[] inputkey, String inputmac) throws Exception { try { Mac mac = Mac.getInstance("HmacMD5"); SecretKey key = new SecretKeySpec(inputkey, "DES"); mac.init(key); byte[] macCode = mac.doFinal(inputByte); String strMacCode = this.byte2hex(macCode); if (strMacCode.equals(inputmac)) { return true; } else { return false; } } catch (Exception ex) { throw ex; } }