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