当前位置: 首页>>代码示例>>Java>>正文


Java SecureRandom.nextBytes方法代码示例

本文整理汇总了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();
}
 
开发者ID:hantsy,项目名称:javaee8-jaxrs-sample,代码行数:26,代码来源:BCrypt.java

示例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();
}
 
开发者ID:kalsowerus,项目名称:Guestbook9001,代码行数:27,代码来源:BCrypt.java

示例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.");
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:19,代码来源:SAML1ArtifactType0002Builder.java

示例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);
}
 
开发者ID:yuweijun,项目名称:cas-server-4.2.1,代码行数:26,代码来源:AbstractSaml20ObjectBuilder.java

示例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);
    }
}
 
开发者ID:wolfSSL,项目名称:wolfcrypt-jni,代码行数:20,代码来源:WolfCryptRandomTest.java

示例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());
  }
}
 
开发者ID:google,项目名称:webauthndemo,代码行数:23,代码来源:AndroidSafetyNetAttestationStatementTest.java

示例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);
}
 
开发者ID:yuweijun,项目名称:cas-server-4.2.1,代码行数:15,代码来源:EncryptedMapDecorator.java

示例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);
}
 
开发者ID:hotpads,项目名称:datarouter,代码行数:12,代码来源:DefaultCsrfValidator.java

示例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);
	}
}
 
开发者ID:EsupPortail,项目名称:esup-ecandidat,代码行数:23,代码来源:PasswordHashServicePBKDF2.java

示例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);
}
 
开发者ID:mit-cml,项目名称:appinventor-extensions,代码行数:20,代码来源:PasswordHash.java

示例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.");
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:28,代码来源:SAML2ArtifactType0004Builder.java

示例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);
	}
 
开发者ID:how2j,项目名称:lazycat,代码行数:10,代码来源:SessionIdGeneratorBase.java

示例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;
}
 
开发者ID:vrk-kpa,项目名称:xrd4j,代码行数:12,代码来源:CryptoHelper.java

示例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);
}
 
开发者ID:quan-to,项目名称:react-native-pgp,代码行数:9,代码来源:Module.java

示例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;
        }
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:13,代码来源:DSA.java


注:本文中的java.security.SecureRandom.nextBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。