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') (버전> = 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 Module Syntax ( 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' "esnext.bigint" 또는 "esnext" "lib" 아래에 tsconfig.json 파일에 추가하십시오.
{
"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이므로 이모티콘을 알파벳으로 사용할 수도 있습니다.
숫자 대신 16 진수를 인코딩합니다.
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 주요 0을 포함하더라도 항상 같은 길이를 초래한다는 것입니다.
예를 들어 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 이 코드는 URL과 같은 가시 장소에 생성 된 ID를 배치하려는 의도로 작성되었습니다. 따라서 기본적으로 알고리즘은 다음과 같은 문자가없는 ID를 생성하여 가장 일반적인 영어 저주 단어를 생성하지 않기 위해 시도합니다.
c, f, h, i, s, t, u
해시드 생성자에게 네 번째 인수를 제공하여 서로 옆에 배치해서는 안되는 숯을 사용자 정의 할 수 있습니다.
// 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.