List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:edu.internet2.middleware.openid.security.SecurityUtils.java
/** * Calculate signature for specified data using an Association. * //from w w w . j a v a 2s. c om * @param association association * @param data data to calculate signature for * @return calculated signature * @throws SecurityException if unable to calculate the signature */ public static String calculateSignature(Association association, String data) throws SecurityException { log.debug("calculating signature using association: {}", association.getHandle()); log.debug("signature data = {}", data); try { Mac mac = Mac.getInstance(association.getMacKey().getAlgorithm()); mac.init(association.getMacKey()); byte[] rawHmac = mac.doFinal(data.getBytes()); return new String(Base64.encodeBase64(rawHmac)); } catch (InvalidKeyException e) { log.error("Unable to generate MAC - " + e.getMessage()); throw new SecurityException("Unable to generate MAC", e); } catch (NoSuchAlgorithmException e) { log.error("Unable to generate MAC - " + e.getMessage()); throw new SecurityException("Unable to generate MAC", e); } }
From source file:com.grouptuity.venmo.VenmoSDK.java
private static String hash_hmac(String payload, String app_secret, String algorithm) { try {/*ww w . j av a 2s .c om*/ Mac mac = Mac.getInstance(algorithm); SecretKeySpec secret = new SecretKeySpec(app_secret.getBytes(), algorithm); mac.init(secret); byte[] digest = mac.doFinal(payload.getBytes()); String enc = new String(digest); return enc; } catch (Exception e) { Log.d("VenmoSDK Error Message Caught", e.getMessage()); return ""; } }
From source file:org.soyatec.windowsazure.authenticate.SharedKeyCredentials.java
private String computeMacSha(String canonicalizedString) { Mac mac;//www .jav a2 s .c o m try { if (getKey() == null) { throw new StorageClientException(StorageErrorCode.AccountNotFound, "The Windows Azure storage account credentials contains invalid values.", HttpStatusConstant.DEFAULT_STATUS, null, null); } mac = Mac.getInstance(HMACSHA256); mac.init(new SecretKeySpec(getKey(), mac.getAlgorithm())); byte[] dataToMAC = canonicalizedString.getBytes(UTF8_CHARSET); mac.update(dataToMAC); byte[] result = mac.doFinal(); return Base64.encode(result); } catch (NoSuchAlgorithmException e) { Logger.error("NoSuchAlgorithmException", e); } catch (InvalidKeyException e) { Logger.error("InvalidKeyException", e); } catch (UnsupportedEncodingException e) { Logger.error("UnsupportedEncodingException", e); } return null; }
From source file:org.dasein.cloud.aws.AWSCloud.java
static public byte[] HmacSHA256(String data, byte[] key) throws InternalException { final String algorithm = "HmacSHA256"; Mac mac;//from w w w . j av a2s. c o m try { mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(key, algorithm)); return mac.doFinal(data.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { throw new InternalException(e); } catch (InvalidKeyException e) { throw new InternalException(e); } catch (UnsupportedEncodingException e) { throw new InternalException(e); } }
From source file:com.mytwitter.retrofit.RetrofitRequestInterceptor2.java
License:asdf
private String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { SecretKey secretKey = null;// w w w.j a va2 s.c om byte[] keyBytes = keyString.getBytes(); secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = baseString.getBytes(); return new String(Base64.encodeBase64(mac.doFinal(text))).trim(); }
From source file:org.apache.nifi.web.security.otp.OtpService.java
/** * Hashes the specified authentication token. The resulting value will be used as the one time use token. * * @param authenticationToken the authentication token * @return the one time use token *///from w w w. j a va 2s .c om private String hash(final OtpAuthenticationToken authenticationToken) { try { // input is the user identity and timestamp final String input = authenticationToken.getName() + "-" + System.nanoTime(); // create the secret using secure random final SecureRandom secureRandom = new SecureRandom(); final byte[] randomBytes = new byte[32]; secureRandom.nextBytes(randomBytes); final SecretKeySpec secret = new SecretKeySpec(randomBytes, HMAC_SHA256); // 256 bit // hash the input final Mac hmacSha256 = Mac.getInstance(HMAC_SHA256); hmacSha256.init(secret); final byte[] output = hmacSha256.doFinal(input.getBytes(StandardCharsets.UTF_8)); // return the result as a base 64 string return Base64.encodeBase64URLSafeString(output); } catch (final NoSuchAlgorithmException | InvalidKeyException e) { final String errorMessage = "There was an error generating the OTP"; logger.error(errorMessage, e); throw new IllegalStateException("Unable to generate single use token."); } }
From source file:com.klinker.android.twitter.utils.api_helper.TwitterDMPicHelper.java
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { SecretKey secretKey = null;/* w w w. ja v a 2 s .c o m*/ byte[] keyBytes = keyString.getBytes(); secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = baseString.getBytes(); return new String(BASE64Encoder.encode(mac.doFinal(text))).trim(); }
From source file:com.hp.autonomy.hod.client.util.Hmac.java
private byte[] hmacSha1(final String message, final String secret) { try {/*from ww w . ja va 2 s .c o m*/ final Mac mac = Mac.getInstance(HMAC_SHA1); final Key key = new SecretKeySpec(bytesFromString(secret), HMAC_SHA1); mac.init(key); return mac.doFinal(bytesFromString(message)); } catch (final NoSuchAlgorithmException e) { // This should never happen on a sensible JVM throw new AssertionError("HMAC SHA1 is not supported", e); } catch (final InvalidKeyException e) { // In practice, this means that the token secret was invalid throw new IllegalArgumentException("Invalid token secret", e); } }
From source file:org.androdyne.StacktraceUploader.java
/** * Given the NameValuePairs forming a stacktrace submission request, creates a * signature over the parameters that the API should recognize. **///from www . j a v a 2 s . c o m private String createSignature(List<NameValuePair> params) { // First, sort the parameter keys. That'll help later. List<String> sortedKeys = new LinkedList<String>(); for (NameValuePair pair : params) { sortedKeys.add(pair.getName()); } Collections.sort(sortedKeys, String.CASE_INSENSITIVE_ORDER); // Create signature. Mac hmac = null; try { hmac = Mac.getInstance("HmacSHA1"); hmac.init(new SecretKeySpec(mAPISecret.getBytes(), "HmacSHA1")); } catch (NoSuchAlgorithmException ex) { android.util.Log.e(LTAG, "No HmacSHA1 available on this phone."); return null; } catch (InvalidKeyException ex) { android.util.Log.e(LTAG, "Invalid secret; shouldn't be possible."); return null; } final int size = sortedKeys.size(); for (int i = 0; i < size; ++i) { String key = sortedKeys.get(i); for (NameValuePair pair : params) { if (!key.equals(pair.getName())) { continue; } // This pair is next! try { hmac.update(String.format("%s=%s", key, URLEncoder.encode(pair.getValue(), "utf8")).getBytes()); } catch (java.io.UnsupportedEncodingException ex) { android.util.Log.e(LTAG, "URLEncoder reports 'utf8' is an unsupported encoding..."); return null; } if (i < size - 1) { hmac.update("&".getBytes()); } } } String signature = new BigInteger(1, hmac.doFinal()).toString(16); // android.util.Log.d(LTAG, "signature: " + signature); return signature; }
From source file:com.mastfrog.acteur.twitter.TwitterSign.java
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { SecretKey secretKey = null;/* w w w .j ava 2 s . com*/ byte[] keyBytes = keyString.getBytes(); secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = baseString.getBytes(); // return new String(Base64.getEncoder().encode(mac.doFinal(text))).trim(); return new String(Base64.encodeBase64(mac.doFinal(text))).trim(); }