generate Hmac Key - Android java.security

Android examples for java.security:HMAC

Description

generate Hmac Key

Demo Code


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

import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Main {

    @SuppressLint("TrulyRandom")
    public static SecretKey generateHmacKey(String algorithm) {
        try {// w  ww.  jav  a2  s. co m
            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;
    }
}

Related Tutorials