吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 935|回复: 0
收起左侧

[Java 转载] Java工具类-验证码工具1

[复制链接]
670432337 发表于 2021-7-21 10:07
import java.util.Arrays;

/**
* 工具类,生成随机验证码字符串
*
* @version 1.0
* @author
*
*/

public class SecurityCode {
    /**
     * 验证码难度级别,Simple只包含数字,Medium包含数字和小写英文,Hard包含数字和大小写英文
     */
    public enum SecurityCodeLevel {
        Simple, Medium, Hard
    };

    /**
     * 产生默认验证码,4位中等难度
     *
     * @Return String 验证码
     */
    public static String getSecurityCode() {
        return getSecurityCode(4, SecurityCodeLevel.Medium, false);
    }

    /**
     * 产生长度和难度任意的验证码
     *
     * @Param length
     *            长度
     * @param level
     *            难度级别
     * @param isCanRepeat
     *            是否能够出现重复的字符,如果为true,则可能出现 5578这样包含两个5,如果为false,则不可能出现这种情况
     * @return String 验证码
     */
    public static String getSecurityCode(int length, SecurityCodeLevel level,
            boolean isCanRepeat) {

        // 随机抽取len个字符
        int len = length;

        // 字符集合(除去易混淆的数字0、数字1、字母l、字母o、字母O)
        char[] codes = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
                'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p',
                'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B',
                'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

        // 根据不同的难度截取字符数组
        if (level == SecurityCodeLevel.Simple) {
            codes = Arrays.copyOfRange(codes, 0, 9);
        } else if (level == SecurityCodeLevel.Medium) {
            codes = Arrays.copyOfRange(codes, 0, 33);
        }

        // 字符集合长度
        int n = codes.length;

        // 抛出运行时异常
        if (len > n && isCanRepeat == false) {
            throw new RuntimeException(String.format(
                    "调用SecurityCode.getSecurityCode(%1$s,%2$s,%3$s)出现异常,"
                            + "当isCanRepeat为%3$s时,传入参数%1$s不能大于%4$s", len,
                    level, isCanRepeat, n));
        }

        // 存放抽取出来的字符
        char[] result = new char[len];

        // 判断能否出现重复的字符
        if (isCanRepeat) {
            for (int i = 0; i < result.length; i++) {
                // 索引 0 and n-1
                int r = (int) (Math.random() * n);

                // 将result中的第i个元素设置为codes[r]存放的数值
                result = codes[r];
            }
        } else {
            for (int i = 0; i < result.length; i++) {
                // 索引 0 and n-1
                int r = (int) (Math.random() * n);

                // 将result中的第i个元素设置为codes[r]存放的数值
                result = codes[r];
                // 必须确保不会再次抽取到那个字符,因为所有抽取的字符必须不相同。
                // 因此,这里用数组中的最后一个字符改写codes[r],并将n减1
                codes[r] = codes[n - 1];
                n--;
            }
        }

        return String.valueOf(result);
    }
    public static void main(String[] args) {
        SecurityCode code = new SecurityCode();
        System.out.println(getSecurityCode(6,SecurityCodeLevel.Hard,false));
    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-4 03:16

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表