List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal(byte[] input) throws IllegalStateException
From source file:fr.ortolang.diffusion.security.authentication.TOTPHelper.java
private static byte[] hmacSha(byte[] keyBytes, byte[] text) { try {//w w w . java 2s .com Mac hmac; hmac = Mac.getInstance("HmacSHA1"); SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW"); hmac.init(macKey); return hmac.doFinal(text); } catch (GeneralSecurityException gse) { throw new UndeclaredThrowableException(gse); } }
From source file:org.b3log.latke.util.Crypts.java
/** * Signs the specified source string using the specified secret. * * @param source the specified source string * @param secret the specified secret/*from w w w. j a v a 2 s.com*/ * @return signed string */ public static String signHmacSHA1(final String source, final String secret) { try { final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA1")); final byte[] signData = mac.doFinal(source.getBytes("UTF-8")); return new String(Base64.encodeBase64(signData), "UTF-8"); } catch (final Exception e) { throw new RuntimeException("HMAC-SHA1 sign failed", e); } }
From source file:com.google.resting.rest.util.oauth.SignatureUtil.java
/** * Sign request./*from www. ja v a 2 s .c om*/ * * @param keyString Consumer key for request signing. * @param targetDomain Domain of the REST endpoint (Ex. login.yahoo.com) * @param verb Type of REST operation (GET/POST/PUT/DELETE) * @param isSecureInvocation HTTP/HTTPS * @param contextPathElement Path element in the base REST uri (Ex. /weather/india) * @param inputParams Collection of request params for REST request (Ex. city=calcutta ) * @return * @throws NoSuchAlgorithmException The exception is thrown if the encryption algorithm is not supported. * @throws InvalidKeyException The exception is thrown if the consumer key is invalid * @throws IllegalStateException * @throws UnsupportedEncodingException The exception is thrown if the URL encoding is incorrect. */ public static String getSignature(String keyString, String targetDomain, Verb verb, boolean isSecureInvocation, String contextPathElement, List<NameValuePair> inputParams, String messageEncoding) throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException, UnsupportedEncodingException { String baseString = getBaseString(targetDomain, verb.toString(), isSecureInvocation, contextPathElement, inputParams, messageEncoding).replace("+", "%20").replace("*", "%2A").replace("%7E", "~"); System.out.println("Base string is " + baseString); Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(keyString.getBytes(messageEncoding), mac.getAlgorithm()); mac.init(secret); byte[] digest = mac.doFinal(baseString.getBytes(messageEncoding)); return new String(Base64.encodeBase64(digest)).replace(RequestConstants.CARRIAGE_RETURN, RequestConstants.EMPTY_STRING); }
From source file:com.google.walkaround.util.server.auth.DigestUtils2.java
/** * Computes the RFC2104 SHA1 HMAC digest for the given message and secret. * * @param message the data to compute the digest of *///www. ja v a 2 s . c o m public static byte[] sha1hmac(Secret key, byte[] message) { try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.data, 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[] ret = mac.doFinal(message); assert ret.length == SHA1_BLOCK_SIZE; return ret; } catch (InvalidKeyException e) { throw new RuntimeException("Failed to generate HMAC", e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Failed to generate HMAC", e); } }
From source file:org.mozilla.android.sync.Cryptographer.java
private static byte[] generateHmac(CryptoInfo bundle) { Mac hmacHasher = HKDF.makeHmacHasher(HKDF.makeHmacKey(bundle.getKeys().getHmacKey())); return hmacHasher.doFinal(Base64.encodeBase64(bundle.getMessage())); }
From source file:org.matmaul.freeboxos.login.LoginManager.java
protected static String hmacSha1(String key, String value) { try {/* w w w . j a v a 2s .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.spectralogic.ds3client.utils.Signature.java
/** * Computes RFC 2104-compliant HMAC signature. * * @param data/*from w w w.jav 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(final String data, final String key) throws java.security.SignatureException { LOG.debug("String to sign: {}", data.replace("\n", "\\n")); final String result; try { // get an hmac_sha1 key from the raw key bytes final SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes final byte[] rawHmac = mac.doFinal(data.getBytes(Charset.forName("UTF-8"))); result = Base64.encodeBase64String(rawHmac); } catch (final Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result.trim(); }
From source file:fi.okm.mpass.shibboleth.authn.impl.BaseInitializeWilmaContext.java
/** * Calculates the checksum for the given string, using the given Mac instance and secret key. * @param mac The Mac instance.// w w w . ja v a 2 s . com * @param string The seed. * @param key The secret key used for calculating the checksum. * @return The checksum value. */ public static String calculateChecksum(final Mac mac, final String string, final SecretKey key) { try { mac.init(key); final byte[] bytes = mac.doFinal(string.getBytes("UTF-8")); return new String(Hex.encodeHex(bytes)); } catch (InvalidKeyException | UnsupportedEncodingException e) { LoggerFactory.getLogger(InitializeStaticWilmaContext.class) .error("Could not sign the input data {} with the key", string); } return null; }
From source file:net.bryansaunders.jee6divelog.util.HashUtils.java
/** * Generates a HMAC-SHA1 Hash of the given String using the specified key. * //from w ww .ja v a 2 s. c o m * @param stringToHash * String to Hash * @param key * Key to Sign the String with * @return Hashed String */ public static String toHmacSha1(final String stringToHash, final String key) { String result = ""; try { // Get an hmac_sha1 key from the raw key bytes final byte[] keyBytes = key.getBytes(); final SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); // Get an hmac_sha1 Mac instance and initialize with the signing key final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); // Compute the hmac on input data bytes final byte[] rawHmac = mac.doFinal(stringToHash.getBytes()); // Convert raw bytes to Hex final byte[] base64 = Base64.encodeBase64(rawHmac); // Covert array of Hex bytes to a String result = new String(base64); } catch (GeneralSecurityException e) { HashUtils.LOGGER.error("An Error Occured Generating an HMAC-SHA1 Hash!", e); } return result; }
From source file:org.jenkinsci.plugins.karotz.KarotzUtil.java
/** * Creates HmacSha1.// ww w . j av a2 s .c o m * * @param secretKey SecretKey * @param data target data * @return HmacSha1 * @throws KarotzException Illegal encoding. */ public static String doHmacSha1(String secretKey, String data) throws KarotzException { String hmacSha1; try { Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(secretKey.getBytes("ASCII"), "HmacSHA1"); mac.init(secret); byte[] digest = mac.doFinal(data.getBytes("UTF-8")); hmacSha1 = new String(Base64.encodeBase64(digest), "ASCII"); } catch (IllegalStateException e) { throw new KarotzException(e); } catch (InvalidKeyException e) { throw new KarotzException(e); } catch (NoSuchAlgorithmException e) { throw new KarotzException(e); } catch (UnsupportedEncodingException e) { throw new KarotzException(e); } return hmacSha1; }