code
/** * 產(chǎn)生隨機(jī)字符串 64位 * */ public static String generateSecretToken() { SecureRandom secRandom = new SecureRandom(); byte[] result = new byte[32]; secRandom.nextBytes(result); return bytesToHex(result); } /** * bytes轉(zhuǎn)16進(jìn)制字符串 * */ public static String bytesToHex(byte[] bytes) { BigInteger bi = new BigInteger(1, bytes); return bi.toString(16); }
生成隨機(jī)數(shù)
/** * 生成一個隨機(jī)整數(shù) * */ public static int getRandom(int maxInt) { try { SecureRandom secRandom = new SecureRandom(); if(maxInt <= 0) { // 生成一個隨機(jī)整數(shù) int randomInt = secRandom.nextInt(); return randomInt; } // 生成一個指定范圍的隨機(jī)整數(shù) int lowerBound = 1; int upperBound = maxInt; int randomIntInRange = lowerBound + secRandom.nextInt((upperBound - lowerBound) + 1); return randomIntInRange; } catch (Exception ex) { return 0; } }
浙公網(wǎng)安備 33010602011771號