List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:com.opentok.test.Helpers.java
private static String signData(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); return toHexString(mac.doFinal(data.getBytes())); }
From source file:org.sentilo.common.rest.hmac.HMACBuilder.java
private static String calculateHMAC(final String secret, final String data) throws GeneralSecurityException { final SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), MAC_ALGORITHM); final Mac mac = Mac.getInstance(MAC_ALGORITHM); mac.init(signingKey); final byte[] rawHmac = mac.doFinal(data.getBytes()); return new String(Base64.encodeBase64(rawHmac)); }
From source file:org.gss_project.gss.web.client.TestClient.java
public static String sign(String httpMethod, String timestamp, String path, String token) { String input = httpMethod + timestamp + path; String signed = null;//w ww . j av a2s .c o m try { System.err.println("Token:" + token); // Get an HMAC-SHA1 key from the authentication token. System.err.println("Input: " + input); SecretKeySpec signingKey = new SecretKeySpec(Base64.decodeBase64(token.getBytes()), "HmacSHA1"); // Get an HMAC-SHA1 Mac instance and initialize with the signing key. Mac hmac = Mac.getInstance("HmacSHA1"); hmac.init(signingKey); // Compute the HMAC on the input data bytes. byte[] rawMac = hmac.doFinal(input.getBytes()); // Do base 64 encoding. signed = new String(Base64.encodeBase64(rawMac), "US-ASCII"); } catch (InvalidKeyException ikex) { System.err.println("Fatal key exception: " + ikex.getMessage()); ikex.printStackTrace(); } catch (UnsupportedEncodingException ueex) { System.err.println("Fatal encoding exception: " + ueex.getMessage()); } catch (NoSuchAlgorithmException nsaex) { System.err.println("Fatal algorithm exception: " + nsaex.getMessage()); nsaex.printStackTrace(); } if (signed == null) System.exit(-1); System.err.println("Signed: " + signed); return signed; }
From source file:controllers.DiscourseAuth.java
private static String checksum(SecretKeySpec key, String macData) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(key); byte[] doFinal = mac.doFinal(macData.getBytes(StandardCharsets.UTF_8)); return Hex.encodeHexString(doFinal); }
From source file:com.vab.iflex.gateway.paybillservice.Stub.java
private static String calculateRFC2104HMAC(String data, String key) throws Exception { String result;//from w ww . j a v a 2 s .c o m 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 BASE64Encoder().encode(rawHmac); result = new Base64().encodeAsString(rawHmac); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }
From source file:com.gmu.uav.RadioSecurity.java
public static String hmacSha1(String value, byte[] key) { try {/*from w w w . j ava 2 s . c o m*/ // Get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key, "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:Main.java
public static String createHMACWithMD5(String source, String key) throws NoSuchAlgorithmException, InvalidKeyException { Mac hmac = Mac.getInstance("HmacMD5"); SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), hmac.getAlgorithm()); hmac.init(secretKey); byte[] signature = hmac.doFinal(source.getBytes()); return Base64.encodeToString(signature, Base64.DEFAULT); }
From source file:cn.ucloud.sdk.utils.EncoderUtils.java
/** * SHA1//from www.j a v a 2 s .com * @param key * @param str * @return */ public static String sha1(String key, String str) { String algorithm = "HmacSHA1"; String result = null; try { SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(keySpec); result = getFormattedText(mac.doFinal(str.getBytes("UTF-8"))); } catch (Exception e) { LogUtils.exception(logger, e); } return result; }
From source file:Main.java
public static String encodeHmac(String key, String data) throws Exception { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key); return bytesToHex(sha256_HMAC.doFinal(data.getBytes())); }
From source file:com.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java
/** * Get a HMAC signature using the specified secret * @param secret//ww w . jav a2 s . c o m * @param signingData * @return a Base64 encoded signature */ public static String getBase64EncodedSignature(String secret, String signingData) { SecretKey key = getMacKey(secret); try { Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(getMacKey(secret)); byte[] digest = mac.doFinal(signingData.getBytes("UTF8")); return new String(Base64.encodeBase64(digest), "ASCII"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }