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:Main.java

public static String hmacSha256Encode(String key, String data)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("utf-8"), "HmacSHA256");
    sha256_HMAC.init(secret_key);
    return asHex(sha256_HMAC.doFinal(data.getBytes("utf-8")));
}

From source file:com.khipu.lib.java.KhipuService.java

public static String HmacSHA256(String secret, String data) {
    try {/*from   ww w  .  j av a2  s  . c  o  m*/
        SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(secretKeySpec);
        byte[] digest = mac.doFinal(data.getBytes("UTF-8"));
        return byteArrayToString(digest);
    } catch (InvalidKeyException e) {
        throw new RuntimeException("Invalid key exception while converting to HMac SHA256");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Algorithm not supported");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Encoding not supported");
    }
}

From source file:com.nlworks.wowapi.util.ConnectionManager.java

private static String generateSignature(String key, String data) throws GeneralSecurityException, IOException {
    byte[] hmacData = null;
    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(secretKey);
    hmacData = mac.doFinal(data.getBytes("UTF-8"));
    return Base64.encodeBase64String(hmacData);
}

From source file:com.eugene.fithealthmaingit.FatSecretSearchAndGet.FatSecretGetMethod.java

private static String sign(String method, String uri, String[] params) {
    String[] p = { method, Uri.encode(uri), Uri.encode(paramify(params)) };
    String s = join(p, "&");
    SecretKey sk = new SecretKeySpec(Globals.APP_SECRET.getBytes(), Globals.HMAC_SHA1_ALGORITHM);
    try {//from  ww  w.  j  a  v  a2s .  c  om
        Mac m = Mac.getInstance(Globals.HMAC_SHA1_ALGORITHM);
        m.init(sk);
        return Uri.encode(new String(Base64.encode(m.doFinal(s.getBytes()), Base64.DEFAULT)).trim());
    } catch (java.security.NoSuchAlgorithmException e) {
        Log.w("FatSecret_TEST FAIL", e.getMessage());
        return null;
    } catch (java.security.InvalidKeyException e) {
        Log.w("FatSecret_TEST FAIL", e.getMessage());
        return null;
    }
}

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 va  2  s.c  o  m*/
 * @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.kku.apps.pricesearch.util.SignedHelper.java

private static String sign(String apiSecretKey, String sourceString) throws Exception {
    SecretKeySpec secretKeySpec = new SecretKeySpec(apiSecretKey.getBytes(ENCODING), ALGORITHM);
    Mac mac = Mac.getInstance(ALGORITHM);
    mac.init(secretKeySpec);
    byte[] data = mac.doFinal(sourceString.getBytes(ENCODING));
    Base64 encoder = new Base64();
    String signature = new String(encoder.encode(data));
    return signature;
}

From source file:crocserver.app.CrocSecurity.java

public static Mac createHmac(byte[] secret) throws Exception {
    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1");
    mac.init(signKey);
    return mac;/*  www.  j a  v a2s  .  c  o m*/
}

From source file:Main.java

public static String getBytesMacEncrypt(byte[] bytes, SecretKey key) {
    String algorithm = key.getAlgorithm();
    if (algorithm.equals(HmacMD5) || algorithm.equals(HmacSHA1) || algorithm.equals(HmacSHA256)
            || algorithm.equals(HmacSHA384) || algorithm.equals(HmacSHA512)) {
        Mac mac = null;
        try {//from w ww  .  j a v  a2s  .  co m
            mac = Mac.getInstance(algorithm);
            mac.init(key);
            return bytes2String(mac.doFinal(bytes));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }
    return null;
}

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
 *//* w ww  .  jav  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:com.spectralogic.ds3client.utils.Signature.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 * * @param data//from   w w  w. j  a v a  2s.  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();
}