SHA-1 key generate - Android java.security

Android examples for java.security:Sha

Description

SHA-1 key generate

Demo Code


//package com.java2s;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.Arrays;

import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static SecretKeySpec keygenerate(String passphrase) {
        SecretKeySpec sks = null;
        try {/*from  w ww .  ja v a  2  s.c  om*/
            byte[] key = passphrase.getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); // use only first 128 bit

            sks = new SecretKeySpec(key, "AES");
        } catch (NoSuchAlgorithmException e) {
        } catch (UnsupportedEncodingException e) {
        }
        return sks;
    }
}

Related Tutorials