2020 Java16位code生成

package com.example.demo.util;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/07/23/14:49
 * @Description:
 */
public class MD5Utils {

    /**
     * byte[]字节数组 转换成 十六进制字符串
     * @param arr 要转换的byte[]字节数组
     * @return  String 返回十六进制字符串
     */
    private static String hex(byte[] arr) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; ++i) {
            sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }


    /**
     * MD5加密,并把结果由字节数组转换成十六进制字符串
     * @param str 要加密的内容
     * @return String 返回加密后的十六进制字符串
     */
    private static String md5Hex(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(str.getBytes());
            return hex(digest);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
            return "";
        }
    }



    /**
     * 生成含有随机盐的密码
     *
     * @param password 要加密的密码
     * @param salt  盐
     * @return String 含有随机盐的密码
     */
    public static String getSaltMD5(String password,String salt) {
        // 生成一个16位的随机数
        Random random = new Random();
        // 密码 + 盐
        return md5Hex(password + salt);
    }



    /**
     * 验证加盐后是否和原密码一致
     * @param password 原密码
     * @param password 加密之后的密码
     *@return boolean true表示和原密码一致   false表示和原密码不一致
     */
    public static boolean getSaltverifyMD5(String password, String md5str ,String salt) {
        return md5Hex(password + salt).equals(String.valueOf(md5str));
    }



    public static void main(String[] args) {
        /*System.out.println(Math.random());
        System.out.println(Math.random() * 36);
        //盐
        String salt = "123";
        // 原密码
        String plaintext = randomCode();
        System.out.println("加密数据:"+plaintext);

        // 获取加盐后的MD5值
        String ciphertext = MD5Utils.getSaltMD5(plaintext,salt);
        System.out.println("加盐后MD5:" + ciphertext);
        System.out.println("是否是同一字符串:" + MD5Utils.getSaltverifyMD5(plaintext, ciphertext,salt));
    */
        for(int i=0;i<100; i++){
            System.out.println("生成16位Code : " + getSn("1234"));
        }




    }


    /**
     * 八位code
     */
    public static String randomCode() {
        String randomcode = "";
        String model = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] m = model.toCharArray();
        for (int j = 0; j < 8; j++) {
            char c = m[(int) (Math.random() * 36)];
            if (randomcode.contains(String.valueOf(c))) {
                j--;
                continue;
            }
            randomcode = randomcode + c;
        }
        return randomcode;
    }


    /**
     *
     * 1、长度:16位(8位随机数+8位校验数据)
     * 2、随机数生成范围:A-Z0-9
     * 3、后8位是校验数据,校验数据生成规则:8位随机数+MD5(上一步生成的8位大写随机数+大写盐值).sub(0,8)
     * 4、盐值配置在系统系统配置文件里,key为:sn.secret
     * @return
     */

    public static String getSn(String salt){
        String code = MD5Utils.randomCode();
        System.out.println("[salt :" + salt +"]");
        String saltMd5 = MD5Utils.getSaltMD5(code,salt).substring(0,8).toUpperCase();
        String tempCode = code + saltMd5 ;
        Map<String, Object> filterMap = new HashMap<>();
        filterMap.put("code",tempCode);
        return tempCode;
    }
}


猜你喜欢

转载自blog.csdn.net/jintaocccq/article/details/107716921