密码工具类

package com.xxx.xxx.common.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Random;

/**
 * 密码工具类
 */
public class PwdUtil {
	
	    public static String encode(String password) {
	        PasswordEncoder encoder = encoder();
	        return encoder.encode(password);
	    }
	
	    public static PasswordEncoder encoder() {
	        return new BCryptPasswordEncoder();
	    }
	
	    public static void main(String[] args) {
	        String pwd1 = encode("123456");
	        System.out.println(pwd1);
	        String pwd2 = encode("123456");
	        System.out.println(pwd2);
	        System.out.println(encoder().matches("123456", pwd1));
        	System.out.println(encoder().matches("123456", pwd2));
        	
        	//随机生成8位密码
        	 System.out.println(getPassword());
    }


	//此处转载:https://blog.csdn.net/graceup/article/details/40398655 随机生成8位密码 工具类
	
    public static String getPassword() {
        Random rd = new Random(); // 创建随机对象
        String n = ""; // 保存随机数
        int rdGet; // 取得随机数
        do {
            if (rd.nextInt() % 2 == 1) {
                rdGet = Math.abs(rd.nextInt()) % 10 + 48; // 产生48到57的随机数(0-9的键位值)
            } else {
                rdGet = Math.abs(rd.nextInt()) % 26 + 97; // 产生97到122的随机数(a-z的键位值)
            }
            char num1 = (char) rdGet; // int转换char
            String dd = Character.toString(num1);
            n += dd;
        } while (n.length() < 8);// 设定长度,此处假定长度小于8
        return n;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值