List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:org.artifactory.security.crypto.CryptoHelper.java
public static String generateUniqueApiKey() throws GeneralSecurityException { byte[] hmacData; try {// ww w. j a v a2 s .co m SecretKeySpec secretKey = new SecretKeySpec("secretKey".getBytes("UTF-8"), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(secretKey); String data = UUID.randomUUID().toString(); hmacData = mac.doFinal(data.getBytes("UTF-8")); return new Base64Encoder().encode(hmacData); } catch (UnsupportedEncodingException e) { throw new GeneralSecurityException(e); } }
From source file:org.apache.abdera2.common.security.HashHelper.java
public static boolean hmacval(Key key, String alg, byte[] mat, byte[] dat) { try {// www. j a va 2s. c om Mac mac = Mac.getInstance(alg); mac.init(key); byte[] sig = mac.doFinal(mat); return Arrays.equals(sig, dat); } catch (Throwable t) { throw ExceptionHelper.propogate(t); } }
From source file:org.apache.abdera2.common.security.HashHelper.java
public static String hmac(Key key, String alg, byte[] mat) { try {/*from w ww . j a va 2s . c om*/ Mac mac = Mac.getInstance(alg); mac.init(key); mac.update(mat, 0, mat.length); byte[] sig = mac.doFinal(); return Base64.encodeBase64URLSafeString(sig); } catch (Throwable t) { throw ExceptionHelper.propogate(t); } }
From source file:org.apereo.openlrs.utils.OAuthUtils.java
public static String sign(String secret, Map<String, String> oauthParameters, String algorithm, String method, String url) {//from w w w. j av a 2 s. c om StringBuilder signatureBase = new StringBuilder(OAuthUtils.percentEncode(method)); signatureBase.append("&"); signatureBase.append(OAuthUtils.percentEncode(url)); signatureBase.append("&"); Map<String, String> treeMap = new TreeMap<String, String>(oauthParameters); treeMap.remove("oauth_signature"); treeMap.remove("realm"); boolean first = true; for (Map.Entry<String, String> entry : treeMap.entrySet()) { if (!first) signatureBase.append(OAuthUtils.percentEncode("&")); else first = false; signatureBase.append(OAuthUtils.percentEncode(entry.getKey() + "=" + entry.getValue())); } Mac mac = null; try { SecretKeySpec secretKeySpec = new SecretKeySpec((OAuthUtils.percentEncode(secret) + "&").getBytes(), algorithm); mac = Mac.getInstance(secretKeySpec.getAlgorithm()); mac.init(secretKeySpec); } catch (Exception e) { throw new RuntimeException(e); } if (log.isDebugEnabled()) { log.debug("signatureBaseString: " + signatureBase.toString()); } byte[] bytes = mac.doFinal(signatureBase.toString().getBytes()); byte[] encodedMacBytes = Base64.encodeBase64(bytes); return new String(encodedMacBytes); }
From source file:com.amazonaws.cbui.AmazonFPSCBUIPipeline.java
/** * Computes RFC 2104-compliant HMAC signature. *///from w ww. j av a 2s . c o m private static String sign(String data, String key, String signatureMethod) throws SignatureException { String signature = ""; try { Mac mac = Mac.getInstance(signatureMethod); mac.init(new SecretKeySpec(key.getBytes(), signatureMethod)); signature = new String(Base64.encodeBase64(mac.doFinal(data.getBytes(UTF_8_Encoding)))); } catch (Exception e) { throw new SignatureException("Failed to generate signature: " + e.getMessage(), e); } return signature; }
From source file:conexionSiabra.Oauth.java
private static String hmac_sha1(String value, String key) { try {//from w w w .j ava 2 s.co m SecretKey secretKey = null; byte[] keyBytes = key.getBytes(); secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = value.getBytes(); return new String(Base64.encode(mac.doFinal(text), 0)).trim(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }
From source file:org.jenkinsci.plugins.gogs.GogsWebHook.java
/** * encode sha256 hmac/* w w w .j a v a2 s . c om*/ * * @param data data to hex * @param key key of HmacSHA256 */ public static String encode(String data, String key) throws Exception { final Charset asciiCs = Charset.forName("UTF-8"); final Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); final SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(asciiCs.encode(key).array(), "HmacSHA256"); sha256_HMAC.init(secret_key); return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8"))); }
From source file:ch.threema.apitool.CryptTool.java
/** * Hashes an email address for identity lookup. * * @param email the email address// ww w .j a v a 2 s.co m * @return the raw hash */ public static byte[] hashEmail(String email) { try { Mac emailMac = Mac.getInstance("HmacSHA256"); emailMac.init(new SecretKeySpec(EMAIL_HMAC_KEY, "HmacSHA256")); String normalizedEmail = email.toLowerCase().trim(); return emailMac.doFinal(normalizedEmail.getBytes("US-ASCII")); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:ch.threema.apitool.CryptTool.java
/** * Hashes a phone number for identity lookup. * * @param phoneNo the phone number/* w w w.j ava2 s. c o m*/ * @return the raw hash */ public static byte[] hashPhoneNo(String phoneNo) { try { Mac phoneMac = Mac.getInstance("HmacSHA256"); phoneMac.init(new SecretKeySpec(PHONENO_HMAC_KEY, "HmacSHA256")); String normalizedPhoneNo = phoneNo.replaceAll("[^0-9]", ""); return phoneMac.doFinal(normalizedPhoneNo.getBytes("US-ASCII")); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.livily.pusher.Pusher.java
/** * Returns a HMAC/SHA256 representation of the given string * // w ww .j a v a 2 s. co m * @param data * @return */ private static String hmacsha256Representation(String data) { try { // Create the HMAC/SHA256 key from application secret final SecretKeySpec signingKey = new SecretKeySpec(pusherApplicationSecret.getBytes(), "HmacSHA256"); // Create the message authentication code (MAC) final Mac mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); // Process and return data byte[] digest = mac.doFinal(data.getBytes("UTF-8")); digest = mac.doFinal(data.getBytes()); // Convert to string BigInteger bigInteger = new BigInteger(1, digest); return String.format("%0" + (digest.length << 1) + "x", bigInteger); } catch (NoSuchAlgorithmException nsae) { // We should never come here, because GAE has HMac SHA256 throw new RuntimeException("No HMac SHA256 algorithm"); } catch (UnsupportedEncodingException e) { // We should never come here, because UTF-8 should be available throw new RuntimeException("No UTF-8"); } catch (InvalidKeyException e) { throw new RuntimeException("Invalid key exception while converting to HMac SHA256"); } }