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:egovframework.rte.tex.com.service.EgovStringUtil.java

License:asdf

/**
 * ? A? Z?? ? ??  ?  ? ? ??/*from   w w  w  . j a v a2s . c  o  m*/
 * ? ??  
 * @param startChr
 *        -  ?
 * @param endChr
 *        - ?
 * @return ??
 * @exception MyException
 * @see
 */
public static String getRandomStr(char startChr, char endChr) {

    int randomInt;
    String randomStr = null;

    // ? ? ? ? .
    int startInt = Integer.valueOf(startChr);
    int endInt = Integer.valueOf(endChr);

    // ?? ? ?
    if (startInt > endInt) {

        throw new IllegalArgumentException("Start String: " + startChr + " End String: " + endChr);
    }

    try {

        // ? ? ?
        SecureRandom rnd = new SecureRandom();

        do {

            // ? ? ? ? ? ? ?.
            randomInt = rnd.nextInt(endInt + 1);

        } while (randomInt < startInt); // ? ?
        // 'A'(65)
        //  
        // ? ?
        // ?.

        // ? ? ?   ?  
        randomStr = (char) randomInt + "";

    } catch (Exception e) {

        log.debug(e.getMessage());

    }

    // ?? 
    return randomStr;

}

From source file:org.sufficientlysecure.keychain.ui.CreateYubiKeyPinFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.create_yubi_key_pin_fragment, container, false);

    mPin = (TextView) view.findViewById(R.id.create_yubi_key_pin);
    mAdminPin = (TextView) view.findViewById(R.id.create_yubi_key_admin_pin);
    mBackButton = view.findViewById(R.id.create_key_back_button);
    mNextButton = view.findViewById(R.id.create_key_next_button);

    if (mCreateKeyActivity.mYubiKeyPin == null) {
        new AsyncTask<Void, Void, Pair<Passphrase, Passphrase>>() {
            @Override// ww  w  .  ja  v  a  2  s . co m
            protected Pair<Passphrase, Passphrase> doInBackground(Void... unused) {
                SecureRandom secureRandom = new SecureRandom();
                // min = 6, we choose 6
                String pin = "" + secureRandom.nextInt(9) + secureRandom.nextInt(9) + secureRandom.nextInt(9)
                        + secureRandom.nextInt(9) + secureRandom.nextInt(9) + secureRandom.nextInt(9);
                // min = 8, we choose 10, but 6 are equals the PIN
                String adminPin = pin + secureRandom.nextInt(9) + secureRandom.nextInt(9)
                        + secureRandom.nextInt(9) + secureRandom.nextInt(9);

                return new Pair<>(new Passphrase(pin), new Passphrase(adminPin));
            }

            @Override
            protected void onPostExecute(Pair<Passphrase, Passphrase> pair) {
                mCreateKeyActivity.mYubiKeyPin = pair.first;
                mCreateKeyActivity.mYubiKeyAdminPin = pair.second;

                mPin.setText(mCreateKeyActivity.mYubiKeyPin.toStringUnsafe());
                mAdminPin.setText(mCreateKeyActivity.mYubiKeyAdminPin.toStringUnsafe());
            }
        }.execute();
    } else {
        mPin.setText(mCreateKeyActivity.mYubiKeyPin.toStringUnsafe());
        mAdminPin.setText(mCreateKeyActivity.mYubiKeyAdminPin.toStringUnsafe());
    }

    mBackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            back();
        }
    });
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nextClicked();
        }
    });

    return view;
}

From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.virtual.VirtualHardwareManager.java

private int getRandom(int max, int min, int current, boolean isSmoothed, int svf) {
    if (isSmoothed) {
        int offset = (max - min) * svf / 100;
        double mx = current + offset;
        max = (mx > max) ? max : (int) Math.round(mx);
        double mn = current - offset;
        min = (mn < min) ? min : (int) Math.round(mn);
    }/*from w w  w  . j av  a  2 s.co  m*/
    try {
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        return secureRandom.nextInt(max - min) + min;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("SHA1PRNG algorithm could not be found.");
    }
}

From source file:com.LaunchKeyManager.http.SimpleMultipartEntity.java

public SimpleMultipartEntity() {
    final StringBuffer buf = new StringBuffer();
    final SecureRandom rand = new SecureRandom();
    for (int i = 0; i < 30; i++) {
        buf.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
    }//from  w w  w .  java  2  s  .c o  m
    this.boundary = buf.toString();

}

From source file:com._17od.upm.gui.AccountDialog.java

/**
 * This method takes as input the user's preferences about password length,
 * including or excluding Escape Characters, and randomly generates a
 * password. Then, the method returns the generated password as a String.
 *
 * @param PassLength/*from  ww  w . jav a 2  s.c  om*/
 * @param InclEscChars
 * @return passwordBuffer.toString()
 */
private static String GeneratePassword(int PassLength, boolean InclEscChars) {
    SecureRandom random = new SecureRandom();
    StringBuffer passwordBuffer = new StringBuffer();

    if (InclEscChars) {
        for (int i = 0; i < PassLength; i++) {
            passwordBuffer.append(EXTRA_ALLOWED_CHARS[random.nextInt(EXTRA_ALLOWED_CHARS.length)]);
        }
        return passwordBuffer.toString();

    } else {
        for (int i = 0; i < PassLength; i++) {
            passwordBuffer.append(ALLOWED_CHARS[random.nextInt(ALLOWED_CHARS.length)]);
        }
        return passwordBuffer.toString();
    }
}

From source file:es.logongas.openshift.ant.JenkinsPasswordHashPropertyTask.java

/**
 * The MIT License// www  . ja va 2 s  .c o m
 * 
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, David Calavera, Seiji Sogabe
 * Generates random salt.
 */
private String generateSalt() {
    StringBuilder buf = new StringBuilder();
    SecureRandom sr = new SecureRandom();
    for (int i = 0; i < 6; i++) {// log2(52^6)=34.20... so, this is about 32bit strong.
        boolean upper = sr.nextBoolean();
        char ch = (char) (sr.nextInt(26) + 'a');
        if (upper) {
            ch = Character.toUpperCase(ch);
        }
        buf.append(ch);
    }
    return buf.toString();
}

From source file:hudson.scm.credential.SshPublicKeyCredential.java

/**
 * @param keyFile stores SSH private key. The file will be copied.
 *//*from w  w  w  .java 2  s  .c om*/
public SshPublicKeyCredential(String userName, String passphrase, File keyFile) throws SVNException {
    this.userName = userName;
    this.passphrase = Scrambler.scramble(passphrase);

    SecureRandom r = new SecureRandom();
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < 16; i++) {
        buf.append(Integer.toHexString(r.nextInt(16)));
    }
    this.id = buf.toString();

    try {
        File savedKeyFile = getKeyFile();
        System.out.println(keyFile.getAbsolutePath());
        System.out.println(savedKeyFile.getAbsolutePath());
        FileUtils.copyFile(keyFile, savedKeyFile);
        setFilePermissions(savedKeyFile, "600");
    } catch (IOException e) {
        throw new SVNException(SVNErrorMessage.create(SVNErrorCode.AUTHN_CREDS_UNAVAILABLE,
                Messages.SshPublicKeyCredential_private_key_save_error()), e);
    }
}

From source file:org.wso2.carbon.identity.recovery.handler.AdminForcedPasswordResetHandler.java

private String generateOTPValue() {
    char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    SecureRandom rnd = new SecureRandom();
    StringBuilder sb = new StringBuilder("");
    for (int i = 0; i < 6; i++) {
        sb.append(chars[rnd.nextInt(chars.length)]);
    }/*from w w w .  ja va2s . c  o  m*/
    return sb.toString();
}

From source file:org.sufficientlysecure.keychain.ui.CreateSecurityTokenPinFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.create_yubi_key_pin_fragment, container, false);

    mPin = (EditText) view.findViewById(R.id.create_yubi_key_pin);
    mPinRepeat = (EditText) view.findViewById(R.id.create_yubi_key_pin_repeat);
    mAdminPin = (TextView) view.findViewById(R.id.create_yubi_key_admin_pin);
    mBackButton = view.findViewById(R.id.create_key_back_button);
    mNextButton = view.findViewById(R.id.create_key_next_button);

    if (mCreateKeyActivity.mSecurityTokenPin == null) {
        new AsyncTask<Void, Void, Passphrase>() {
            @Override/*from w ww .  j a v  a  2  s  .  c  o m*/
            protected Passphrase doInBackground(Void... unused) {
                SecureRandom secureRandom = new SecureRandom();
                // min = 8, we choose 8
                String adminPin = "" + secureRandom.nextInt(9) + secureRandom.nextInt(9)
                        + secureRandom.nextInt(9) + secureRandom.nextInt(9) + secureRandom.nextInt(9)
                        + secureRandom.nextInt(9) + secureRandom.nextInt(9) + secureRandom.nextInt(9);

                return new Passphrase(adminPin);
            }

            @Override
            protected void onPostExecute(Passphrase adminPin) {
                mCreateKeyActivity.mSecurityTokenAdminPin = adminPin;

                mAdminPin.setText(mCreateKeyActivity.mSecurityTokenAdminPin.toStringUnsafe());
            }
        }.execute();
    } else {
        mAdminPin.setText(mCreateKeyActivity.mSecurityTokenAdminPin.toStringUnsafe());
    }

    mPin.requestFocus();
    mBackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            back();
        }
    });
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nextClicked();
        }
    });

    return view;
}

From source file:de.metas.procurement.webui.service.impl.LoginService.java

private final String generatePassword(final int passwordLength) {
    Preconditions.checkArgument(passwordLength >= 4, "paswordLength shall be at least 4");
    final StringBuilder password = new StringBuilder();
    final SecureRandom random = new SecureRandom();
    final int poolLength = PASSWORD_CHARS.length();
    for (int i = 0; i < passwordLength; i++) {
        final int charIndex = random.nextInt(poolLength);
        final char ch = PASSWORD_CHARS.charAt(charIndex);
        password.append(ch);/*  w  w w  .j ava  2  s. com*/
    }

    return password.toString();
}