List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:lti.oauth.OAuthMessageSigner.java
/** * This method double encodes the parameter keys and values. * Thus, it expects the keys and values contained in the 'parameters' SortedMap * NOT to be encoded.//from ww w . j a v a 2 s.com * * @param secret * @param algorithm * @param method * @param url * @param parameters * @return oauth signature * @throws Exception */ public String sign(String secret, String algorithm, String method, String url, SortedMap<String, String> parameters) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec((secret.concat(OAuthUtil.AMPERSAND)).getBytes(), algorithm); Mac mac = Mac.getInstance(secretKeySpec.getAlgorithm()); mac.init(secretKeySpec); StringBuilder signatureBase = new StringBuilder(OAuthUtil.percentEncode(method)); signatureBase.append(OAuthUtil.AMPERSAND); signatureBase.append(OAuthUtil.percentEncode(url)); signatureBase.append(OAuthUtil.AMPERSAND); int count = 0; for (String key : parameters.keySet()) { count++; signatureBase.append(OAuthUtil.percentEncode(OAuthUtil.percentEncode(key))); signatureBase.append(URLEncoder.encode(OAuthUtil.EQUAL, OAuthUtil.ENCODING)); signatureBase.append(OAuthUtil.percentEncode(OAuthUtil.percentEncode(parameters.get(key)))); if (count < parameters.size()) { signatureBase.append(URLEncoder.encode(OAuthUtil.AMPERSAND, OAuthUtil.ENCODING)); } } if (log.isDebugEnabled()) { log.debug(signatureBase.toString()); } byte[] bytes = mac.doFinal(signatureBase.toString().getBytes()); byte[] encodedMacBytes = Base64.encodeBase64(bytes); return new String(encodedMacBytes); }
From source file:com.cloud.stack.CloudStackCommand.java
private String calculateRFC2104HMAC(String signIt, String secretKey) throws SignatureException { String result = null;// w ww . j a va 2s . c o m 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) { throw new SignatureException("Failed to generate keyed HMAC on soap request: " + e.getMessage()); } return result.trim(); }
From source file:com.kolich.havalo.client.signing.algorithms.HMACSHA256Signer.java
/** * Returns a Base-64 encoded HMAC-SHA256 signature. *///from ww w . ja v a2 s .c om @Override public String sign(final HavaloCredentials credentials, final String input) { String result = null; try { // Get a new instance of the HMAC-SHA256 algorithm. final Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM_NAME); // Init it with our secret and the secret-key algorithm. mac.init(new SecretKeySpec(getBytesUtf8(credentials.getSecret()), HMAC_SHA256_ALGORITHM_NAME)); // Sign the input. result = newStringUtf8(encodeBase64(mac.doFinal(getBytesUtf8(input)))); } catch (Exception e) { throw new HavaloClientException("Failed to SHA-256 sign input " + "string: " + input, e); } return result; }
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.jav a2 s .co 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(Base64.encodeBase64(mac.doFinal(text))).trim(); }
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); String customerOrderString = customerId + orderId; byte[] publicBytes = sha1Mac.doFinal(customerOrderString.getBytes()); String publicDigest = encoder.encodeToString(publicBytes); return publicDigest.replaceAll("\\r|\\n", ""); }
From source file:de.hybris.platform.acceleratorservices.payment.utils.impl.DefaultAcceleratorDigestUtils.java
@Override public String getPublicDigest(final String customValues, final String key) throws NoSuchAlgorithmException, InvalidKeyException { final Base64 encoder = new Base64(); final Mac sha1Mac = Mac.getInstance(getMacAlgorithm()); final SecretKeySpec publicKeySpec = new SecretKeySpec(key.getBytes(), getMacAlgorithm()); sha1Mac.init(publicKeySpec); final byte[] publicBytes = sha1Mac.doFinal(customValues.getBytes()); final String publicDigest = new String(encoder.encode(publicBytes)); return publicDigest.replaceAll("\n", ""); }
From source file:com.microsoft.azure.batch.auth.BatchCredentialsInterceptor.java
private String sign(String accessKey, String stringToSign) { try {/*from ww w.j a v a 2s.c o m*/ // Encoding the Signature // Signature=Base64(HMAC-SHA256(UTF8(StringToSign))) Mac hmac = Mac.getInstance("hmacSHA256"); hmac.init(new SecretKeySpec(Base64.decodeBase64(accessKey), "hmacSHA256")); byte[] digest = hmac.doFinal(stringToSign.getBytes("UTF-8")); return Base64.encodeBase64String(digest); } catch (Exception e) { throw new IllegalArgumentException("accessKey", 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.skplanet.syruppay.token.tav.TransactionAuthenticationValue.java
public boolean isValidBy(final String key, final String checksum) throws NoSuchAlgorithmException, IOException, InvalidKeyException { 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()).equals(checksum); }
From source file:id.pazpo.agent.utils.OAuthHeaderBuilder.java
private String generateSignature(String signatureBase, String accessTokenSecret) throws InvalidKeyException, NoSuchAlgorithmException { Mac mac = Mac.getInstance(ENCRYPTION_ALGO); mac.init(new SecretKeySpec((CONSUMER_SECRET + "&" + accessTokenSecret).getBytes(), ENCRYPTION_ALGO)); mac.update(signatureBase.getBytes()); byte[] res = mac.doFinal(); String signature = new String(Base64.encodeBase64(res)).trim(); Log.d("headers", signature); return signature; }