python id轉隨機字串庫 Hashids

2021-10-05 08:07:15 字數 1083 閱讀 1424

當你想把你url中的id變成隨機字串,避免被爬蟲時你會用到這個工具,hashids可以將任意字元轉換成隨機字串,並能夠反編碼回來,talk is cheap,show code。

pip install hashids
mac使用者請使用如下命令安裝:

pip install hashids --user
import hashids

hasher = hashids.hashids(salt='你的金鑰', min_length=4, alphabet='abcdefghijklmnop')

hashids提供了三個可選的初始化引數,當都不填時,hashids實現的功能基本就是hash化,下面介紹三個引數含義:

1)salt:生成隨機字串的種子

2)min_length:生成的隨機字串的長度

3)alphabet:生成的隨機字串內容必須是alphabet中的,alphabet長度最少16位

1)encode和encrypt:加密,encrypt和encode作用用法一樣,看了原始碼encrypt會在v2版本被棄用,只能加密int型別

>>> hasher.encode(1234)

'1lj'

>>> hasher.encrypt(1234)

'1lj'

2)decode和decrypt:解密,decode和decrypt作用用法一樣,看了原始碼decrypt會在v2版本被棄用

>>> hasher.decode('1lj')

(1234,)

>>> hasher.decrypt('1lj')

(1234,)

3)encode_hex和decode_hex:用來加解密可以轉換位int型別的字串,比如mongo的id是字串型的數字

>>> hasher.encode_hex('1234')

'qymp'

>>> hasher.decode_hex('qymp')

'1234'

JavaScript數字轉字串,字串轉數字

1 數字轉字串 這裡的name需要繫結字串型別,而index是陣列型別,就是拼接了乙個空字串 也可以使用tostring 字串轉數字 2 var s 234 3 1 純數字轉換 4 5 1 字串在運算操作中會被當做數字型別來處理 6 s 1 7 8 2 字元前加 9 console.log s 10...

隨機字串

region 隨機字串 生成隨機字串 指定長度 是否允許重複 種子值 只需要第0個值,null表示使用預設種子值 字符集 public static string random int length,bool repeat,int seed,params char all random rnd ne...

隨機字串

直接產生隨機的字串的庫函式是沒有的,但是可以這麼實現 1.產生固定長度的隨機的字串 主要就是產生固定個數的隨機字元,那麼就簡單了,用兩組隨機數 一組 隨機產生0 25的整數num,然後用 a num來賦值小寫字母,a num來賦值大寫字母 一組 隨機產生0,1,產生0,用 a 來和num相加,生成小...