generate Salt from SecureRandom - Android java.util

Android examples for java.util:Random

Description

generate Salt from SecureRandom

Demo Code


//package com.java2s;
import java.security.SecureRandom;

public class Main {
    public static final String RANDOM_ALGORITHM = "SHA1PRNG";

    public static byte[] generateSalt() {
        return randomByte(20);
    }/* w ww.  j  a va  2 s .c  o  m*/

    private static byte[] randomByte(int length) {
        byte[] iv = new byte[length];
        SecureRandom random = getSecureRandom();
        random.nextBytes(iv);
        return iv;
    }

    public static SecureRandom getSecureRandom() {
        try {
            return SecureRandom.getInstance(RANDOM_ALGORITHM, "Crypto");
        } catch (Exception e) {
            try {
                return SecureRandom.getInstance(RANDOM_ALGORITHM);
            } catch (Exception e1) {
                return new SecureRandom();
            }
        }
    }
}

Related Tutorials