code
/**
     * 产生随机字符串 64位
     * */
    public static String generateSecretToken() {
        SecureRandom secRandom = new SecureRandom();
        byte[] result = new byte[32];
        secRandom.nextBytes(result);
        return bytesToHex(result);
    }
    
    /**
     * bytes转16进制字符串
     * */
    public static String bytesToHex(byte[] bytes) {
        BigInteger bi = new BigInteger(1, bytes);
        return bi.toString(16);
    }

 生成随机数

/**
     * 生成一个随机整数
     * */
    public static int getRandom(int maxInt) {
        try {
            SecureRandom secRandom = new SecureRandom();
            if(maxInt <= 0) {
                // 生成一个随机整数
                int randomInt = secRandom.nextInt();
                return randomInt;
            }
            // 生成一个指定范围的随机整数
            int lowerBound = 1;
            int upperBound = maxInt;
            int randomIntInRange = lowerBound + secRandom.nextInt((upperBound - lowerBound) + 1);
            return randomIntInRange;
        } catch (Exception ex) {
            return 0;
        }
    }

 

posted on 2025-03-04 16:43  邢帅杰  阅读(9)  评论(0编辑  收藏  举报