get String Mac Encrypt - Android java.security

Android examples for java.security:MAC

Description

get String Mac Encrypt

Demo Code


//package com.java2s;
import android.annotation.SuppressLint;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static final String HmacMD5 = "HmacMD5";
    public static final String HmacSHA1 = "HmacSHA1";
    public static final String HmacSHA256 = "HmacSHA256";
    public static final String HmacSHA384 = "HmacSHA384";
    public static final String HmacSHA512 = "HmacSHA512";

    public static String getStringMacEncrypt(String s, String algorithm,
            String keyString) {//from   w  w  w .  j a  va  2 s.c om
        SecretKey key = generateHmacKey(keyString, algorithm);
        return getStringMacEncrypt(s, key);
    }

    public static String getStringMacEncrypt(String s, SecretKey key) {
        return getBytesMacEncrypt(s.getBytes(), key);
    }

    @SuppressLint("TrulyRandom")
    public static SecretKey generateHmacKey(String algorithm) {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
            SecretKey key = keyGenerator.generateKey();
            return key;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static SecretKey generateHmacKey(String keyString,
            String algorithm) {
        SecretKey key = new SecretKeySpec(keyString.getBytes(), algorithm);
        return key;
    }

    public static String getBytesMacEncrypt(byte[] bytes, String algorithm,
            String keyString) {
        SecretKey key = generateHmacKey(keyString, algorithm);
        return getBytesMacEncrypt(bytes, key);
    }

    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 {
                mac = Mac.getInstance(algorithm);
                mac.init(key);
                return bytes2String(mac.doFinal(bytes));
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String bytes2String(byte digest[]) {
        String str = "";
        String tempStr = "";
        for (int i = 0; i < digest.length; i++) {
            tempStr = (Integer.toHexString(digest[i] & 0xff));
            if (tempStr.length() == 1) {
                str = str + "0" + tempStr;
            } else {
                str += tempStr;
            }
        }
        return str;
    }
}

Related Tutorials