List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
private static String generateSignature(String base, String type, String nonce, String timestamp, String token, String tokenSecret, String verifier, ArrayList<String> parameters) throws NoSuchAlgorithmException, InvalidKeyException { String encodedBase = Uri.encode(base); StringBuilder builder = new StringBuilder(); //Create an array of all the parameters //So that we can sort them //OAuth requires that we sort all parameters ArrayList<String> sortingArray = new ArrayList<String>(); sortingArray.add("oauth_consumer_key=" + oauthConsumerKey); sortingArray.add("oauth_nonce=" + nonce); sortingArray.add("oauth_signature_method=HMAC-SHA1"); sortingArray.add("oauth_timestamp=" + timestamp); sortingArray.add("oauth_version=1.0"); if (parameters != null) { sortingArray.addAll(parameters); }/*from w ww . j a v a 2 s . c o m*/ if (token != "" && token != null) { sortingArray.add("oauth_token=" + token); } if (verifier != "" && verifier != null) { sortingArray.add("oauth_verifier=" + verifier); } Collections.sort(sortingArray); //Append all parameters to the builder in the right order for (int i = 0; i < sortingArray.size(); i++) { if (i > 0) builder.append("&" + sortingArray.get(i)); else builder.append(sortingArray.get(i)); } String params = builder.toString(); //Percent encoded the whole url String encodedParams = Uri.encode(params); String completeUrl = type + "&" + encodedBase + "&" + encodedParams; String completeSecret = oauthSecretKey; if (tokenSecret != null && tokenSecret != "") { completeSecret = completeSecret + "&" + tokenSecret; } else { completeSecret = completeSecret + "&"; } Log.v("Complete URL: ", completeUrl); Log.v("Complete Key: ", completeSecret); Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(completeSecret.getBytes(), mac.getAlgorithm()); mac.init(secret); byte[] sig = mac.doFinal(completeUrl.getBytes()); String signature = Base64.encodeToString(sig, 0); signature = signature.replace("+", "%2b"); //Specifically encode all +s to %2b return signature; }
From source file:com.cloud.bridge.util.S3SoapAuth.java
/** * Create a signature by the following method: * new String( Base64( SHA1( key, byte array ))) * /*from w w w.j a va2s .com*/ * @param signIt - the data to generate a keyed HMAC over * @param secretKey - the user's unique key for the HMAC operation * @return String - the recalculated string */ private static String calculateRFC2104HMAC(String signIt, String secretKey) { String result = null; try { SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1"); Mac hmacSha1 = Mac.getInstance("HmacSHA1"); hmacSha1.init(key); byte[] rawHmac = hmacSha1.doFinal(signIt.getBytes()); result = new String(Base64.encodeBase64(rawHmac)); } catch (Exception e) { logger.error("Failed to generate keyed HMAC on soap request: " + e.getMessage()); return null; } return result.trim(); }
From source file:com.moha.demo.utils.Hashsalt.java
public String encrypt(String password) { String algorithm = EnvUtils.getProperty("algorithm"); String keyString = EnvUtils.getProperty("keyString"); SecretKey key = new SecretKeySpec(keyString.getBytes(), algorithm); try {/*from w w w .j a v a2s . c o m*/ Mac m = Mac.getInstance(algorithm); m.init(key); m.update(password.getBytes()); byte[] mac = m.doFinal(); return toHexString(mac); } catch (Exception e) { System.out.println(e.toString()); } return StringUtils.EMPTY; }
From source file:com.k42b3.aletheia.oauth.HMACSHA1.java
public String build(String baseString, String consumerSecret, String tokenSecret) { try {// w w w . j a v a 2s .co m String key = Oauth.urlEncode(consumerSecret) + "&" + Oauth.urlEncode(tokenSecret); Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(key.getBytes(), "HmacSHA1"); mac.init(secret); byte[] result = mac.doFinal(baseString.getBytes()); return Base64.encodeBase64String(result); } catch (Exception e) { Aletheia.handleException(e); return null; } }
From source file:com.k42b3.neodym.oauth.HMACSHA1.java
public String build(String baseString, String consumerSecret, String tokenSecret) throws Exception { String key = Oauth.urlEncode(consumerSecret) + "&" + Oauth.urlEncode(tokenSecret); Charset charset = Charset.defaultCharset(); SecretKey sk = new SecretKeySpec(key.getBytes(charset), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(sk);//from ww w . ja v a2 s . c om byte[] result = mac.doFinal(baseString.getBytes(charset)); return Base64.encodeBase64String(result); }
From source file:org.broadleafcommerce.common.payment.service.PaymentGatewayTamperProofSealServiceImpl.java
@Override public String createTamperProofSeal(String secretKey, String customerId, String orderId) throws NoSuchAlgorithmException, InvalidKeyException { //Create a URL-Safe Base64 encoder as some of these may get passed back as URL GET parameters Base64 encoder = new Base64(true); Mac sha1Mac = Mac.getInstance("HmacSHA1"); SecretKeySpec publicKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1"); sha1Mac.init(publicKeySpec);// w w w . j a v a 2 s .c om String customerOrderString = customerId + orderId; byte[] publicBytes = sha1Mac.doFinal(customerOrderString.getBytes()); String publicDigest = encoder.encodeToString(publicBytes); return publicDigest.replaceAll("\\r|\\n", ""); }
From source file:com.ibm.sbt.security.encryption.HMACEncryptionUtility.java
public static String generateHMACSignature(String apiUrl, String method, String consumerSecret, String tokenSecret, Map<String, String> paramsSortedMap) throws OAuthException { try {//from www . ja v a 2 s. c o m String parameterString = generateParameterString(paramsSortedMap); String signature_base_string = generateSignatureBaseString(method, apiUrl, parameterString); String signingKey = null; if (StringUtil.isEmpty(tokenSecret)) { // No token secret is available when call is made from getRequestToken, tokensecret is fetched // later in OADance signingKey = consumerSecret + "&"; } else { signingKey = consumerSecret + "&" + tokenSecret; } byte[] keyBytes = null; try { keyBytes = signingKey.getBytes(Configuration.ENCODING); } catch (UnsupportedEncodingException e) { throw new OAuthException(e, "HMACEncryptionUtility : generateHMACSignature caused UnsupportedEncodingException exception"); } SecretKey secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = null; try { text = signature_base_string.getBytes(Configuration.ENCODING); } catch (UnsupportedEncodingException e) { throw new OAuthException(e, "HMACEncryptionUtility : generateHMACSignature caused UnsupportedEncodingException exception"); } String signature = new String(Base64.encodeBase64(mac.doFinal(text))).trim(); return signature; } catch (NoSuchAlgorithmException e) { throw new OAuthException(e, "HMACEncryptionUtility : generateHMACSignature caused NoSuchAlgorithmException exception"); } catch (InvalidKeyException e) { throw new OAuthException(e, "HMACEncryptionUtility : generateHMACSignature caused InvalidKeyException exception"); } }
From source file:com.threatconnect.sdk.conn.ConnectionUtil.java
public static String getHmacSha256Signature(String signature, String apiSecretKey) { try {//from www .java 2 s . com String calculatedSignature; SecretKeySpec spec = new SecretKeySpec(apiSecretKey.getBytes(), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(spec); byte[] rawSignature = mac.doFinal(signature.getBytes()); calculatedSignature = Base64.encodeBase64String(rawSignature); return calculatedSignature; } catch (NoSuchAlgorithmException | InvalidKeyException | IllegalStateException ex) { logger.error("Error creating HMAC SHA256 signature", ex); return null; } }
From source file:org.apache.vysper.xmpp.modules.extension.xep0220_server_dailback.DialbackIdGenerator.java
public DialbackIdGenerator() { try {//w w w.ja va 2 s . co m mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } }
From source file:cn.ucloud.sdk.utils.EncoderUtils.java
/** * SHA1/* w ww .j a va 2 s. c om*/ * @param key * @param str * @return */ public static String sha1(String key, String str) { String algorithm = "HmacSHA1"; String result = null; try { SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(keySpec); result = getFormattedText(mac.doFinal(str.getBytes("UTF-8"))); } catch (Exception e) { LogUtils.exception(logger, e); } return result; }