From e5aefc407dd4392dbbdd20869b166605582de39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Mon, 2 Jul 2018 01:53:02 +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 --- index.js | 15 ++++++++++++--- package.json | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 25fa28c..1ca1f94 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,8 @@ const FS = require('fs') module.exports = { origin: CRYPTO, + __stamp__: '', + __inc__: 0, hash(mode, data, outEncode) { let sum = CRYPTO.createHash(mode) @@ -97,10 +99,17 @@ module.exports = { // 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID uuid() { let rand = CRYPTO.randomBytes(8).toString('hex') - let stamp = ((Date.now() / 1000) >>> 0).toString(16) + let now = (Date.now() / 1000).toString(16).slice(0, 8) + if (this.__stamp__ === now) { + this.__inc__++ + } else { + this.__stamp__ = now + this.__inc__ = 0 + } + rand = this.__inc__.toString(16) + rand - rand = rand.replace(/^([0-9a-z]{4})([0-9a-z]{4})([0-9a-z]{8})$/, '$1-$2-$3') - return stamp + '-' + rand + rand = rand.slice(0, 4) + '-' + rand.slice(4, 8) + '-' + rand.slice(8, 16) + return this.__stamp__ + '-' + rand }, /** diff --git a/package.json b/package.json index 00b78b9..4385e16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto.js", - "version": "1.1.2", + "version": "1.1.3", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", "keywords": [ "md5",