New file |
| | |
| | | package com.yeshi.fanli.util;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.math.BigInteger;
|
| | | import java.util.regex.Matcher;
|
| | | import java.util.regex.Pattern;
|
| | |
|
| | | /**
|
| | | * 口令工具
|
| | | * |
| | | * @author Administrator
|
| | | *
|
| | | */
|
| | | public class TokenUtil {
|
| | |
|
| | | private static String convert10To62(Long num) {
|
| | | String numbers = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
| | | BigDecimal numBig = new BigDecimal(num);
|
| | |
|
| | | int ge = numBig.divideAndRemainder(new BigDecimal(numbers.length()))[1].intValue();
|
| | | int n = 1;
|
| | | String result = "";
|
| | | result += numbers.charAt(ge);
|
| | |
|
| | | while (numBig.divideAndRemainder(new BigDecimal(numbers.length()).pow(n))[0].compareTo(new BigDecimal(0)) > 0) {
|
| | | int w = numBig.divideAndRemainder(new BigDecimal(numbers.length()).pow(n))[0]
|
| | | .divideAndRemainder(new BigDecimal(numbers.length()))[1].intValue();
|
| | | result = numbers.charAt(w) + result;
|
| | | n++;
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 生成口令
|
| | | * |
| | | * @param id
|
| | | * @return
|
| | | */
|
| | | public static String createToken(Long id) {// 生成口令
|
| | | long fid = 10000000000000L * ((int) (Math.random() * 1000) + 1) + id;
|
| | | return "&" + convert10To62(fid) + "&";
|
| | | }
|
| | |
|
| | | /**
|
| | | * 提取口令
|
| | | * |
| | | * @param content
|
| | | * @return
|
| | | */
|
| | | public static String parseToken(String content) {
|
| | | String regex = "&[A-Za-z0-9]{6,10}&";
|
| | | Pattern pattern = Pattern.compile(regex);
|
| | | String source = content;
|
| | | Matcher matcher = pattern.matcher(source);
|
| | | while (matcher.find()) {
|
| | | String result = matcher.group(0);
|
| | | return result;
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | }
|