Java HMAC hmacSha(String crypto, String key, String value)

Here you can find the source of hmacSha(String crypto, String key, String value)

Description

General hmac method - can pass in any crypto string recognised by Mac.getInstance .

License

Open Source License

Parameter

Parameter Description
crypto a parameter
key a parameter
value a parameter

Declaration

public static String hmacSha(String crypto, String key, String value) 

Method Source Code


//package com.java2s;
// This is part of Xively4J library, it is under the BSD 3-Clause license.

import java.math.BigInteger;
import java.security.GeneralSecurityException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**/* ww  w .jav  a2 s . c  o m*/
     * General hmac method - can pass in any crypto string recognised by
     * {@link Mac.getInstance}.
     * 
     * @param crypto
     * @param key
     * @param value
     * @return
     */
    public static String hmacSha(String crypto, String key, String value) {
        try {
            byte[] keyBytes = hexStr2Bytes(key);

            Mac hmac;
            hmac = Mac.getInstance(crypto);
            SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
            hmac.init(macKey);
            return bytesToHex(hmac.doFinal(value.getBytes()));
        } catch (GeneralSecurityException gse) {
            throw new RuntimeException(gse);
        }
    }

    /**
     * Convert hex string into byte array.
     * 
     * @param hex
     * @return
     */
    private static byte[] hexStr2Bytes(String hex) {
        // Adding one byte to get the right conversion
        // Values starting with "0" can be converted
        byte[] bArray = new BigInteger("10" + hex, 16).toByteArray();

        // Copy all the REAL bytes, not the "first"
        byte[] ret = new byte[bArray.length - 1];
        for (int i = 0; i < ret.length; i++)
            ret[i] = bArray[i + 1];
        return ret;
    }

    /**
     * 
     * Convert a byte array into a hex string
     * 
     * @param bytes
     * @return
     */
    private static String bytesToHex(byte[] bytes) {
        final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for (int j = 0; j < bytes.length; j++) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Related

  1. hmac_sha(String crypto, byte[] keyBytes, byte[] text)
  2. hmacDigest(String message, String secretKey, String algorithm)
  3. hmacEncode(String algorithm, String input, String privateKey)
  4. hmacHex(String keyString, String message, String hmac)
  5. hmacSha(byte[] keyBytes, byte[] text)
  6. hmacSha1(byte[] data, byte[] key)
  7. hmacSha1(byte[] key_bytes, byte[] text_bytes)
  8. hmacSha1(byte[] secret, byte[] data)
  9. hmacSha1(SecretKey key, byte[] data)