From f0240827db488bb05f64ae72e8735a9995284e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Thu, 24 Jun 2021 17:55:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96uuid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 2 ++ index.js | 26 ++++++++++++++------------ index.mjs | 26 ++++++++++++++------------ package.json | 2 +- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Readme.md b/Readme.md index 931a2e6..ed7157f 100644 --- a/Readme.md +++ b/Readme.md @@ -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') diff --git a/index.js b/index.js index 90c40e0..6b01694 100644 --- a/index.js +++ b/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) ) } diff --git a/index.mjs b/index.mjs index 18932d9..9964160 100644 --- a/index.mjs +++ b/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) ) } diff --git a/package.json b/package.json index 209c459..03615a2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto.js", - "version": "2.0.4", + "version": "2.0.5", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", "keywords": [ "md5",