Hashidsは、数字からYouTubeのようなIDを生成するための小さなJavaScriptライブラリです。データベース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') 。
< 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
} CommonJSバージョンを直接インポートする場合、上記は必要ありません。 import Hashids from 'hashids/cjs' 。
エラーが表示された場合: Cannot find name 'BigInt'場合、 "lib"の下にあるtsconfig.jsonファイルに"esnext.bigint"または"esnext"を追加します。
{
"compilerOptions" : {
...
"lib" : [
" esnext.bigint " ,
...
]
}
}あなたの環境は、実際にハシドが機能するために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 ] ) ) // o2fXhVIDを一意にする:
「塩」を渡して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のような番号をエンコードしたい場合に便利です。
渡すことができる16進数の大きさに制限がないことに注意してください。
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 2つの違いは、組み込みのencodeHex 、たとえ主要なゼロが含まれていても、常に同じ長さになることです。
たとえば、 hashids.encodeHex('00000000')はqExOgK7にエンコードし、 '00000000'にデコードします(長さ情報が保持されます)。
デコードの場合、出力は常に数字の配列です(たとえ1つの数値のみをエンコードしても):
const hashids = new Hashids ( )
const id = hashids . encode ( 1 )
console . log ( hashids . decode ( id ) ) // [1]負の数のエンコードはサポートされていません。
bogus inputを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 このコードは、URLのような目に見える場所に作成されたIDを配置する目的で記述されました。したがって、デフォルトでは、アルゴリズムは、次の文字が隣にないIDを生成することにより、最も一般的な英語の呪い語の生成を避けようとします。
c, f, h, i, s, t, u
Hashidsコンストラクターに4番目の議論を提供することにより、隣り合ってはいけないCharをカスタマイズできます。
// 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されたハシドをエンコードしようとすると、エラーが発生します。
MITライセンス。ライセンスファイルを参照してください。オープンソースプロジェクトや商用製品でハシドを使用できます。インターネットを壊さないでください。 kthxbye。