List of usage examples for java.security SecureRandom nextInt
public int nextInt(int bound)
From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils.java
/** * Attempts to find a free port between the MIN_PORT_NUMBER(9000) and MAX_PORT_NUMBER(11000). * Tries 'RANDOMLY picked' port numbers between this range up-until "randomAttempts" number of * times. If still fails, then tries each port in descending order from the MAX_PORT_NUMBER * whilst skipping already attempted ones via random selection. * * @param randomAttempts no of times to TEST port numbers picked randomly over the given range * @return an available/free port/* w ww .j a v a 2 s. c o m*/ */ public static synchronized int getAvailablePort(int randomAttempts) { ArrayList<Integer> failedPorts = new ArrayList<Integer>(randomAttempts); try { SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); int randomPort = MAX_PORT_NUMBER; while (randomAttempts > 0) { randomPort = secureRandom.nextInt(MAX_PORT_NUMBER - MIN_PORT_NUMBER) + MIN_PORT_NUMBER; if (checkIfPortAvailable(randomPort)) { return randomPort; } failedPorts.add(randomPort); randomAttempts--; } randomPort = MAX_PORT_NUMBER; while (true) { if (!failedPorts.contains(randomPort) && checkIfPortAvailable(randomPort)) { return randomPort; } randomPort--; } } catch (NoSuchAlgorithmException e) { throw new RuntimeException("SHA1PRNG algorithm could not be found."); } }
From source file:egovframework.oe1.utl.fcc.service.EgovNumberUtil.java
public static int getRandomNum(int startNum, int endNum) { int randomNum = 0; try {/*ww w . j a v a 2 s .c o m*/ // ? ? ? SecureRandom rnd = new SecureRandom(); do { // ?? ? ? ?. randomNum = rnd.nextInt(endNum + 1); } while (randomNum < startNum); // ? ? // ? // ? // ?? // ?. } catch (Exception e) { //log.debug(e.getMessage()); log.trace(e.getMessage()); } return randomNum; }
From source file:egovframework.rte.tex.com.service.EgovNumberUtil.java
public static int getRandomNum(int startNum, int endNum) { int randomNum = 0; try {/*from w ww .j av a2 s .c om*/ // ? ? ? SecureRandom rnd = new SecureRandom(); do { // ?? ? ? ?. randomNum = rnd.nextInt(endNum + 1); } while (randomNum < startNum); // ? ? // ? // ? // ?? // ?. } catch (Exception e) { //log.debug(e.getMessage()); log.trace(e.getMessage()); } return randomNum; }
From source file:edu.stanford.mobisocial.dungbeetle.model.DbObject.java
private static int colorFor(Long hash) { float[] baseHues = Feed.getBaseHues(); ByteBuffer bos = ByteBuffer.allocate(8); bos.putLong(hash);//www.j a v a 2 s . c o m byte[] hashBytes = new byte[8]; bos.position(0); bos.get(hashBytes); SecureRandom r = new SecureRandom(hashBytes); float hsv[] = new float[] { baseHues[r.nextInt(baseHues.length)], r.nextFloat(), r.nextFloat() }; hsv[0] = hsv[0] + 20 * r.nextFloat() - 10; hsv[1] = hsv[1] * 0.2f + 0.8f; hsv[2] = hsv[2] * 0.2f + 0.8f; return Color.HSVToColor(hsv); }
From source file:syncthing.android.service.SyncthingUtils.java
public static String randomString(int len) { PRNGFixes.apply();// w w w. j ava 2s . c om StringBuilder sb = new StringBuilder(); SecureRandom random = new SecureRandom(); for (int i = 0; i < len; i++) sb.append(CHARS.charAt(random.nextInt(CHARS.length()))); return sb.toString(); }
From source file:com.taikang.dic.ltci.service.impl.AgencyInterfaceServiceImpl.java
private static String getAgencyCode(String areaCode) { StringBuffer sb = new StringBuffer(areaCode); SecureRandom random = new SecureRandom(); for (int i = 0; i < 6; i++) { sb.append(org[random.nextInt(org.length)]); }// w w w . jav a 2 s. c o m return sb.toString(); }
From source file:org.apache.hadoop.security.authorize.TestProxyUsers.java
public static void loadTest(String ipString, int testRange) { Configuration conf = new Configuration(); conf.set(DefaultImpersonationProvider.getTestProvider().getProxySuperuserGroupConfKey(REAL_USER_NAME), StringUtils.join(",", Arrays.asList(GROUP_NAMES))); conf.set(DefaultImpersonationProvider.getTestProvider().getProxySuperuserIpConfKey(REAL_USER_NAME), ipString);//from ww w . jav a2 s .c o m ProxyUsers.refreshSuperUserGroupsConfiguration(conf); // First try proxying a group that's allowed UserGroupInformation realUserUgi = UserGroupInformation.createRemoteUser(REAL_USER_NAME); UserGroupInformation proxyUserUgi = UserGroupInformation.createProxyUserForTesting(PROXY_USER_NAME, realUserUgi, GROUP_NAMES); long startTime = System.nanoTime(); SecureRandom sr = new SecureRandom(); for (int i = 1; i < 1000000; i++) { try { ProxyUsers.authorize(proxyUserUgi, "1.2.3." + sr.nextInt(testRange)); } catch (AuthorizationException e) { } } long stopTime = System.nanoTime(); long elapsedTime = stopTime - startTime; System.out.println(elapsedTime / 1000000 + " ms"); }
From source file:com.taikang.dic.ltci.service.impl.AgencyStaffImpl.java
public static String getAgencyCode(String areaCode) { StringBuffer sb = new StringBuffer(areaCode); SecureRandom random = new SecureRandom(); for (int i = 0; i < 6; i++) { sb.append(org[random.nextInt(org.length)]); }/*www. j a v a 2 s . c om*/ return sb.toString(); }
From source file:com.taikang.dic.ltci.service.impl.AgencyStaffImpl.java
public static String getStaffCode(String areaCode) { StringBuffer sb = new StringBuffer(areaCode); SecureRandom random = new SecureRandom(); for (int i = 0; i < 6; i++) { sb.append(org[random.nextInt(org.length)]); }/*ww w. ja va 2 s. c om*/ return sb.toString(); }
From source file:nersc.greenseas.portlet.GreenseasPortlet.java
private static String createRandomString(int number) { SecureRandom sr = new SecureRandom(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < number; i++) { int randomInt = sr.nextInt(62); char nextChar = '_'; if (randomInt < 10) { nextChar = (char) (randomInt + 48); } else if (randomInt < 36) { nextChar = (char) (randomInt + 55); } else {/*w w w. j a v a 2 s. co m*/ nextChar = (char) (randomInt + 61); } sb.append(nextChar); } return sb.toString(); }