Generate a token string

Source: Internet
Author: User
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:

  1. A token consists of two parts: a 32-bit random number + A 32-bit sequence.
  2. The sequence guarantees uniqueness, and the random number ensures randomness.
  3. And then shuffle again.

Of course, my algorithm also has some limitations, such:

  1. The token must be unique within 4G.
  2. The token must be unique in the context.
  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.