List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:name.martingeisse.common.util.HmacUtil.java
/** * Base function to generate a HMAC.//from www . ja v a 2 s. co m * * @param payload the payload data * @param secret the secret to sign the HMAC * @param algorithm the HMAC algorithm to use * @return the HMAC */ public static byte[] generateHmac(byte[] payload, byte[] secret, String algorithm) { try { final Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(secret, algorithm)); return mac.doFinal(payload); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } }
From source file:org.fs.ghanaian.util.StringUtility.java
static Mac getMac(String key, String algo) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(KEY_ENCODING), algo); Mac mac = Mac.getInstance(algo); mac.init(secretKeySpec); return mac;//from w w w.j av a 2 s . c o m }
From source file:co.mitro.twofactor.TwoFactorCodeChecker.java
public static int computeHash(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException { byte[] data = new byte[8]; long value = t; for (int i = 8; i-- > 0; value >>>= 8) { data[i] = (byte) value; }//from ww w.j a v a 2s . c o m SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signKey); byte[] hash = mac.doFinal(data); int offset = hash[20 - 1] & 0xF; long truncatedHash = 0; for (int i = 0; i < 4; ++i) { truncatedHash <<= 8; truncatedHash |= (hash[offset + i] & 0xFF); } truncatedHash &= 0x7FFFFFFF; truncatedHash %= 1000000; return (int) truncatedHash; }
From source file:de.marx_labs.utilities.common.util.HashUtil.java
public static String hmacSha1(String value, String key) { try {/*from w w w . j a v a2s . c o m*/ // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "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(value.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.cloud.bridge.util.S3SoapAuth.java
/** * Create a signature by the following method: * new String( Base64( SHA1( key, byte array ))) * //from www. j a va2s . c o m * @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:org.apache.tapestry5.internal.util.MacOutputStream.java
public static MacOutputStream streamFor(Key key) throws IOException { try {/* w w w . j a v a 2s . com*/ Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key); return new MacOutputStream(mac); } catch (Exception ex) { throw new IOException("Unable to create MacOutputStream: " + ExceptionUtils.toMessage(ex), ex); } }
From source file:org.egov.infra.security.utils.HMAC.java
public static String digest(String message, String privateKey) { try {/*from w w w . ja va 2 s . c o m*/ Mac hmac = Mac.getInstance(HMAC_SHA_256); hmac.init(new SecretKeySpec(privateKey.getBytes(UTF_8), HMAC_SHA_256)); return Hex.encodeHexString(hmac.doFinal(privateKey.concat(message).getBytes(US_ASCII))); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new ApplicationRuntimeException("Error occurred while hashing message", e); } }
From source file:Main.java
/*** * Computes RFC 2104-compliant HMAC signature. This can be used to sign the Amazon S3 * request urls/*w w w . ja v a 2 s.c o m*/ * * @param data The data to be signed. * @param key The signing key. * @return The Base64-encoded RFC 2104-compliant HMAC signature. * @throws SignatureException when signature generation fails */ public static String getHMac(String data, String key) throws SignatureException { if (data == null) { throw new NullPointerException("Data to be signed cannot be null"); } String result = null; try { final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; // 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 & // initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] digest = mac.doFinal(data.getBytes()); if (digest != null) { // Base 64 Encode the results result = Base64.encodeToString(digest, Base64.NO_WRAP); } } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }
From source file:de.desy.dcache.s3.Signature.java
/** * Computes RFC 2104-compliant HMAC signature. * * @param data/*from w ww . j a v a 2 s . c o 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 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 String(Base64.encodeBase64(rawHmac)); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC: " + e.getMessage()); } return result; }
From source file:org.jenkinsci.plugins.zanata.webhook.HmacUtil.java
private static Mac getInitializedMac(final String algorithm, final byte[] key) { if (key == null) { throw new IllegalArgumentException("Null key"); }/*from w ww .j a va 2s. c o m*/ try { final SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); final Mac mac = Mac.getInstance(algorithm); mac.init(keySpec); return mac; } catch (final NoSuchAlgorithmException | InvalidKeyException e) { throw new IllegalArgumentException(e); } }