Java 8 Random和SecureRandom生成随机字符串新变化

在java 8中,在Random和SecureRandom类中增加了一些新方法。这些方法就像ints、longs和double一样。它们分别返回IntStream、LongStream和DoubleStream。SecureRandom类扩展了Random类。

In java 8, some new methods has been added in Random and SecureRandom classes. These methods are like ints, longs and doubles. They return IntStream, LongStream and DoubleStream respectively. SecureRandom class extends Random class. Here in this page, I will provide an example of password generator.

一、java.util.Random

Random类产生伪随机数。对于每个实例的调用,都会返回一个伪随机数。Random类是线程安全的。

Random class generates pseudorandom numbers. For each call of the instance, a pseudorandom number is returned. Random class is thread safe. Random class has some new methods to support java 8.

1、Random类有一些支持Java 8的新方法

ints: 以IntStream的形式返回integer值。
longs: 以LongStream的形式返回long值。
doubles: 以DoubleStream的形式返回double值。

2、生成密码的示例

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;

public class RandomDemo {

private static final List<Integer> VALID_PWD_CHARS = new ArrayList<>();

static {
    IntStream.rangeClosed('0', '9').forEach(VALID_PWD_CHARS::add);    // 0-9
    IntStream.rangeClosed('a', 'z').forEach(VALID_PWD_CHARS::add);    // a-z
}

public static void main(String[] args) {
    int passwordLength = 8;
    System.out.println("---Generated Password---");

    for(int i=0;i<5;i++) {
       new Random().ints(passwordLength, 0, VALID_PWD_CHARS.size())
                            .map(VALID_PWD_CHARS::get).forEach(s -> System.out.print((char) s));
       System.out.println();
    }
}
}

输出:

---Generated Password---
6mx3dunz
teuf505p
03nym5w3
zez006fc
y9q0rbs3 

二、java.security.SecureRandom

SecureRandom是加密的强数字发生器(RNG)。SecureRandom扩展了Random类,并通过在java 8中添加的新方法得到了丰富。

SecureRandom is cryptographically strong number generator (RNG). SecureRandom extends Random class and is enriched by new methods added in java 8. Find the example for SecureRandom.

1、SecureRandom的例子

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class SecureRandomDemo {

private static final List<Integer> VALID_PWD_CHARS = new ArrayList<>();

static {
    IntStream.rangeClosed('0', '9').forEach(VALID_PWD_CHARS::add);    // 0-9
    IntStream.rangeClosed('A', 'Z').forEach(VALID_PWD_CHARS::add);    // A-Z
    IntStream.rangeClosed('a', 'z').forEach(VALID_PWD_CHARS::add);    // a-z
    IntStream.rangeClosed('!', '*').forEach(VALID_PWD_CHARS::add);    // !-*
}

public static void main(String[] args) {
    int passwordLength = 8;
    System.out.println("---Generated Password---");

    for(int i=0;i<5;i++) {
        new SecureRandom().ints(passwordLength, 0, VALID_PWD_CHARS.size())
                            .map(VALID_PWD_CHARS::get).forEach(s -> System.out.print((char) s));
    System.out.println();
    }
}
}

输出:

---Generated Password---
Qq2R%SsQ
7PjxGxkO
xkMgQq2h
dljs4*w%
55"tSGJ5 

三、参考文献

Java 8 Random and SecureRandom Changes Example

发表评论