List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:net.shopxx.plugin.yeepayPayment.YeepayPaymentPlugin.java
private String hmacDigest(String value, String key) { try {//from w ww. j ava 2 s . com Mac mac = Mac.getInstance("HmacMD5"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacMD5")); byte[] bytes = mac.doFinal(value.getBytes("UTF-8")); StringBuilder digest = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { digest.append("0"); } digest.append(hex); } return digest.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:com.runtimecollective.influence.metrics.Alexa.java
/** * Computes RFC 2104-compliant HMAC signature. * /*w w w . j ava2s . co m*/ * @param data * The data to be signed. * @param key * The signing key. * @return * The Base64-encoded RFC 2104-compliant HMAC signature. * @throws * java.security.SignatureException when signature generation fails */ private String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException { String result; try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); // base64-encode the hmac result = new sun.misc.BASE64Encoder().encode(rawHmac); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }
From source file:ch.cyberduck.core.openstack.SwiftUrlProvider.java
protected String sign(final String secret, final String body) { try {// w w w. j av a 2 s. c o m // Acquire an HMAC/SHA1 from the raw key bytes. final SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(Charset.forName("UTF-8")), Constants.HMAC_SHA1_ALGORITHM); // Acquire the MAC instance and initialize with the signing key. final Mac mac = Mac.getInstance(Constants.HMAC_SHA1_ALGORITHM); mac.init(signingKey); return Hex.encodeHexString(mac.doFinal(body.getBytes(Charset.forName("UTF-8")))); } catch (NoSuchAlgorithmException | InvalidKeyException e) { log.error(String.format("Error signing %s %s", body, e.getMessage())); return null; } }
From source file:org.akvo.flow.api.FlowApi.java
private String getAuthorization(String query) { String authorization = null;//from w ww .j av a2 s . co m try { SecretKeySpec signingKey = new SecretKeySpec(API_KEY.getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); byte[] rawHmac = mac.doFinal(query.getBytes()); authorization = Base64.encodeToString(rawHmac, Base64.DEFAULT); } catch (NoSuchAlgorithmException e) { Log.e(TAG, e.getMessage()); } catch (InvalidKeyException e) { Log.e(TAG, e.getMessage()); } return authorization; }
From source file:com.zimbra.common.auth.twofactor.TOTPAuthenticator.java
private byte[] calculateHash(byte[] K, byte[] C) throws ServiceException { try {//from w w w. j a v a2 s . c o m Mac mac = Mac.getInstance(config.getHashAlgorithm().getLabel()); mac.init(new SecretKeySpec(K, config.getHashAlgorithm().getLabel())); byte[] hash = mac.doFinal(C); return hash; } catch (NoSuchAlgorithmException e) { throw ServiceException.FAILURE("no such algorithm", e); } catch (InvalidKeyException e) { throw ServiceException.FAILURE("invalid key", e); } }
From source file:com.muk.ext.security.impl.DefaultNonceService.java
private String hash(String salt, String payload) throws NoSuchAlgorithmException, InvalidKeyException { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(salt.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key);// ww w . jav a 2 s. co m return Base64.encodeBase64URLSafeString(sha256_HMAC.doFinal(payload.getBytes())); }
From source file:com.mytwitter.retrofit.RetrofitRequestInterceptor2.java
License:asdf
private String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { SecretKey secretKey = null;//from w w w . j a v a 2s .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(Base64.encodeBase64(mac.doFinal(text))).trim(); }
From source file:com.appdynamics.cloudstack.CloudStackApiClient.java
public String calculateRFC2104HMAC(String data, String key) throws Exception { String result;/*from w ww. jav a 2s. c om*/ try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1"); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); // base64-encode the hmac // result = Base64.encode(rawHmac); result = new String(Base64.encodeBase64(rawHmac)); return result.trim(); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } // return encodeUrl(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);//from w w w. j ava 2 s . co m String customerOrderString = customerId + orderId; byte[] publicBytes = sha1Mac.doFinal(customerOrderString.getBytes()); String publicDigest = encoder.encodeToString(publicBytes); return publicDigest.replaceAll("\\r|\\n", ""); }
From source file:net.groupbuy.plugin.yeepay.YeepayPlugin.java
/** * Hmac//w w w. jav a 2 s .c om * * @param value * * @param key * * @return */ private String hmacDigest(String value, String key) { try { Mac mac = Mac.getInstance("HmacMD5"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacMD5")); byte[] bytes = mac.doFinal(value.getBytes("UTF-8")); StringBuffer digest = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { digest.append("0"); } digest.append(hex); } return digest.toString(); } catch (Exception e) { return null; } }