优化uuid
parent
9abf208084
commit
f0240827db
|
@ -58,6 +58,8 @@ crypto.rand(10, true) // 3458765234
|
||||||
|
|
||||||
### uuid()
|
### uuid()
|
||||||
> 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
> 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
||||||
|
>> 1、加入机器码, 减小不同机器生成的uuid相同的机率
|
||||||
|
>> 2、每秒可生成20万个ID(测试机器: Intel i5-8500B@3.00GHz 16G内存)
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
let crypto = require('crypto.js')
|
let crypto = require('crypto.js')
|
||||||
|
|
26
index.js
26
index.js
|
@ -4,11 +4,20 @@
|
||||||
* @date 2020/09/16 18:11:51
|
* @date 2020/09/16 18:11:51
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const os = require('os')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const Helper = require('./lib/helper.js')
|
const Helper = require('./lib/helper.js')
|
||||||
|
|
||||||
const PID = process.pid
|
const MAC = (function(ns) {
|
||||||
const PPID = process.ppid
|
for (let k in ns) {
|
||||||
|
let _ = ns[k].pop()
|
||||||
|
if (_.mac !== '00:00:00:00:00:00') {
|
||||||
|
return _.mac
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return process.pid.toString(16) + process.ppid.toString(16)
|
||||||
|
})(os.networkInterfaces())
|
||||||
|
|
||||||
var __inc__ = 1024
|
var __inc__ = 1024
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,23 +83,16 @@ Helper.rand = function(len, forceNum) {
|
||||||
Helper.uuid = function(pipe = '-') {
|
Helper.uuid = function(pipe = '-') {
|
||||||
var rand = Helper.origin.randomBytes(8).toString('hex')
|
var rand = Helper.origin.randomBytes(8).toString('hex')
|
||||||
var now = (~~(Date.now() / 1000)).toString(16)
|
var now = (~~(Date.now() / 1000)).toString(16)
|
||||||
var inc
|
var str
|
||||||
|
|
||||||
__inc__++
|
__inc__++
|
||||||
if (__inc__ > 65535) {
|
if (__inc__ > 65535) {
|
||||||
__inc__ = 1024
|
__inc__ = 1024
|
||||||
}
|
}
|
||||||
|
str = md5(MAC + rand + __inc__)
|
||||||
inc = (__inc__ + PID + PPID).toString(16).padStart(4, '0') + rand.slice(0, 4)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
now +
|
now + pipe + str.slice(0, 4) + pipe + str.slice(4, 8) + pipe + str.slice(-8)
|
||||||
pipe +
|
|
||||||
inc.slice(0, 4) +
|
|
||||||
pipe +
|
|
||||||
inc.slice(4, 8) +
|
|
||||||
pipe +
|
|
||||||
rand.slice(-8)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
26
index.mjs
26
index.mjs
|
@ -4,11 +4,20 @@
|
||||||
* @date 2020/09/16 18:11:51
|
* @date 2020/09/16 18:11:51
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import os from 'os'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import Helper from './lib/helper.mjs'
|
import Helper from './lib/helper.mjs'
|
||||||
|
|
||||||
const PID = process.pid
|
const MAC = (function(ns) {
|
||||||
const PPID = process.ppid
|
for (let k in ns) {
|
||||||
|
let _ = ns[k].pop()
|
||||||
|
if (_.mac !== '00:00:00:00:00:00') {
|
||||||
|
return _.mac
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return process.pid.toString(16) + process.ppid.toString(16)
|
||||||
|
})(os.networkInterfaces())
|
||||||
|
|
||||||
var __inc__ = 1024
|
var __inc__ = 1024
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,23 +83,16 @@ export function rand(len, forceNum) {
|
||||||
export function uuid(pipe = '-') {
|
export function uuid(pipe = '-') {
|
||||||
var rand = Helper.origin.randomBytes(8).toString('hex')
|
var rand = Helper.origin.randomBytes(8).toString('hex')
|
||||||
var now = (~~(Date.now() / 1000)).toString(16)
|
var now = (~~(Date.now() / 1000)).toString(16)
|
||||||
var inc
|
var str
|
||||||
|
|
||||||
__inc__++
|
__inc__++
|
||||||
if (__inc__ > 65535) {
|
if (__inc__ > 65535) {
|
||||||
__inc__ = 1024
|
__inc__ = 1024
|
||||||
}
|
}
|
||||||
|
str = md5(MAC + rand + __inc__)
|
||||||
inc = (__inc__ + PID + PPID).toString(16).padStart(4, '0') + rand.slice(0, 4)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
now +
|
now + pipe + str.slice(0, 4) + pipe + str.slice(4, 8) + pipe + str.slice(-8)
|
||||||
pipe +
|
|
||||||
inc.slice(0, 4) +
|
|
||||||
pipe +
|
|
||||||
inc.slice(4, 8) +
|
|
||||||
pipe +
|
|
||||||
rand.slice(-8)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "crypto.js",
|
"name": "crypto.js",
|
||||||
"version": "2.0.4",
|
"version": "2.0.5",
|
||||||
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
|
"description": "原生crypto加密模块的二次封装,简化常用加密函数的使用",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"md5",
|
"md5",
|
||||||
|
|
Loading…
Reference in New Issue