- java.lang.Object
-
- java.util.Random
-
- java.security.SecureRandom
-
- 实现的所有接口
-
Serializable
public class SecureRandom extends Random
该类提供加密强随机数生成器(RNG)。加密强随机数最低限度符合FIPS 140-2, Security Requirements for Cryptographic Modules第4.9.1节中规定的统计随机数发生器测试。 此外,
SecureRandom必须产生非确定性输出。 因此,传递给SecureRandom对象的任何种子材料必须是不可预测的,并且所有SecureRandom输出序列必须加密强,如RFC 4086: Randomness Requirements for Security中所述 。许多
SecureRandom实现采用伪随机数生成器(PRNG,也称为确定性随机比特生成器或DRBG)的形式,这意味着它们使用确定性算法从随机种子生成伪随机序列。 其他实现可以产生真正的随机数,而其他实现可以使用两种技术的组合。调用者通过无参数构造函数或
getInstance方法之一获取SecureRandom实例。 例如:SecureRandom r1 = new SecureRandom(); SecureRandom r2 = SecureRandom.getInstance("NativePRNG"); SecureRandom r3 = SecureRandom.getInstance("DRBG", DrbgParameters.instantiation(128, RESEED_ONLY, null));上面的第三个语句返回支持特定实例化参数的特定算法的
SecureRandom对象。 实现的有效实例化参数必须与此最小请求匹配,但不一定相同。 例如,即使请求不需要某个特征,实际的实例化也可以提供该特征。 实现可能会懒惰地实例化SecureRandom直到它实际使用,但有效的实例化参数必须在创建后立即确定,并且getParameters()应始终保持不变的相同结果。SecureRandom典型调用SecureRandom调用以下方法来检索随机字节:SecureRandom random = new SecureRandom(); byte[] bytes = new byte[20]; random.nextBytes(bytes);
调用者也可以调用
generateSeed(int)方法来生成给定数量的种子字节(例如,为其他随机数生成器播种):byte[] seed = random.generateSeed(20);
新创建的PRNG
SecureRandom对象不是种子(除非它是由SecureRandom(byte[])创建的)。 第一次调用nextBytes将强制它从特定于实现的熵源中播种。 如果先前调用了setSeed则不会发生这种自播种。甲
SecureRandom可以在任何时候通过调用补种reseed或setSeed方法。reseed方法从其熵源读取熵输入以重新定位自身。setSeed方法要求调用者提供种子。请注意:
reseed并非所有支持SecureRandom实现。某些
SecureRandom实现可以在其nextBytes(byte[], SecureRandomParameters)和reseed(SecureRandomParameters)方法中接受SecureRandomParameters参数,以进一步控制方法的行为。注意:根据实施,
generateSeed,reseed和nextBytes方法可阻止作为熵被收集,例如,如果源熵是/ dev /上的各种类Unix操作系统随机的。线程安全
SecureRandom对象可安全使用多个并发线程。- 实现要求:
-
SecureRandom服务提供商可以通过在注册提供商时将service provider attribute “ThreadSafe”设置为“true”来通告它是线程安全的。 否则,此类将同步访问SecureRandomSpi实现的以下方法: - 从以下版本开始:
- 1.1
- 另请参见:
-
SecureRandomSpi,Random, Serialized Form
-
-
构造方法摘要
构造方法 变量 构造器 描述 SecureRandom()构造一个实现默认随机数算法的安全随机数发生器(RNG)。SecureRandom(byte[] seed)构造一个实现默认随机数算法的安全随机数发生器(RNG)。protectedSecureRandom(SecureRandomSpi secureRandomSpi, Provider provider)创建一个SecureRandom对象。
-
方法摘要
所有方法 静态方法 实例方法 具体的方法 变量和类型 方法 描述 byte[]generateSeed(int numBytes)返回给定的种子字节数,使用此类用于种子自身的种子生成算法计算。StringgetAlgorithm()返回此SecureRandom对象实现的算法的名称。static SecureRandomgetInstance(String algorithm)返回实现指定的随机数生成器(RNG)算法的SecureRandom对象。static SecureRandomgetInstance(String algorithm, String provider)返回实现指定的随机数生成器(RNG)算法的SecureRandom对象。static SecureRandomgetInstance(String algorithm, Provider provider)返回实现指定的随机数生成器(RNG)算法的SecureRandom对象。static SecureRandomgetInstance(String algorithm, SecureRandomParameters params)返回实现指定的随机数生成器(RNG)算法的SecureRandom对象,并支持指定的SecureRandomParameters请求。static SecureRandomgetInstance(String algorithm, SecureRandomParameters params, String provider)返回实现指定的随机数生成器(RNG)算法的SecureRandom对象,并支持指定的SecureRandomParameters请求。static SecureRandomgetInstance(String algorithm, SecureRandomParameters params, Provider provider)返回实现指定的随机数生成器(RNG)算法的SecureRandom对象,并支持指定的SecureRandomParameters请求。static SecureRandomgetInstanceStrong()返回使用securerandom.strongAlgorithmsSecurity属性中指定的算法/提供程序选择的SecureRandom对象。SecureRandomParametersgetParameters()返回此SecureRandom实例的有效SecureRandomParameters。ProvidergetProvider()返回此SecureRandom对象的提供程序。static byte[]getSeed(int numBytes)返回给定的种子字节数,使用此类用于种子自身的种子生成算法计算。protected intnext(int numBits)生成一个包含用户指定数量的伪随机位的整数(右对齐,前导零)。voidnextBytes(byte[] bytes)生成用户指定的随机字节数。voidnextBytes(byte[] bytes, SecureRandomParameters params)使用其他参数生成用户指定数量的随机字节。voidreseed()使用从其熵源读取的熵输入重新选择此SecureRandom。voidreseed(SecureRandomParameters params)使用附加参数从其熵源读取的熵输入重新选择此SecureRandom。voidsetSeed(byte[] seed)使用给定的种子重新种植此随机对象。voidsetSeed(long seed)使用给定long seed包含的八个字节重新设置此随机对象。StringtoString()返回此SecureRandom的人类可读字符串表示SecureRandom。
-
-
-
构造方法详细信息
-
SecureRandom
public SecureRandom()
构造一个实现默认随机数算法的安全随机数发生器(RNG)。此构造函数遍历已注册的安全提供程序列表,从最首选的提供程序开始。 将
SecureRandomSpi从第一个支持SecureRandom(RNG)算法的Provider封装SecureRandomSpi实现的新SecureRandom对象。 如果没有提供者支持RNG算法,则返回特定于实现的默认值。请注意,可以通过
Security.getProviders()方法检索已注册提供商的列表。见
SecureRandom在部分Java Security Standard Algorithm Names Specification有关标准RNG算法名称的信息。
-
SecureRandom
public SecureRandom(byte[] seed)
构造一个实现默认随机数算法的安全随机数发生器(RNG)。SecureRandom实例以指定的种子字节播种。此构造函数遍历已注册的安全提供程序列表,从最首选的提供程序开始。 将
SecureRandomSpi从第一个支持SecureRandom(RNG)算法的Provider封装SecureRandomSpi实现的新SecureRandom对象。 如果没有提供者支持RNG算法,则返回特定于实现的默认值。请注意,可以通过
Security.getProviders()方法检索已注册提供程序的列表。见
SecureRandom在部分Java Security Standard Algorithm Names Specification有关标准RNG算法名称的信息。- 参数
-
seed- 种子。
-
SecureRandom
protected SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider)
创建一个SecureRandom对象。- 参数
-
secureRandomSpi-SecureRandom实施。 -
provider- 提供者。
-
-
方法详细信息
-
getInstance
public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException
返回实现指定的随机数生成器(RNG)算法的SecureRandom对象。此方法遍历已注册的安全提供程序列表,从最首选的提供程序开始。 将
SecureRandomSpi从第一个支持指定算法的Provider封装SecureRandomSpi实现的新SecureRandom对象。请注意,可以通过
Security.getProviders()方法检索已注册提供商的列表。- Implementation Note:
-
JDK Reference Implementation还使用
jdk.security.provider.preferredSecurity属性来确定指定算法的首选提供程序顺序。 这可能与Security.getProviders()返回的提供商的顺序不同。 - 参数
-
algorithm- RNG算法的名称。 见SecureRandom在部分Java Security Standard Algorithm Names Specification有关标准RNG算法名称的信息。 - 结果
-
新的
SecureRandom对象 - 异常
-
NoSuchAlgorithmException- 如果没有Provider支持指定算法的SecureRandomSpi实现 -
NullPointerException- 如果algorithm是null - 从以下版本开始:
- 1.2
- 另请参见:
-
Provider
-
getInstance
public static SecureRandom getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
返回实现指定的随机数生成器(RNG)算法的SecureRandom对象。将返回从指定提供程序封装
SecureRandomSpi实现的新SecureRandom对象。 必须在安全提供程序列表中注册指定的提供程序。请注意,可以通过
Security.getProviders()方法检索已注册提供商的列表。- 参数
-
algorithm- RNG算法的名称。 见SecureRandom在部分Java Security Standard Algorithm Names Specification有关标准RNG算法名称的信息。 -
provider- 提供者的名称。 - 结果
-
新的
SecureRandom对象 - 异常
-
IllegalArgumentException- 如果提供程序名称为null或为空 -
NoSuchAlgorithmException- 如果指定的提供程序无法使用指定算法的SecureRandomSpi实现 -
NoSuchProviderException- 如果指定的提供程序未在安全提供程序列表中注册 -
NullPointerException- 如果algorithm是null - 从以下版本开始:
- 1.2
- 另请参见:
-
Provider
-
getInstance
public static SecureRandom getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
返回实现指定的随机数生成器(RNG)算法的SecureRandom对象。将
SecureRandom一个新的SecureRandom对象,该对象封装了指定的Provider对象中的SecureRandomSpi实现。 请注意,指定的Provider对象不必在提供程序列表中注册。- 参数
-
algorithm- RNG算法的名称。 见SecureRandom在部分Java Security Standard Algorithm Names Specification有关标准RNG算法名称的信息。 -
provider- 提供者。 - 结果
-
新的
SecureRandom对象 - 异常
-
IllegalArgumentException- 如果指定的提供者是null -
NoSuchAlgorithmException- 如果指定的算法的SecureRandomSpi实现不可用于指定的Provider对象 -
NullPointerException- 如果algorithm是null - 从以下版本开始:
- 1.4
- 另请参见:
-
Provider
-
getInstance
public static SecureRandom getInstance(String algorithm, SecureRandomParameters params) throws NoSuchAlgorithmException
返回实现指定的随机数生成器(RNG)算法的SecureRandom对象,并支持指定的SecureRandomParameters请求。此方法遍历已注册的安全提供程序列表,从最首选的提供程序开始。 将
SecureRandom一个新的SecureRandom对象,该对象从第一个支持指定算法的Provider和指定的SecureRandomParameters封装SecureRandomSpi实现。请注意,可以通过
Security.getProviders()方法检索已注册提供程序的列表。- Implementation Note:
-
JDK Reference Implementation还使用
jdk.security.provider.preferred属性来确定指定算法的首选提供程序顺序。 这可能与Security.getProviders()返回的提供商的顺序不同。 - 参数
-
algorithm- RNG算法的名称。 见SecureRandom在部分Java Security Standard Algorithm Names Specification有关标准RNG算法名称的信息。 -
params-SecureRandomParameters新创建的SecureRandom对象必须支持。 - 结果
-
新的
SecureRandom对象 - 异常
-
IllegalArgumentException- 如果指定的参数是null -
NoSuchAlgorithmException- 如果没有Provider支持指定算法和参数的SecureRandomSpi实现 -
NullPointerException- 如果algorithm是null - 从以下版本开始:
- 9
- 另请参见:
-
Provider
-
getInstance
public static SecureRandom getInstance(String algorithm, SecureRandomParameters params, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
返回实现指定的随机数生成器(RNG)算法的SecureRandom对象,并支持指定的SecureRandomParameters请求。将返回从指定提供程序封装
SecureRandomSpi实现的新SecureRandom对象。 必须在安全提供程序列表中注册指定的提供程序。请注意,可以通过
Security.getProviders()方法检索已注册提供商的列表。- 参数
-
algorithm- RNG算法的名称。 见SecureRandom在部分Java Security Standard Algorithm Names Specification有关标准RNG算法名称的信息。 -
params-SecureRandomParameters新创建的SecureRandom对象必须支持。 -
provider- 提供者的名称。 - 结果
-
新的
SecureRandom对象 - 异常
-
IllegalArgumentException- 如果提供者名称为null或为空,或者params为null -
NoSuchAlgorithmException- 如果指定的提供程序不支持指定算法和参数的SecureRandomSpi实现 -
NoSuchProviderException- 如果指定的提供程序未在安全提供程序列表中注册 -
NullPointerException- 如果algorithm是null - 从以下版本开始:
- 9
- 另请参见:
-
Provider
-
getInstance
public static SecureRandom getInstance(String algorithm, SecureRandomParameters params, Provider provider) throws NoSuchAlgorithmException
返回实现指定的随机数生成器(RNG)算法的SecureRandom对象,并支持指定的SecureRandomParameters请求。将
SecureRandom一个新的SecureRandom对象,该对象封装了指定的Provider对象中的SecureRandomSpi实现。 请注意,指定的Provider对象不必在提供程序列表中注册。- 参数
-
algorithm- RNG算法的名称。 见SecureRandom在部分Java Security Standard Algorithm Names Specification有关标准RNG算法名称的信息。 -
params-SecureRandomParameters新创建的SecureRandom对象必须支持。 -
provider- 提供者。 - 结果
-
新的
SecureRandom对象 - 异常
-
IllegalArgumentException- 如果指定的提供者或参数是null -
NoSuchAlgorithmException- 如果指定的提供程序不支持指定算法和参数的SecureRandomSpi实现 -
NullPointerException- 如果algorithm是null - 从以下版本开始:
- 9
- 另请参见:
-
Provider
-
getProvider
public final Provider getProvider()
返回此SecureRandom对象的提供程序。- 结果
-
此
SecureRandom对象的提供者。
-
getAlgorithm
public String getAlgorithm()
返回此SecureRandom对象实现的算法的名称。- 结果
-
如果无法确定算法名称,则为算法名称或
unknown。 - 从以下版本开始:
- 1.5
-
toString
public String toString()
返回此SecureRandom的人类可读字符串表示SecureRandom。
-
getParameters
public SecureRandomParameters getParameters()
返回此SecureRandom实例的有效SecureRandomParameters。返回值可以与传递到
getInstance方法的SecureRandomParameters对象不同,但在此SecureRandom对象的生命周期内不能更改。调用者可以使用返回的值来查找此
SecureRandom支持的功能。- 结果
-
有效的
SecureRandomParameters参数,如果没有使用参数null。 - 从以下版本开始:
- 9
- 另请参见:
-
SecureRandomSpi
-
setSeed
public void setSeed(byte[] seed)
使用给定的种子重新种植此随机对象。 种子补充而不是替代现有的种子。 因此,保证重复呼叫永远不会减少随机性。一个PRNG
SecureRandom如果不会自动种子本身setSeed之前任何所谓的nextBytes或reseed电话。 调用者应确保seed参数包含足够的熵以保证此SecureRandom的安全性。- 参数
-
seed- 种子 - 另请参见:
-
getSeed(int)
-
setSeed
public void setSeed(long seed)
使用给定long seed包含的八个字节重新设置此随机对象。 给定的种子补充而不是替代现有的种子。 因此,保证重复呼叫永远不会减少随机性。定义此方法是为了与
java.util.Random兼容。- 重写:
-
setSeed在课程Random - 参数
-
seed- 种子 - 另请参见:
-
getSeed(int)
-
nextBytes
public void nextBytes(byte[] bytes)
生成用户指定的随机字节数。
-
nextBytes
public void nextBytes(byte[] bytes, SecureRandomParameters params)使用其他参数生成用户指定数量的随机字节。- 参数
-
bytes- 用随机字节填充的数组 -
params- 其他参数 - 异常
-
NullPointerException- 如果bytes为空 -
UnsupportedOperationException- 如果基础提供程序实现未覆盖此方法 -
IllegalArgumentException- 如果params是null,非法或不受此SecureRandom不支持 - 从以下版本开始:
- 9
-
next
protected final int next(int numBits)
生成一个包含用户指定数量的伪随机位的整数(右对齐,前导零)。 此方法重写一个java.util.Random方法,以及用于提供随机比特的源到所有的从类继承的方法(例如,nextInt,nextLong,和nextFloat)。
-
getSeed
public static byte[] getSeed(int numBytes)
返回给定的种子字节数,使用此类用于种子自身的种子生成算法计算。 该调用可用于播种其他随机数生成器。此方法仅用于向后兼容。 建议调用者使用备用
getInstance方法之一获取SecureRandom对象,然后调用generateSeed方法从该对象获取种子字节。- 参数
-
numBytes- 要生成的种子字节数。 - 结果
- 种子字节。
- 异常
-
IllegalArgumentException- 如果numBytes是负数 - 另请参见:
-
setSeed(byte[])
-
generateSeed
public byte[] generateSeed(int numBytes)
返回给定的种子字节数,使用此类用于种子自身的种子生成算法计算。 该调用可用于播种其他随机数生成器。- 参数
-
numBytes- 要生成的种子字节数。 - 结果
- 种子字节。
- 异常
-
IllegalArgumentException- 如果numBytes为负数
-
getInstanceStrong
public static SecureRandom getInstanceStrong() throws NoSuchAlgorithmException
返回使用securerandom.strongAlgorithmsSecurity属性中指定的算法/提供程序选择的SecureRandom对象。某些情况需要强大的随机值,例如在创建RSA公钥/私钥等高价值/长期密码时。 为了帮助指导应用程序选择合适的强大的
SecureRandom实现,Java发行版包含securerandom.strongAlgorithmsSecurity属性中已知的强大的SecureRandom实现列表。Java平台的每个实现都需要支持至少一个强大的
SecureRandom实现。- 结果
-
强大的
SecureRandom实现,如securerandom.strongAlgorithmsSecurity属性所示 - 异常
-
NoSuchAlgorithmException- 如果没有可用的算法 - 从以下版本开始:
- 1.8
- 另请参见:
-
Security.getProperty(String)
-
reseed
public void reseed()
使用从其熵源读取的熵输入重新设定此SecureRandom。- 异常
-
UnsupportedOperationException- 如果基础提供程序实现未覆盖此方法。 - 从以下版本开始:
- 9
-
reseed
public void reseed(SecureRandomParameters params)
使用附加参数从其熵源读取的熵输入重新选择此SecureRandom。注意,熵是从熵源获得的。 虽然
params一些数据可能包含熵,但其主要用途是提供多样性。- 参数
-
params- 额外参数 - 异常
-
UnsupportedOperationException- 如果基础提供程序实现未覆盖此方法。 -
IllegalArgumentException- 如果params是null,非法或不受此SecureRandom不支持 - 从以下版本开始:
- 9
-
-