| | |
| | | package cc.mrbird.febs.common.utils;
|
| | |
|
| | | import java.util.ArrayList;
|
| | | import java.util.Random;
|
| | |
|
| | | public class ShareCodeUtil {
|
| | |
| | | /**
|
| | | * 自定义进制(0,1没有加入,容易与o,l混淆)
|
| | | */
|
| | | // private static final char[] r=new char[]{'q', 'w', 'e', '8', 'a', 's', '2', 'd', 'z', 'x', '9', 'c', '7', 'p', '5', 'i', 'k', '3', 'm', 'j', 'u', 'f', 'r', '4', 'v', 'y', 'l', 't', 'n', '6', 'b', 'g', 'h'};
|
| | | private static final char[] r = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
| | | private static final char[] r=new char[]{'q', 'w', 'e', '8', 'a', 's', '2', 'd', 'z', 'x', '9', 'c', '7', 'p', '5', 'i', 'k', '3', 'm', 'j', 'u', 'f', 'r', '4', 'v', 'y', 'l', 't', 'n', '6', 'b', 'g', 'h'};
|
| | | // private static final char[] rAccount = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
| | | private static final char[] rInvitedId = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
| | | private static final char[] rAccount = new char[]{'q', 'w', 'e', '8', 'a', 's', '2', 'd', 'z', 'x', '9', 'c', '7', 'p', '5', 'i', 'k', '3', 'm', 'j', 'u', 'f', 'r', '4', 'v', 'y', 'l', 't', 'n', '6', 'b', 'g', 'h'};
|
| | |
|
| | | /**
|
| | | * (不能与自定义进制有重复)
|
| | |
| | | * 进制长度
|
| | | */
|
| | | private static final int binLen = r.length;
|
| | | private static final int binLenAccount = rAccount.length;
|
| | | private static final int binLenInvitedId = rInvitedId.length;
|
| | |
|
| | | /**
|
| | | * 序列最小长度
|
| | | */
|
| | | private static final int s = 8;
|
| | | private static final int s = 32;
|
| | | private static final int sAccount = 6;
|
| | | private static final int sInvitedId = 8;
|
| | |
|
| | | /**
|
| | | * 根据ID生成六位随机码
|
| | | * 根据ID生成32位随机密钥
|
| | | *
|
| | | * @param id ID
|
| | | * @return 随机码
|
| | |
| | | return str;
|
| | | }
|
| | |
|
| | | public static void main(String[] args) {
|
| | | ArrayList<String> objects = new ArrayList<>();
|
| | | for(int i =0;i<=1000;i++){
|
| | | String s = ShareCodeUtil.toSerialNumberCode(i);
|
| | | if(objects.contains(s)){
|
| | | System.out.println(s);
|
| | | }
|
| | | objects.add(s);
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | /**
|
| | | * 根据ID生成六位随机码
|
| | | *
|
| | |
| | | char[] buf = new char[32];
|
| | | int charPos = 32;
|
| | |
|
| | | while ((id / binLen) > 0) {
|
| | | int ind = (int) (id % binLen);
|
| | | buf[--charPos] = r[ind];
|
| | | id /= binLen;
|
| | | while ((id / binLenAccount) > 0) {
|
| | | int ind = (int) (id % binLenAccount);
|
| | | buf[--charPos] = rAccount[ind];
|
| | | id /= binLenAccount;
|
| | | }
|
| | | buf[--charPos] = r[(int) (id % binLen)];
|
| | | buf[--charPos] = rAccount[(int) (id % binLenAccount)];
|
| | | String str = new String(buf, charPos, (32 - charPos));
|
| | | // 不够长度的自动随机补全
|
| | | if (str.length() < s) {
|
| | | if (str.length() < sAccount) {
|
| | | StringBuilder sb = new StringBuilder();
|
| | | sb.append(b);
|
| | | Random rnd = new Random();
|
| | | for (int i = 1; i < s - str.length(); i++) {
|
| | | sb.append(r[rnd.nextInt(binLen)]);
|
| | | for (int i = 1; i < sAccount - str.length(); i++) {
|
| | | sb.append(rAccount[rnd.nextInt(binLenAccount)]);
|
| | | }
|
| | | str += sb.toString();
|
| | | }
|
| | | return str;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 根据ID生成六位随机数字码
|
| | | *
|
| | | * @param id ID
|
| | | * @return 随机码
|
| | | */
|
| | | public static String toSerialNumberCodeTwo(long id) {
|
| | | char[] buf = new char[32];
|
| | | int charPos = 32;
|
| | |
|
| | | while ((id / binLenInvitedId) > 0) {
|
| | | int ind = (int) (id % binLenInvitedId);
|
| | | buf[--charPos] = rInvitedId[ind];
|
| | | id /= binLenInvitedId;
|
| | | }
|
| | | buf[--charPos] = rInvitedId[(int) (id % binLenInvitedId)];
|
| | | String str = new String(buf, charPos, (32 - charPos));
|
| | | // 不够长度的自动随机补全
|
| | | if (str.length() < sInvitedId) {
|
| | | StringBuilder sb = new StringBuilder();
|
| | | sb.append(b);
|
| | | Random rnd = new Random();
|
| | | for (int i = 1; i < sInvitedId - str.length(); i++) {
|
| | | sb.append(rInvitedId[rnd.nextInt(binLenInvitedId)]);
|
| | | }
|
| | | str += sb.toString();
|
| | | }
|