本文整理汇总了Java中java.security.SecureRandom.nextBytes方法的典型用法代码示例。如果您正苦于以下问题:Java SecureRandom.nextBytes方法的具体用法?Java SecureRandom.nextBytes怎么用?Java SecureRandom.nextBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.SecureRandom
的用法示例。
在下文中一共展示了SecureRandom.nextBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: gensalt
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Generate a salt for use with the BCrypt.hashpw() method
* @param log_rounds the log2 of the number of rounds of hashing to apply - the work
* factor therefore increases as 2**log_rounds. Minimum 4, maximum 31.
* @param random an instance of SecureRandom to use
* @return an encoded salt value
*/
public static String gensalt(int log_rounds, SecureRandom random) {
if (log_rounds < MIN_LOG_ROUNDS || log_rounds > MAX_LOG_ROUNDS) {
throw new IllegalArgumentException("Bad number of rounds");
}
StringBuilder rs = new StringBuilder();
byte rnd[] = new byte[BCRYPT_SALT_LEN];
random.nextBytes(rnd);
rs.append("$2a$");
if (log_rounds < 10) {
rs.append("0");
}
rs.append(log_rounds);
rs.append("$");
encode_base64(rnd, rnd.length, rs);
return rs.toString();
}
示例2: gensalt
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Generate a salt for use with the BCrypt.hashpw() method
* @param log_rounds the log2 of the number of rounds of
* hashing to apply - the work factor therefore increases as
* 2**log_rounds.
* @param random an instance of SecureRandom to use
* @return an encoded salt value
*/
public static String gensalt(int log_rounds, SecureRandom random) {
StringBuffer rs = new StringBuffer();
byte rnd[] = new byte[BCRYPT_SALT_LEN];
random.nextBytes(rnd);
rs.append("$2a$");
if (log_rounds < 10)
rs.append("0");
if (log_rounds > 30) {
throw new IllegalArgumentException(
"log_rounds exceeds maximum (30)");
}
rs.append(Integer.toString(log_rounds));
rs.append("$");
rs.append(encode_base64(rnd, rnd.length));
return rs.toString();
}
示例3: buildArtifact
import java.security.SecureRandom; //导入方法依赖的package包/类
/** {@inheritDoc} */
public SAML1ArtifactType0002 buildArtifact(
SAMLMessageContext<RequestAbstractType, Response, NameIdentifier> requestContext, Assertion assertion) {
try {
String sourceLocation = getSourceLocation(requestContext);
if (sourceLocation == null) {
return null;
}
SecureRandom handleGenerator = SecureRandom.getInstance("SHA1PRNG");
byte[] assertionHandle = new byte[20];
handleGenerator.nextBytes(assertionHandle);
return new SAML1ArtifactType0002(assertionHandle, sourceLocation);
} catch (NoSuchAlgorithmException e) {
log.error("JVM does not support required cryptography algorithms: SHA1PRNG.", e);
throw new InternalError("JVM does not support required cryptography algorithms: SHA1PRNG.");
}
}
示例4: generateSecureRandomId
import java.security.SecureRandom; //导入方法依赖的package包/类
@Override
public String generateSecureRandomId() {
final SecureRandom generator = new SecureRandom();
final char[] charMappings = {
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p'};
final int charsLength = 40;
final int generatorBytesLength = 20;
final int shiftLength = 4;
// 160 bits
final byte[] bytes = new byte[generatorBytesLength];
generator.nextBytes(bytes);
final char[] chars = new char[charsLength];
for (int i = 0; i < bytes.length; i++) {
final int left = bytes[i] >> shiftLength & HEX_HIGH_BITS_BITWISE_FLAG;
final int right = bytes[i] & HEX_HIGH_BITS_BITWISE_FLAG;
chars[i * 2] = charMappings[left];
chars[i * 2 + 1] = charMappings[right];
}
return String.valueOf(chars);
}
示例5: testNextBytes
import java.security.SecureRandom; //导入方法依赖的package包/类
@Test
public void testNextBytes()
throws NoSuchProviderException, NoSuchAlgorithmException {
byte[] valuesA = new byte[128];
byte[] valuesB = new byte[128];
SecureRandom rand = SecureRandom.getInstance("HashDRBG", "wolfJCE");
rand.nextBytes(valuesA);
for (int i = 0; i < 10; i++) {
rand.nextBytes(valuesB);
if(Arrays.equals(valuesA, valuesB))
fail("SecureRandom generated two equal consecutive arrays");
valuesA = Arrays.copyOf(valuesB, valuesB.length);
}
}
示例6: testEncode
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Test method for
* {@link com.google.webauthn.gaedemo.objects.AndroidSafetyNetAttestationStatement#encode()} and
* {@link com.google.webauthn.gaedemo.objects.AndroidSafetyNetAttestationStatement#decode(co.nstant.in.cbor.model.DataItem)}.
*/
@Test
public void testEncode() {
SecureRandom random = new SecureRandom();
AndroidSafetyNetAttestationStatement attStmt = new AndroidSafetyNetAttestationStatement();
attStmt.ver = "10";
attStmt.response = new byte[20];
random.nextBytes(attStmt.response);
try {
DataItem encoded = attStmt.encode();
AndroidSafetyNetAttestationStatement decoded =
AndroidSafetyNetAttestationStatement.decode(encoded);
assertEquals(decoded, attStmt);
} catch (CborException | ResponseException e) {
fail(e.getMessage());
}
}
示例7: getRandomSalt
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Gets the random salt.
*
* @param size the size
* @return the random salt
*/
private static String getRandomSalt(final int size) {
final SecureRandom secureRandom = new SecureRandom();
final byte[] bytes = new byte[size];
secureRandom.nextBytes(bytes);
return getFormattedText(bytes);
}
示例8: generateCsrfIv
import java.security.SecureRandom; //导入方法依赖的package包/类
public static String generateCsrfIv(){
SecureRandom sr;
try{
sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch(NoSuchAlgorithmException | NoSuchProviderException e){
throw new RuntimeException("error in SecureRandom.getInstance()");
}
byte[] salt = new byte[16];
sr.nextBytes(salt);
return Base64.getEncoder().encodeToString(salt);
}
示例9: createHash
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Returns a salted PBKDF2 hash of the password.
*
* @param password
* the password to hash
* @return a salted PBKDF2 hash of the password
* @throws CustomException
*/
public String createHash(char[] password) throws CustomException {
try {
// Generate a random salt
SecureRandom random = new SecureRandom();
byte[] salt = new byte[SALT_BYTE_SIZE];
random.nextBytes(salt);
// Hash the password
byte[] hash = pbkdf2(password, salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
return PBKDF2_ITERATIONS + ":" + toHex(salt) + ":" + toHex(hash);
}catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new CustomException(e);
}
}
示例10: createHash
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Returns a salted PBKDF2 hash of the password.
*
* @param password the password to hash
* @return a salted PBKDF2 hash of the password
*/
public static String createHash(char[] password)
throws NoSuchAlgorithmException, InvalidKeySpecException
{
// Generate a random salt
SecureRandom random = new SecureRandom();
byte[] salt = new byte[SALT_BYTE_SIZE];
random.nextBytes(salt);
// Hash the password
byte[] hash = pbkdf2(password, salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
// format iterations:salt:hash
return PBKDF2_ITERATIONS + ":" + toHex(salt) + ":" + toHex(hash);
}
示例11: buildArtifact
import java.security.SecureRandom; //导入方法依赖的package包/类
/** {@inheritDoc} */
public SAML2ArtifactType0004 buildArtifact(SAMLMessageContext<SAMLObject, SAMLObject, NameID> requestContext) {
try {
IndexedEndpoint acsEndpoint = (IndexedEndpoint) getAcsEndpoint(requestContext);
if (acsEndpoint == null) {
return null;
}
byte[] endpointIndex = DatatypeHelper.intToByteArray(acsEndpoint.getIndex());
byte[] trimmedIndex = new byte[2];
trimmedIndex[0] = endpointIndex[2];
trimmedIndex[1] = endpointIndex[3];
MessageDigest sha1Digester = MessageDigest.getInstance("SHA-1");
byte[] source = sha1Digester.digest(requestContext.getLocalEntityId().getBytes());
SecureRandom handleGenerator = SecureRandom.getInstance("SHA1PRNG");
byte[] assertionHandle;
assertionHandle = new byte[20];
handleGenerator.nextBytes(assertionHandle);
return new SAML2ArtifactType0004(trimmedIndex, source, assertionHandle);
} catch (NoSuchAlgorithmException e) {
log.error("JVM does not support required cryptography algorithms: SHA-1/SHA1PRNG.", e);
throw new InternalError("JVM does not support required cryptography algorithms: SHA-1/SHA1PRNG.");
}
}
示例12: getRandomBytes
import java.security.SecureRandom; //导入方法依赖的package包/类
protected void getRandomBytes(byte bytes[]) {
SecureRandom random = randoms.poll();
if (random == null) {
random = createSecureRandom();
}
random.nextBytes(bytes);
randoms.add(random);
}
示例13: generateIV
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Generates a new initialization vector (IV).
*
* @return new initialization vector (IV)
*/
public static byte[] generateIV() {
SecureRandom random = new SecureRandom();
byte[] iv = new byte[AES_BLOCK_SIZE];
random.nextBytes(iv);
return iv;
}
示例14: randomBytes
import java.security.SecureRandom; //导入方法依赖的package包/类
@ReactMethod
public void randomBytes(int size, Callback success) {
SecureRandom sr = new SecureRandom();
byte[] output = new byte[size];
sr.nextBytes(output);
String string = Base64.encodeToString(output, Base64.DEFAULT);
success.invoke(null, string);
}
示例15: generateK
import java.security.SecureRandom; //导入方法依赖的package包/类
protected BigInteger generateK(BigInteger q) {
SecureRandom random = getSigningRandom();
byte[] kValue = new byte[q.bitLength()/8];
while (true) {
random.nextBytes(kValue);
BigInteger k = new BigInteger(1, kValue).mod(q);
if (k.signum() > 0 && k.compareTo(q) < 0) {
return k;
}
}
}