Hashids是小型JavaScript庫,可從數字中生成類似YouTube的ID。當您不想將數據庫ID公開給用戶時使用:http://hashids.org/javascript
安裝橋樑通過:
yarn add hashids (或直接在dist/hashids.js上使用代碼)
import Hashids from 'hashids'
const hashids = new Hashids ( )
console . log ( hashids . encode ( 1 ) ) const Hashids = require ( 'hashids/cjs' )
const hashids = new Hashids ( )
console . log ( hashids . encode ( 1 ) )注意:當使用支持條件導出的節點時, require('hashids') (版本> = 13)也將起作用。
< script type =" text/javascript " src =" hashids.min.js " > </ script >
< script type =" text/javascript " >
var hashids = new Hashids ( ) ;
console . log ( hashids . encode ( 1 ) ) ;
</ script > 根據環境import或require (見上文)。如果要使用commonjs模塊語法( require ),則需要從DefinitelyTyped存儲庫中安裝node.js類型。
npm install @types/node
如果要使用ESM語法( import Hashids from 'hashids' ),則需要在tsconfig.json中包含以下選項。
{
"allowSyntheticDefaultImports" : true ,
"esModuleInterop" : true
}如果您直接導入CONCORJS版本: import Hashids from 'hashids/cjs'則不需要上述。
如果您發現錯誤說明: Cannot find name 'BigInt' ,請在tsconfig.json文件中添加"esnext.bigint"或"esnext" , "lib" :
{
"compilerOptions" : {
...
"lib" : [
" esnext.bigint " ,
...
]
}
}請注意,您的環境實際上不必為Hashids的功能支持BigInt 。
const hashids = new Hashids ( )
const id = hashids . encode ( 1 , 2 , 3 ) // o2fXhV
const numbers = hashids . decode ( id ) // [1, 2, 3] 更多方法傳遞到encode() :
const hashids = new Hashids ( )
console . log ( hashids . encode ( 1 , 2 , 3 ) ) // o2fXhV
console . log ( hashids . encode ( [ 1 , 2 , 3 ] ) ) // o2fXhV
// strings containing integers are coerced to numbers:
console . log ( hashids . encode ( '1' , '2' , '3' ) ) // o2fXhV
console . log ( hashids . encode ( [ '1' , '2' , '3' ] ) ) // o2fXhV
// BigInt support:
console . log ( hashids . encode ( [ 1n , 2n , 3n ] ) ) // o2fXhV
// Hex notation BigInt:
console . log ( hashids . encode ( [ 0x1n , 0x2n , 0x3n ] ) ) // o2fXhV使您的ID與眾不同:
傳遞“鹽”以使您的ID獨特(例如,項目名稱):
var hashids = new Hashids ( 'My Project' )
console . log ( hashids . encode ( 1 , 2 , 3 ) ) // Z4UrtW
var hashids = new Hashids ( 'My Other Project' )
console . log ( hashids . encode ( 1 , 2 , 3 ) ) // gPUasb使用填充使您的ID更長:
請注意,ID僅適合至少一定長度。這並不意味著您的ID將正是那個長度。
const hashids = new Hashids ( ) // no padding
console . log ( hashids . encode ( 1 ) ) // jR
const hashids = new Hashids ( '' , 10 ) // pad to length 10
console . log ( hashids . encode ( 1 ) ) // VolejRejNm傳遞自定義字母:
const hashids = new Hashids ( '' , 0 , 'abcdefghijklmnopqrstuvwxyz' ) // all lowercase
console . log ( hashids . encode ( 1 , 2 , 3 ) ) // mdfphx默認字母為abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 。
由於v2.0,您甚至可以使用表情符號作為字母。
編碼十六進製而不是數字:
如果您想編碼Mongo的Objectids等數字,則有用。
請注意,您可以通過的十六進制數量沒有限制。
var hashids = new Hashids ( )
var id = hashids . encodeHex ( '507f1f77bcf86cd799439011' ) // y42LW46J9luq3Xq9XMly
var hex = hashids . decodeHex ( id ) // 507f1f77bcf86cd799439011請注意,這與:
const hashids = new Hashids ( )
const id = Hashids . encode ( BigInt ( '0x507f1f77bcf86cd799439011' ) ) // y8qpJL3ZgzJ8lWk4GEV
const hex = Hashids . decode ( id ) [ 0 ] . toString ( 16 ) // 507f1f77bcf86cd799439011兩者之間的區別在於,即使包含領先的零,內置的encodeHex也將始終導致相同的長度。
例如, hashids.encodeHex('00000000')將編碼為qExOgK7並解碼為'00000000' (保留長度信息)。
解碼時,輸出始終是數字數組(即使您僅編碼一個數字):
const hashids = new Hashids ( )
const id = hashids . encode ( 1 )
console . log ( hashids . decode ( id ) ) // [1]不支持編碼負數。
如果將偽造輸入傳遞給encode() ,則將返回一個空字符串:
const hashids = new Hashids ( )
const id = hashids . encode ( '123a' )
console . log ( id === '' ) // true請勿將此庫用作安全工具,也不要編碼敏感數據。這不是加密庫。
橋樑的主要目的是混淆ID。它並不意味著或測試用作安全或壓縮工具。話雖如此,該算法確實試圖使這些ID隨機且不可預測:
沒有重複模式顯示ID中有3個相同的數字:
const hashids = new Hashids ( )
console . log ( hashids . encode ( 5 , 5 , 5 ) ) // A6t1tQ相同的數字相同:
const hashids = new Hashids ( )
console . log ( hashids . encode ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ) // wpfLh9iwsqt0uyCEFjHM
console . log ( hashids . encode ( 1 ) ) // jR
console . log ( hashids . encode ( 2 ) ) // k5
console . log ( hashids . encode ( 3 ) ) // l5
console . log ( hashids . encode ( 4 ) ) // mO
console . log ( hashids . encode ( 5 ) ) // nR 該代碼的編寫意圖是將創建的ID放置在可見位置,例如URL。因此,默認情況下,該算法試圖通過生成從未彼此相鄰的以下字母的ID來避免生成最常見的英語詛咒單詞:
c, f, h, i, s, t, u
您可以通過向Hashids構造函數提供第四個論點來自定義不應彼此相鄰放置的字符:
// first 4 arguments will fallback to defaults (empty salt, no minimum length, default alphabet)
const hashids = new Hashids ( undefined , undefined , undefined , 'zyxZYX' ) 如果您的環境支持BigInt ,則可以使用標準API與普通數字相同的方式進行編碼和解碼。
試圖在不受支持的環境上解碼BigInt編碼的哈希德,這會引發錯誤。
麻省理工學院許可證。請參閱許可證文件。您可以在開源項目和商業產品中使用橋樑。不要打破互聯網。 kthxbye。