Based on the code you gave, I think you want to use the MD5 algorithm for the 16-base string and 0x3fffffff (30-bit 1) AND operation.Due to the algorithm, multiple results will end up and there may be duplicate results. Finally, the effect is achieved by saving to the database for mapping.
Code:
static void Main(string[] args)
{
Random rd = new Random();
string url = "https://localhost:44316/regpage.aspx";
string str = "https://localhost:44316/regpage.aspx?id=321321321";
string[] resAry = str.Split(new string[] { url }, StringSplitOptions.None);
Console.WriteLine(resAry[1]);
for (int i = 0; i< 100; i++)
{
int index = rd.Next(0, 4);
var stortUrls = ShortUrl(resAry[1]);
Console.WriteLine( url+'/'+stortUrls[index]);
}
Console.Read();
}
public static string[] ShortUrl(string url)
{
string key = "key";
string[] chars = new string[]
{
"a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x",
"y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D",
"E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"
};
string hex = FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");
string[] resUrl = new string[4];
for (int i = 0; i < 4; i++)
{
int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
string outChars = string.Empty;
for (int j = 0; j < 6; j++)
{
int index = 0x0000003D & hexint;
outChars += chars[index];
hexint = hexint >> 5;
}
resUrl[i] = outChars;
}
return resUrl;
}
Output:
Best Regards
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.