Sometimes, we need to generate some tokens as identification: such as authentication identifiers, resource extraction codes, etc. A more common algorithm is to generate a GUID as a token. Due to the randomness and uniqueness of the GUID, being a token is a very reliable choice.
GUID is a 128 bit array. For easy portability, it is often expressed as a string. It is generally expressed as the following form: {79FAF822-7194-4FE3-8C4F-1D99BE71BC9C}. There is a disadvantage to this: it is too long, so how can we make it shorter?
Remove unnecessary modifiers
First, you can reduce the length by removing meaningless brackets and minus signs:
var token = ("N");
In this way the string becomes: 79faf82271944fe38c4f1d99be71bc9c. It feels quite long.
Use Base64 encoding to represent
In the previous representation method, the 16 mechanism is used to represent it. If Base64 encoding is used, the string can be further compressed.
var token = Convert.ToBase64String(()).TrimEnd('=');
In this way, the string becomes: Ivj6eZRx40+MTx2ZvnG8nA. It looks a little better.
Change token generation method
After using Base64 encoding, the Token string still has more than 20 bits, and sometimes it still seems that it is too long. Since the GUID itself has 128 bits, it is difficult to further improve it while requiring good readability. So how do we generate shorter strings? Another way is to use a random number of a certain length, such as 64bit, and then use Base64 encoding to represent:
var rnd = new Random(); var tokenData = new byte[8]; (tokenData); var token = Convert.ToBase64String(tokenData).TrimEnd('=');
Since only 64bit is used here, the string obtained at this time is the form of Onh0h95n7nw, and the length is half shorter. This makes it much easier to carry. But this method has no unique guarantee. However, it is still possible to use it as identity authentication (such as the extraction code of the network disk).
Going further
In the previous algorithm, length and randomness exist, but there is no uniqueness. For scenarios that require uniqueness, you need to rewrite the token generation algorithm. I will give a simple example here:
class Token { static Random rnd = new Random(); static int seed = 0; public static string Create() { var rndData = new byte[4]; (rndData); var seedValue = (ref seed, 1); var seedData = (seedValue); var tokenData = (seedData).OrderBy(_ => ()); return Convert.ToBase64String(()).TrimEnd('='); } }
My algorithm here is very simple:
- Token consists of two parts, a 32-bit random number + a 32-bit sequence
- The uniqueness is guaranteed by the sequence, and the randomness is guaranteed by the random number.
- After the combination, perform shuffle again.
Of course, my algorithm also has certain limitations, such as:
- Token can only be guaranteed to be unique within 4G range
- Tokens can only be guaranteed to be unique in context
- Tokens are not completely random
To solve these problems, it is possible to achieve uniqueness and randomness in any time, anywhere, and under any circumstances. However, it is important to know that any function comes with a certain price. The cost of these conditions is the increase in the length of the Token - GUID is an algorithm that meets this series of conditions. There is no silver bullet in the world of software, we just need to find a way to solve the problem within a certain range.
This is all about this article about C# generating Token strings. I hope it will be helpful to everyone's learning and I hope everyone will support me more.