Example usage for java.security SecureRandom nextInt

List of usage examples for java.security SecureRandom nextInt

Introduction

In this page you can find the example usage for java.security SecureRandom nextInt.

Prototype

public int nextInt(int bound) 

Source Link

Document

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Usage

From source file:net.solarnetwork.node.setup.impl.DefaultKeystoreService.java

private String generateNewKeyStorePassword() {
    String manualKeyStorePassword = super.getKeyStorePassword();
    if (manualKeyStorePassword != null && manualKeyStorePassword.length() > 0) {
        return manualKeyStorePassword;
    }//from  www .j  a va  2 s. co m
    try {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        final int start = 32;
        final int end = 126;
        final int range = end - start;
        char[] passwd = new char[PASSWORD_LENGTH];
        for (int i = 0; i < PASSWORD_LENGTH; i++) {
            passwd[i] = (char) (random.nextInt(range) + start);
        }
        return new String(passwd);
    } catch (NoSuchAlgorithmException e) {
        throw new CertificateException("Error creating random password", e);
    }
}

From source file:tv.phantombot.PhantomBot.java

public static String generateRandomString(int length) {
    String randomAllowed = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char[] randomChars = randomAllowed.toCharArray();
    char[] randomBuffer;

    randomBuffer = new char[length];
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < randomBuffer.length; i++) {
        randomBuffer[i] = randomChars[random.nextInt(randomChars.length)];
    }//  w w w  .j a va2s . com
    return new String(randomBuffer);
}

From source file:org.wso2.carbon.device.mgt.jaxrs.service.impl.UserManagementServiceImpl.java

private String generateInitialUserPassword() {
    int passwordLength = 6;
    //defining the pool of characters to be used for initial password generation
    String lowerCaseCharset = "abcdefghijklmnopqrstuvwxyz";
    String upperCaseCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String numericCharset = "0123456789";
    SecureRandom randomGenerator = new SecureRandom();
    String totalCharset = lowerCaseCharset + upperCaseCharset + numericCharset;
    int totalCharsetLength = totalCharset.length();
    StringBuilder initialUserPassword = new StringBuilder();
    for (int i = 0; i < passwordLength; i++) {
        initialUserPassword.append(totalCharset.charAt(randomGenerator.nextInt(totalCharsetLength)));
    }/*from   ww w.j av  a2  s  .  co  m*/
    if (log.isDebugEnabled()) {
        log.debug("Initial user password is created for new user: " + initialUserPassword);
    }
    return initialUserPassword.toString();
}

From source file:me.gloriouseggroll.quorrabot.Quorrabot.java

private static String generateWebAuth() {
    String randomAllowed = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char[] randomChars = randomAllowed.toCharArray();
    char[] randomBuffer;

    randomBuffer = new char[30];
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < randomBuffer.length; i++) {
        randomBuffer[i] = randomChars[random.nextInt(randomChars.length)];
    }/*from   w  ww  .j  av  a 2  s  .  c om*/
    return new String(randomBuffer);
}

From source file:me.gloriouseggroll.quorrabot.Quorrabot.java

/**
 * gen a random string//w  w  w .j  a  v  a2 s . c om
 */
private static String generateRandomString(int length) {
    String randomAllowed = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char[] randomChars = randomAllowed.toCharArray();
    char[] randomBuffer;

    randomBuffer = new char[length];
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < randomBuffer.length; i++) {
        randomBuffer[i] = randomChars[random.nextInt(randomChars.length)];
    }
    return new String(randomBuffer);
}

From source file:me.mast3rplan.phantombot.PhantomBot.java

private static String generateRandomString(int length) {
    String randomAllowed = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char[] randomChars = randomAllowed.toCharArray();
    char[] randomBuffer;

    randomBuffer = new char[length];
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < randomBuffer.length; i++) {
        randomBuffer[i] = randomChars[random.nextInt(randomChars.length)];
    }/* w w  w .  j  a  va2  s.  com*/
    return new String(randomBuffer);
}

From source file:org.sofun.platform.web.rest.resource.ejb.MemberResourceBean.java

@Override
public Response debitMember(Map<String, String> params) throws ReSTException {

    final String email = params.get("email");

    Member member = getCoreMemberByEmail(email);
    if (!MemberAccountStatus.VERIFIED_FR.equals(member.getAccountStatus())) {
        return Response.status(401).entity("Account has not been verified.").build();
    }// w w w  .j av a  2s . c o  m

    final float txnAmount = Float.valueOf(params.get("txn_amount"));
    final String txnCurrency = params.get("txn_currency");

    if (txnAmount == 0 || txnCurrency == null) {
        return Response.status(401).entity("Missing information.").build();
    }

    if (getMemberService().getTransferableAmountFor(member) < txnAmount) {
        return Response.status(401).entity("Balance is too low for such a wire.").build();
    }

    SecureRandom randomGenerator = new SecureRandom();
    MemberTransaction txn = new MemberTransactionImpl(new Date(), txnAmount, txnCurrency,
            MemberTransactionType.WIRE_DEBIT);
    txn.setLabel(MemberTransactionType.WIRE_DEBIT);
    txn.setDebit(true);
    txn.setCredit(false);
    // txnId will be updated after the actual bank transaction will be
    // performed out of process.
    txn.setTransactionId(String.valueOf(randomGenerator.nextInt(1000000000)));
    member.addTransaction(txn);
    txn.setMember(member);

    return Response.status(202).entity("OK").build();

}

From source file:com.vmware.identity.idm.server.config.directory.DirectoryConfigStore.java

private String randomKey() {
    SecureRandom random = new SecureRandom();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 16; i++)
        sb.append((char) (random.nextInt(96) + 32));
    return sb.toString();
}

From source file:com.vmware.identity.idm.server.IdentityManager.java

private String createSessionId() {
    SecureRandom randomGenerator = new SecureRandom();
    int sessionId = randomGenerator.nextInt(1000000);
    return String.valueOf(sessionId);
}