Put... In the database Short chain and long chain are mapped ; Table field id( Self increasing ), longurl, shorturl;
Use 0-9a-zA-Z Combine into five digits (62^5) Or six (62^6) Character to represent the field id ; Short chain and id Transform each other , Mapping and uniqueness can be achieved by using radix ;
from random import shuffle
secret = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# secret = 'YTiqlfv682MtKzV0RjLgpWJwmXAbcNkIoyPZda9ExO3S7FBnGU15eHshDuQCr4'
# String obfuscation
def random_string(s):
s = list(s)
shuffle(s)
print("".join(s))
return "".join(s)
# Decimal to 62 Base number
def encode62(id):
num_list = []
while (id > 0):
remainder = id % 62
num_list.insert(0, secret[remainder])
id = id // 62
return "".join(num_list).rjust(6, secret[0])
# 62 Convert base to decimal
def decode62(secret_str):
num_list = list(map(secret.index, secret_str))
num_sum = 0
for i, num in enumerate(num_list[::-1]):
num_sum += num * 62**i
return num_sum
print(encode62(138))
print(decode62(encode62(138)))