List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.quantil.http.HttpProcessor.java
private String createKey() throws Exception { SimpleDateFormat formatter;/*from w w w.j av a 2s. c o m*/ formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); currentDate = formatter.format(new Date()); SecretKeySpec signingKey = new SecretKeySpec(pass.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(currentDate.getBytes()); Base64 b64 = new Base64(); String pas = user + ":" + new String(b64.encode(rawHmac), "UTF-8"); return new String(b64.encode(pas.getBytes()), "UTF-8"); }
From source file:com.ec2box.manage.util.OTPUtil.java
/** * verifies code for OTP secret per time interval * * @param secret shared secret/* www . j a v a2 s . co m*/ * @param token verification token * @param time time representation to calculate OTP * @return true if success */ private static boolean verifyToken(String secret, long token, long time) { long calculated = -1; byte[] key = new Base32().decode(secret); SecretKeySpec secretKey = new SecretKeySpec(key, "HmacSHA1"); try { Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] hash = mac.doFinal(ByteBuffer.allocate(8).putLong(time).array()); int offset = hash[hash.length - 1] & 0xF; for (int i = 0; i < 4; ++i) { calculated <<= 8; calculated |= (hash[offset + i] & 0xFF); } calculated &= 0x7FFFFFFF; calculated %= 1000000; } catch (Exception ex) { log.error(ex.toString(), ex); } return calculated != -1 && calculated == token; }
From source file:com.keybox.manage.util.OTPUtil.java
/** * verifies code for OTP secret per time interval * * @param secret shared secret//from w ww . j a v a 2 s . c o m * @param token verification token * @param time time representation to calculate OTP * @return true if success */ private static boolean verifyToken(String secret, long token, long time) { long calculated = -1; byte[] key = new Base32().decode(secret); SecretKeySpec secretKey = new SecretKeySpec(key, "HmacSHA1"); try { Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] hash = mac.doFinal(ByteBuffer.allocate(8).putLong(time).array()); int offset = hash[hash.length - 1] & 0xF; for (int i = 0; i < 4; ++i) { calculated <<= 8; calculated |= (hash[offset + i] & 0xFF); } calculated &= 0x7FFFFFFF; calculated %= 1000000; } catch (Exception ex) { log.error(ex.toString(), ex); } return (calculated != -1 && calculated == token); }
From source file:com.kolich.havalo.client.signing.algorithms.HMACSHA256Signer.java
/** * Returns a Base-64 encoded HMAC-SHA256 signature. */// w ww . j a v a 2 s . co m @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:fi.vm.kapa.identification.service.PhaseIdService.java
public PhaseIdService(String sharedSecret, int timeInterval, String hmacAlgorithm) throws Exception { this.timeInterval = timeInterval; hmacCalc = Mac.getInstance(hmacAlgorithm); SecretKeySpec keySpec = new SecretKeySpec(sharedSecret.getBytes(), hmacAlgorithm); hmacCalc.init(keySpec);//from w ww .j ava 2 s .c o m }
From source file:com.microsoft.azure.batch.auth.BatchCredentialsInterceptor.java
private String sign(String accessKey, String stringToSign) { try {//w w w .j ava2s .c om // 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.lehman.ic9.common.base64.java
/** * Computes RFC 2104-compliant HMAC signature. * @param data Is a String with the data to encode. * @param key Is a String with the key.//from w w w . ja v a 2s . c o m * @return A string with the encoded signature. * @throws SignatureException Exception */ public static String encodeHmac(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 String(Base64.encodeBase64(rawHmac)); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }
From source file:net.fenyo.mail4hotspot.web.GMailOAuthStep1Servlet.java
private String hmac(final String key, final String message) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException { final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1")); return org.apache.commons.codec.binary.Base64.encodeBase64String(mac.doFinal(message.getBytes("UTF-8"))); }
From source file:org.apache.hadoop.mapred.PriorityAuthorization.java
/** * Adapted from AWS Query Authentication cookbook: * Computes RFC 2104-compliant HMAC signature. * * @param data//from www .ja v a2 s.co m * 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 */ public static String hmac(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 String(Base64.encodeBase64(rawHmac)); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e, e); } return result; }
From source file:com.adyen.Util.HMACValidator.java
public String calculateHMAC(String data, String key) throws java.security.SignatureException { try {//from w w w . java 2 s. co m byte[] rawKey = Hex.decodeHex(key.toCharArray()); // Create an hmac_sha256 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(rawKey, HMAC_SHA256_ALGORITHM); // Get an hmac_sha256 Mac instance and initialize with the signing // key Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes(C_UTF8)); // Base64-encode the hmac return new String(Base64.encodeBase64(rawHmac)); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } }