Example usage for javax.crypto Mac getInstance

List of usage examples for javax.crypto Mac getInstance

Introduction

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

Prototype

public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a Mac object that implements the specified MAC algorithm.

Usage

From source file:Main.java

public static int generate(byte[] key, long t, int digits) {
    int r = 0;/*from   www  .  j  a  va2  s  .  c  om*/
    try {
        t /= 30;
        byte[] data = new byte[8];
        long value = t;
        for (int i = 8; i-- > 0; value >>>= 8) {
            data[i] = (byte) value;
        }

        SecretKeySpec signKey = new SecretKeySpec(key, SHA1);
        Mac mac = Mac.getInstance(SHA1);
        mac.init(signKey);
        byte[] hash = mac.doFinal(data);

        int offset = hash[20 - 1] & 0xF;

        long truncatedHash = 0;
        for (int i = 0; i < 4; ++i) {
            truncatedHash <<= 8;
            truncatedHash |= (hash[offset + i] & 0xFF);
        }

        truncatedHash &= 0x7FFFFFFF;
        truncatedHash %= Math.pow(10, digits);

        r = (int) truncatedHash;
    }

    catch (Exception e) {
    }

    return r;
}

From source file:net.foreworld.util.RestUtil.java

public static String genSignature(String data, String key) {
    byte[] byteHMAC = null;
    try {/*from w  w  w .j  a  v a  2  s .c  om*/
        Mac mac = Mac.getInstance(ALGORITHM);
        SecretKeySpec spec = new SecretKeySpec(key.getBytes(), ALGORITHM);
        mac.init(spec);
        byteHMAC = mac.doFinal(data.toLowerCase(Locale.getDefault()).getBytes());
    } catch (InvalidKeyException ignore) {
        return null;
    } catch (NoSuchAlgorithmException ignore) {
        return null;
    } // END

    if (null == byteHMAC)
        return null;

    // String code = new BASE64Encoder().encode(byteHMAC);
    String code = Base64.encodeBase64String(byteHMAC);

    try {
        return URLEncoder.encode(code, ENC);
    } catch (UnsupportedEncodingException ignore) {
    }
    return null;
}

From source file:Main.java

public static String getFileMacEncrypt(File file, SecretKey key) throws IOException {
    String algorithm = key.getAlgorithm();
    Mac mac = null;//  www  .  j  a  v  a2s.c om
    try {
        mac = Mac.getInstance(algorithm);
        mac.init(key);
        FileInputStream in = new FileInputStream(file);
        byte[] buffer = new byte[1024 * 1024];
        int len = 0;
        while ((len = in.read(buffer)) > 0) {
            mac.update(buffer, 0, len);
        }
        in.close();
        return bytes2String(mac.doFinal());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] sha256(final byte[] message, final byte[] key) {
    final Mac mac;
    try {/*from  www  . j a  v  a  2  s.  co  m*/
        mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(key, "HmacSHA256"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return mac.doFinal(message);
}

From source file:name.martingeisse.common.util.HmacUtil.java

/**
 * Base function to generate a HMAC./* w  w  w . j a  v a2s.c  om*/
 * 
 * @param payload the payload data
 * @param secret the secret to sign the HMAC
 * @param algorithm the HMAC algorithm to use
 * @return the HMAC
 */
public static byte[] generateHmac(byte[] payload, byte[] secret, String algorithm) {
    try {
        final Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec(secret, algorithm));
        return mac.doFinal(payload);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] generateCramMD5ClientResponse(String userName, String userPassword, byte[] challengeBytes)
        throws Exception {
    String macAlgorithm = "HmacMD5";
    Mac mac = Mac.getInstance(macAlgorithm);
    mac.init(new SecretKeySpec(userPassword.getBytes("UTF-8"), macAlgorithm));
    final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes);
    String responseAsString = userName + " " + toHex(messageAuthenticationCode);
    return responseAsString.getBytes();
}

From source file:Main.java

private static byte[] hmacSha1(byte[] value, byte[] key) {
    try {//from   w ww .  j a v  a2  s.  c om
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        return mac.doFinal(value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/***
 * Computes RFC 2104-compliant HMAC signature. This can be used to sign the Amazon S3
 * request urls//from w w w .  ja va  2s. c o  m
 * 
 * @param data The data to be signed.
 * @param key The signing key.
 * @return The Base64-encoded RFC 2104-compliant HMAC signature.
 * @throws SignatureException when signature generation fails
 */
public static String getHMac(String data, String key) throws SignatureException {

    if (data == null) {
        throw new NullPointerException("Data to be signed cannot be null");
    }

    String result = null;
    try {

        final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

        // 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 &
        // initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] digest = mac.doFinal(data.getBytes());

        if (digest != null) {
            // Base 64 Encode the results
            result = Base64.encodeToString(digest, Base64.NO_WRAP);
        }

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }

    return result;
}

From source file:com.centurylink.mdw.util.HmacSha1Signature.java

public static String getHMACHexdigestSignature(byte[] payloadBytes, String key)
        throws InvalidKeyException, NoSuchAlgorithmException {
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM);
    Mac mac = Mac.getInstance(ALGORITHM);
    mac.init(keySpec);//from  w  w w. ja  va  2  s  .  c  om
    byte[] rawHmac = mac.doFinal(payloadBytes);
    return Hex.encodeHexString(rawHmac);
}

From source file:io.milton.http.http11.auth.HmacUtils.java

public static String calcShaHash(String data, String key) {
    String result = null;// w w  w .  j a  v  a 2  s .  c om
    try {
        Key signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        result = Base64.encodeBase64URLSafeString(rawHmac);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(HMAC_SHA1_ALGORITHM, e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(HMAC_SHA1_ALGORITHM, e);
    } catch (IllegalStateException e) {
        throw new RuntimeException(HMAC_SHA1_ALGORITHM, e);
    }

    return result;
}