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。