Package com.halis.p2p.util;
Import Java.util.Random; /** * Invitation Code Tool Class * @author CX * */@SuppressWarnings ("All") public class Invitationcodeutil {private Invitationcodeuti L () {}/** * serialized encoding Dictionary * from left to right without scrambling code, all for normal serialization encoding * * private static final char[] serialcodedict = new char[]{' Q ', ' w ', '% ', ' 8 ', ' A ', '-', ' 2 ', ' d ', ' # ', ' x ', ' 9 ', ' O ', ' C ', ' 7 ', ' _ ', ' 5 ', ' I ', ' K ', ' 3 ', ' m ', ' j ', ' U ', ' f ', ' r ', ' 4 ',
' V ', ' y ', ' l ', ' t ', ' n ', ' 6 ', ' B ', ' g ', ' H '}; /** * Remark: The remaining characters after these characters are all random characters */private static final char[] scramblerdict = new char[]{'
S ', ' Z ', ' $ ', ' e ', ' P ', ' 0 ', ' 1 '}; /** * All dictionaries/private static final char[] alldict = new char[]{' q ', ' w ', '% ', ' 8 ', ' A ', '-', ' 2 ', ' d ', ' # ', ' X ', ' 9 ', ' O ', ' C ', ' 7 ', ' _ ', ' 5 ', ' I ', ' K ', ' 3 ', ' m ', ' j ', ' U ', ' f ', ' r ', ' 4 ', ' V ', ' y ', ' l ', ' t ', ' n ', ' 6 ', ' B ', ' g ', ' H '
, ' s ', ' Z ', ' $ ', ' e ', ' P ', ' 0 ', ' 1 '}; /** * Remark:SERIALCODEDICT.length 1/private Static final int binlen = serialcodedict.length;
/** * Invitation Code Minimum Length * * private static final int serialcodelength = 10; /** * Generate SERIALCODELENGTH-32 bytes of random serialized string based on ID * @param ID ID * @return random Code/public static string
Toserialcode (Long ID) {//stored true serialized ID after the encoding of the longest 32 characters final char[] _codes = new CHAR[32];
subscript int index = _codes.length; ID is greater than the number of digits into the loop while (Id > Binlen) {//ID and the remainder of the digits 10/33 = ten int residue = (int
) (id% binlen);
Save serialized character _codes[--index]=serialcodedict[residue];
ID/= Binlen;
} _codes[--index] = serialcodedict[(int) (ID% binlen)];
String serialcode = new String (_codes, index, (32-index)); Insufficient length automatic random complement if (Serialcode.length () < serialcodelength) {Random Random =new Random ();
Scrambling code Serialcode + = Scramblerdict[random.nextint (scramblerdict.length)];
for (int i=1; Serialcode.length () < serialcodelength; i++) {//Scrambling code if (Random.nextint (3) ==1) {
Serialcode = Scramblerdict[random.nextint (scramblerdict.length)]+serialcode;
}else{Serialcode + + alldict[random.nextint (alldict.length)];
}} return Serialcode; /** * Decoding the serialized invitation code * @param code * @return/public static long Serialcodetoid (Strin
G Serialcode) {final char[] _codes = Serialcode.tochararray ();
Exclude the left of the scrambling code to find the normal code start of the subscript long id = 0L;
Whether to end a Boolean scramblerisend = False from left to right scrambling code; for (int i=0 i < _codes.length i++) {//Excluding front spoiler section if (!scramblerisend) {//If it is a scrambling code Skip this cycle directly if (ISSCRAMBLER (_codes[i])) {continue;
}else{scramblerisend = true;
}///If the spoiler is directly stopping the loop if (Isscrambler (_codes[i)) {break;
//Find the corresponding encoded subscript int index = 0; for (int j=0 J < Binlen J + +) {if (_codes[i] = = Serialcodedict[j]) {IND
ex = j;
Break
} if (i > 0) {id = id * binlen + index;
else {id = index;
} return ID; /** * is the scrambling code * @param scramblercode * @return/private static Boolean Isscrambler (Char SC
Ramblercode) {for (char _code:scramblerdict) {if (Scramblercode = = _code) {return true;
return false; }
}