根据指定规则生成唯一纯数字Id

根据指定规则生成唯一纯数字Id

JAVA版


import java.security.MessageDigest;

/**
 * @ClassName: Md5Utils
 * @Author yang
 * @Date: 2021/4/25 9:52 上午
 * @Version 1.0
 */
public class Md5Utils {

    public static String byteArrayToString2(byte[] b) {
        StringBuilder sb = new StringBuilder(40);
        for (byte x : b) {
            sb.append(Integer.toHexString(x & 0xff));
        }
        return sb.toString();
    }

    public static String byteArrayToString(byte[] b) {
        StringBuffer sb = new StringBuffer(40);
        for (byte x : b) {
            sb.append(byteToNumString(x));
        }
        return sb.toString();
    }


    public static String encrypt16(byte[] b) {
        return byteArrayToString(b).substring(8, 24);
    }

    private static String byteToNumString(byte b) {
        int _b = b;
        if (_b < 0) {
            _b = 256 + _b;
        }
        return String.valueOf(_b);
    }

    public static Long MD5Encode(String origin) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = encrypt16(md.digest(resultString.getBytes()));
        } catch (Exception ex) {

        }
        return Long.parseLong(resultString);
    }

    public static String MD5Encode2(String origin) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byteArrayToString2(md.digest(resultString.getBytes()));
        } catch (Exception ex) {

        }
        return resultString;
    }

    public static void main(String[] args) {
        System.out.println(Md5Utils.MD5Encode("张三-李四-1"));
        System.out.println(Md5Utils.MD5Encode("张三-李四-2"));
    }

}

Mysql版

select 
cast(conv( substr( md5( CONCAT( '测试', '完成', '0000' )), 15, 16 ), 16, 10 ) AS DECIMAL ( 65 )) AS `20`,
cast(conv( substr( md5( CONCAT( '测试', '完成', '0000' )), 16, 15 ), 16, 10 ) AS DECIMAL ( 65 )) AS `17`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAVA界小学僧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值