Example usage for javax.crypto Mac init

List of usage examples for javax.crypto Mac init

Introduction

In this page you can find the example usage for javax.crypto Mac init.

Prototype

public final void init(Key key) throws InvalidKeyException 

Source Link

Document

Initializes this Mac object with the given key.

Usage

From source file:com.chumbok.aauth.otp.TOTP.java

/**
 * This method uses the JCE to provide the crypto algorithm. HMAC computes a
 * Hashed Message Authentication Code with the crypto hash algorithm as a
 * parameter.//from w  w w  .j a va  2s.c o m
 *
 * @param crypto
 *            : the crypto algorithm (HmacSHA1, HmacSHA256, HmacSHA512)
 * @param keyBytes
 *            : the bytes to use for the HMAC key
 * @param text
 *            : the message or text to be authenticated
 */

private static byte[] hmac_sha(String crypto, byte[] keyBytes, byte[] text) {
    try {
        Mac hmac;
        hmac = Mac.getInstance(crypto);
        SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
        hmac.init(macKey);
        return hmac.doFinal(text);
    } catch (GeneralSecurityException gse) {
        throw new UndeclaredThrowableException(gse);
    }
}

From source file:com.grouptuity.venmo.VenmoSDK.java

private static String hash_hmac(String payload, String app_secret, String algorithm) {
    try {//w w  w.j  av  a2  s . c om
        Mac mac = Mac.getInstance(algorithm);
        SecretKeySpec secret = new SecretKeySpec(app_secret.getBytes(), algorithm);
        mac.init(secret);
        byte[] digest = mac.doFinal(payload.getBytes());
        String enc = new String(digest);
        return enc;
    } catch (Exception e) {
        Log.d("VenmoSDK Error Message Caught", e.getMessage());
        return "";
    }
}

From source file:com.akamai.edgegrid.signer.EdgeGridV1Signer.java

private static byte[] sign(String s, byte[] key) throws RequestSigningException {
    try {/*from  w w  w  .j  a v  a2  s  .  c  om*/
        SecretKeySpec signingKey = new SecretKeySpec(key, SIGNING_ALGORITHM);
        Mac mac = Mac.getInstance(SIGNING_ALGORITHM);
        mac.init(signingKey);

        byte[] valueBytes = s.getBytes(StandardCharsets.UTF_8);
        return mac.doFinal(valueBytes);
    } catch (NoSuchAlgorithmException e) {
        throw new RequestSigningException(
                "Failed to sign: your JDK does not recognize signing algorithm <" + SIGNING_ALGORITHM + ">", e);
    } catch (InvalidKeyException e) {
        throw new RequestSigningException("Failed to sign: invalid key", e);
    }
}

From source file:uk.ac.tgac.bbsrc.miso.external.ajax.ExternalSectionControllerHelperService.java

public static String calculateHMAC(String data, String key) throws java.security.SignatureException {
    String result;//ww w .jav  a  2  s.  c  om
    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.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(data.getBytes());

        // base64-encode the hmac
        result = Base64.encodeBase64URLSafeString(rawHmac);
    } catch (Exception e) {
        log.error("failed to generate HMAC", e);
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
}

From source file:com.emc.atmos.api.RestUtil.java

public static String sign(String string, byte[] hashKey) {
    try {/*from   w w w  .  ja va  2s .co m*/
        // Compute the signature hash
        l4j.debug("Hashing: \n" + string);

        byte[] input = string.getBytes("UTF-8");

        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec key = new SecretKeySpec(hashKey, "HmacSHA1");
        mac.init(key);

        byte[] hashBytes = mac.doFinal(input);

        // Encode the hash in Base64.
        String hash = new String(Base64.encodeBase64(hashBytes), "UTF-8");

        l4j.debug("Hash: " + hash);

        return hash;
    } catch (Exception e) {
        throw new RuntimeException("Error signing string:\n" + string + "\n", e);
    }
}

From source file:org.encuestame.oauth1.support.OAuth1Utils.java

/**
 *
 * @param signatureBaseString/*from   w  w  w .j a v  a  2s . co m*/
 * @param key
 * @return
 */
private static String sign(String signatureBaseString, String key) {
    try {
        Mac mac = Mac.getInstance(HMAC_SHA1_MAC_NAME);
        SecretKeySpec spec = new SecretKeySpec(key.getBytes(), HMAC_SHA1_MAC_NAME);
        mac.init(spec);
        byte[] text = signatureBaseString.getBytes("UTF-8");
        byte[] signatureBytes = mac.doFinal(text);
        signatureBytes = Base64.encodeBase64(signatureBytes);
        String signature = new String(signatureBytes, "UTF-8");
        return signature;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.siphyc.utils.Utilities.java

public static String getHMACSHA256(String secret, String message) {
    try {/*from www.  j  a  v a  2 s. com*/
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
        sha256_HMAC.init(secret_key);

        String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));
        return hash;
    } catch (Exception e) {
        System.out.println("Error");
    }
    return null;
}

From source file:be.fedict.eid.idp.model.CryptoUtil.java

public static Mac getMac(byte[] hmacSecret) throws InvalidKeyException {

    SecretKey macKey = new SecretKeySpec(hmacSecret, "HmacSHA1");
    Mac mac;
    try {//ww  w  .  j a  v a  2 s. c om
        mac = Mac.getInstance(macKey.getAlgorithm());
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("HMAC algo not available: " + e.getMessage());
    }
    mac.init(macKey);
    return mac;
}

From source file:io.syndesis.rest.v1.state.ClientSideState.java

static byte[] mac(final String authenticationAlgorithm, final CharSequence base,
        final SecretKey authenticationKey) {
    try {//from w w  w . j  av  a 2s. c o  m
        final String baseString = base.toString();

        final Mac mac = Mac.getInstance(authenticationAlgorithm);
        mac.init(authenticationKey);

        // base contains only BASE64 characters and '|', so we use ASCII
        final byte[] raw = baseString.getBytes(StandardCharsets.US_ASCII);

        return mac.doFinal(raw);
    } catch (final GeneralSecurityException e) {
        throw new IllegalStateException("Unable to compute MAC of the given value", e);
    }
}

From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java

public static String sign(byte[] auth_key, String message, byte hmac_type)
        throws NoSuchAlgorithmException, InvalidKeyException {
    // Check if hmac_type is valid
    if (hmac_type > 4 || hmac_type < 0)
        throw new IllegalArgumentException("Invalid digest type was specified");

    // Create Mac instance 
    Mac hmac;
    hmac = Mac.getInstance(HMAC_ALGORITHMS[hmac_type]);

    // Create key
    SecretKeySpec hmac_key = new SecretKeySpec(auth_key, HMAC_ALGORITHMS[hmac_type]);

    // Init hmac object
    hmac.init(hmac_key);

    // Prepare enc_part to calculate HMAC
    byte[] msg_to_hmac = FWKNOP_ENCRYPTION_HEADER.concat(message).getBytes();

    // Calculate HMAC and return
    return message.concat(Base64.encodeBase64String(hmac.doFinal(msg_to_hmac)).replace("=", ""));
}