Generate a short token string
Sometimes, we need to generate some tokens as the identifiers, such as the authenticated identifiers and resource extraction codes. A common algorithm is to generate a guid as the token. Because of its randomness and uniqueness, token is a very reliable choice.
GUID is an array of bits. To facilitate portability, it is often expressed as a string. It is generally expressed as follows: {79faf822-7194-4fe3-8c4f-1d99be71bc9c }. There is a drawback: It's too long. How can we shorten it?
Remove unnecessary modifiers,
First, you can remove meaningless parentheses and minus signs to Reduce the length:
VaR token = guid. tostring ("N ");
In this way, the string will be: 79faf82271944fe38c4f1d99be71bc9c. It looks pretty long.
Base64 encoding
In the preceding representation method, the 16 mechanism is used for representation. If base64 encoding is used, the string can be further compressed.
VaR token = convert. tobase64string (guid. tobytearray (). trimend ('= ');
In this way, the string is: ivj6ezrx40 + mtx2zvng8na. It looks a little better.
Another Token Generation Method
After base64 encoding is used, the token string still has more than 20 bits, sometimes it is too long. Since the guid itself has bits, it is difficult to further improve it on the premise that it requires good readability. How can we generate shorter strings? Another way is to use a random number with a certain length, such as 64bit, instead of guid, with base64 encoding:
VaR RND = new random ();
VaR tokendata = newbyte [8];
RND. nextbytes (tokendata );
VaR token = convert. tobase64string (tokendata). trimend ('= ');
Because 64bit is used here, the resulting string is in the form of onh0h95n7nw, And the length must be shorter than half. This makes it much easier to carry. However, this method does not guarantee uniqueness. However, it can be used as an identity authentication method (for example, an online storage extraction code ).
Further steps
In the preceding algorithm, the length and randomness are fixed, but they are not unique. For scenarios requiring uniqueness, You need to rewrite the Token Generation Algorithm. Here is a simple example:
Classtoken
{
Staticrandom RND = newrandom ();
Staticint seed = 0;
Publicstaticstring create ()
{
VaR rnddata = newbyte [4];
RND. nextbytes (rnddata );
VaR seedvalue = interlocked. Add (ref seed, 1 );
VaR seeddata = bitconverter. getbytes (seedvalue );
VaR tokendata = rnddata. Concat (seeddata). orderby (_ => RND. Next ());
Returnconvert. tobase64string (tokendata. toarray (). trimend ('= ');
}
}
Here, my algorithm is very simple:
- A token consists of two parts: a 32-bit random number + A 32-bit sequence.
- The sequence guarantees uniqueness, and the random number ensures randomness.
- And then shuffle again.
Of course, my algorithm also has some limitations, such:
- The token must be unique within 4G.
- The token must be unique in the context.
- The token is not completely random.
To solve these problems, the token generated at any time, anywhere, and under any circumstances is unique and random. However, you must know that any function has a certain price. The cost of these conditions is that the length of the token is increased-GUID is the algorithm that satisfies this series of conditions. There is no silver bullet in the world of software. We only need to find a solution to the problem within a certain range.
System. guid = system. guid. newguid (); // guid type string strguid = system. guid. newguid (). tostring (); // directly return the string type