账号-生成密码Math.random()

package com.yanzhanbo.demo.center;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class test {
    public static void main(String[] args) {
        int i = 8 ;
        List<String> list = new ArrayList<>();
//        String pd=getRandomPassword(i);
//        String passWordOne = getPassWordOne(i);
        for (int j = 0; j <3 ; j++) {
            String passWordOne = getPsw(i);
            if(!isNumeric(passWordOne) &&!list.contains(passWordOne)){
                log.info("======================,   : "+ passWordOne);
                list.add(passWordOne);
            }
        }
//        boolean b = mobile.startsWith("800");
        System.out.println("======================,    " + JSONObject.toJSONString(list));
    }

    public static String getRandomPassword(int len) {
        String result = null;
        while(len>=6){
            result = makeRandomPassword(len);
            if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") && result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
                return result;
            }
            result = makeRandomPassword(len);
        }
        return "长度不得少于6位!";
    }
    public static String makeRandomPassword(int len){
        char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&?".toCharArray();
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int x = 0; x < len; ++x) {
            sb.append(charr[r.nextInt(charr.length)]);
        }
        return sb.toString();
    }

    /**
     * 生成随机密码生成方式一
     * 密码字典 -> 随机获取字符
     *
     * @param len 生成密码长度(最短为4位)
     * @return 随机密码
     */
    public static String getPassWordOne(int len) {
        // 如果len 小于等于3
        if (len <= 3) {
            throw new RuntimeException("密码的位数不能小于3位!");
        }
        //生成的随机数
        int i;
        //生成的密码的长度
        int count = 0;
        // 密码字典
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', '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', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        // 大写字母密码字典
        List<Character> bigStrList = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        Set<Character> bigStrSet = new HashSet<>(bigStrList);
        // 小写字母的密码字典
        List<Character> upperStrList = Arrays.asList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
        Set<Character> upperStrSet = new HashSet<>(upperStrList);
        // 数字的密码字典
        List<Character> numStrList = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        Set<Character> numStrSet = new HashSet<>(numStrList);

        StringBuilder builder = new StringBuilder();
        Random r = new Random();
        boolean isContainBigChar = false;
        boolean isContainUpperChar = false;
        boolean isContainNumChar = false;
        while (count < len - 3) {
            //生成 0 ~ 密码字典-1之间的随机数
            i = r.nextInt(str.length);
            builder.append(str[i]);
            count++;
            if (!isContainBigChar && bigStrSet.contains(str[i])) {
                isContainBigChar = true;
            }
            if (!isContainUpperChar && upperStrSet.contains(str[i])) {
                isContainUpperChar = true;
            }
            if (!isContainNumChar && numStrSet.contains(str[i])) {
                isContainNumChar = true;
            }
        }
        // 如果不存在的,则加,确保一定会存在数字,大写字母,小写字母
        if (!isContainBigChar) {
            builder.append(bigStrList.get(r.nextInt(bigStrList.size())));
        }
        if (!isContainUpperChar) {
            builder.append(upperStrList.get(r.nextInt(upperStrList.size())));
        }
        if (!isContainNumChar) {
            builder.append(numStrList.get(r.nextInt(numStrList.size())));
        }
        while (builder.length() < len) {
            builder.append(str[r.nextInt(str.length)]);
        }
        return builder.toString();
    }
    public static String getPsw(int len) {
        // 1、定义基本字符串baseStr和出参password
        String password = null;
//        String baseStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}|<>?";
//        String baseStr = "0123456789abcdefg0123456789hijkmnopq0123456789rstuvwxyz0123456789ABCDEFG0123456789HIJKLMNOPQ0123456789RSTUVWXYZ0123456789!@#$&?";
        String baseStr = "0123456789abcdefg0123456789hijkmnopq0123456789rstuvwxyz0123456789ABCDEFG0123456789HIJKLMNOPQ0123456789RSTUVWXYZ0123456789";
        boolean flag = false;
        // 2、使用循环来判断是否是正确的密码
        while (!flag) {
            // 密码重置
            password = "";
            // 个数计数
            int a = 0,b = 0,c = 0,d = 0;
            for (int i = 0; i < len; i++) {
                int rand = (int) (Math.random() * baseStr.length());
                password+=baseStr.charAt(rand);
                if (0<=rand && rand<=9) {
                    a++;
                }
                if (10<=rand && rand<=35) {
                    b++;
                }
                if (36<=rand && rand<=61) {
                    c++;
                }
                if (62<=rand && rand<baseStr.length()) {
                    d++;
                }
            }
            // 3、判断是否是正确的密码(4类中仅一类为0,其他不为0)
            flag = (a*b*c!=0&&d==0)||(a*b*d!=0&&c==0)||(a*c*d!=0&&b==0)||(b*c*d!=0&&a==0);
        }
        return password;
    }

    public final static boolean isNumeric(String str){
        if (str != null && !"".equals(str.trim())){
            return str.matches("^[0-9]*$");
        }
        else{
            return false;
        }
    }


}

 ["8e5i1877","HQa85di4","04TDr2N7"]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值