生成邀请码算法
邀请码我们是采用10位数字,排除o和l容易混淆的字母24个,共34个可选字符,八位的长度就是34的八次幂,能容纳17万亿个不重复爱豆码。唯一缺陷就是我们的种子数字是采用的数据库自增id,容易被反向猜出生成码的算法(难度也很大),如果担心被破解可以采用随机数的方式作为种子id,但是需要提前对种子id判断之前的种子id是否有重复,多做一次查询。更加难破解的方式还可以对种子id采用混淆加密等方式增加破解难度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
/** * @ClassName AiDouGenerateUtils * @Description 邀请码生成的工具类 */ public class AiDouGenerateUtils { /** * 自定义进制(选择你想要的进制数,不能重复且最好不要0、1这些容易混淆的字符) */ private static final char[] r = new char[]{'w', 'y', 'e', '3', 'h', 'u', '7', 'z', 'k', '9', 'c', 'f', 'g', '5', 'x', '8', 'm', 'j', '2', 'd', 'r', '4', 'v', 'q', 't', 'n', '6', 'b', 'p', 's'}; /** * 定义一个字符用来补全邀请码长度(该字符前面是计算出来的邀请码,后面是用来补全用的) */ private static final char b = 'a'; /** * 进制长度 */ private static final int binLen = r.length; /** * 邀请码长度 */ private static final int s = 10; private static final String PREFIX = "ad"; /** * 根据ID生成随机码 * * @param id ID * @return 随机码 */ public static String toSerialCode(long id, String prefix) { String result = toSerialCode(id); if (StringUtils.isBlank(prefix)) { result = PREFIX + result; } else { result = prefix + result; } return result; } /** * 根据ID生成随机码 * * @param id ID * @return 随机码 */ public static String toSerialCode(long id) { char[] buf = new char[32]; int charPos = 32; while ((id / binLen) > 0) { int ind = (int) (id % binLen); buf[--charPos] = r[ind]; id /= binLen; } buf[--charPos] = r[(int) (id % binLen)]; String str = new String(buf, charPos, (32 - charPos)); // 不够长度的自动随机补全 if (str.length() < s) { 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)]); } str += sb.toString(); } return str; } /** * 根据随机码生成ID * * @param code 随机码 * @return ID */ public static long codeToId(String code) { char chs[] = code.toCharArray(); long res = 0L; for (int i = 0; i < chs.length; i++) { int ind = 0; for (int j = 0; j < binLen; j++) { if (chs[i] == r[j]) { ind = j; break; } } if (chs[i] == b) { break; } if (i > 0) { res = res * binLen + ind; } else { res = ind; } } return res; } public static String toUpperCaseSerialCode(long id) { String code = toSerialCode(id); return StringUtils.upperCase(code); } } |
©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!
发表评论