Hashids เป็นไลบรารี JavaScript ขนาดเล็กเพื่อสร้าง ID ที่มีลักษณะคล้าย YouTube จากตัวเลข ใช้เมื่อคุณไม่ต้องการเปิดเผยรหัสฐานข้อมูลของคุณไปยังผู้ใช้: http://hashids.org/javascript
ติดตั้ง Hashids ผ่าน:
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 ) คุณจะต้องติดตั้งประเภท Node.js จากที่เก็บ DefinitelyTyped
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' เพิ่ม "esnext.bigint" หรือ "esnext" ในไฟล์ tsconfig.json ของคุณภายใต้ "lib" :
{
"compilerOptions" : {
...
"lib" : [
" esnext.bigint " ,
...
]
}
} โปรดทราบว่าสภาพแวดล้อมของคุณไม่จำเป็นต้องสนับสนุน BigInt สำหรับ Hashids ในการทำงาน
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 ของคุณจะมีความยาว อย่าง นั้น
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 คุณสามารถใช้อิโมจิเป็นตัวอักษรได้
เข้ารหัส Hex แทนตัวเลข:
มีประโยชน์หากคุณต้องการเข้ารหัสหมายเลขเช่น ObjectIds ของ Mongo
โปรดทราบว่า ไม่มี การ จำกัด จำนวนเลขฐานสิบหกที่คุณสามารถผ่านได้
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 เหล่านี้สุ่มและคาดเดาไม่ได้:
ไม่มีรูปแบบการทำซ้ำที่แสดงมี 3 ตัวเลขที่เหมือนกันใน ID:
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 ที่ไม่เคยมีตัวอักษรต่อไปนี้อยู่ข้างๆ:
c, f, h, i, s, t, u
คุณสามารถปรับแต่ง Chars ที่ไม่ควรวางไว้ข้างๆกันโดยให้อาร์กิวเมนต์ที่ 4 กับตัวสร้าง 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 มาตรฐานเพื่อเข้ารหัสและถอดรหัสได้ในลักษณะเดียวกับตัวเลขธรรมดา
การพยายามถอดรหัส Hashid ที่เข้ารหัส BigInt ในสภาพแวดล้อมที่ไม่ได้รับการสนับสนุนจะทำให้เกิดข้อผิดพลาด
ใบอนุญาต MIT ดูไฟล์ใบอนุญาต คุณสามารถใช้ hashids ในโครงการโอเพ่นซอร์สและผลิตภัณฑ์เชิงพาณิชย์ อย่าทำลายอินเทอร์เน็ต Kthxbye