From 704a806e49cb133d13cd501ae68131edb41c180f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Mon, 17 Jun 2019 17:11:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sha256Sign=E6=96=B9=E6=B3=95;?= =?UTF-8?q?uuid=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 11 +++++++++-- index.js | 28 +++++++++++++++++++++++++--- package.json | 2 +- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Readme.md b/Readme.md index 737c8f8..de44865 100644 --- a/Readme.md +++ b/Readme.md @@ -65,7 +65,7 @@ crypto.md5('hello world', 'base64') // XrY7u+Ae7tCTyyK7j1rNww== ### md5Sign(file) - file `` -> 该方法用于计算文件的md5签名,`file`即为文件的绝对路径。 +> 该方法用于计算文件的md5签名,`file`即为文件的路径。 ```javascript crypto.md5Sign('xx.jpg') @@ -89,7 +89,7 @@ crypto.sha1('hello world', 'base64') // Kq5sNclPz7QV2+lfQIuc6R7oRu0= ### sha1Sign(file) - file `` -> 该方法用于计算文件的sha1签名,`file`即为文件的绝对路径。 +> 该方法用于计算文件的sha1签名,`file`即为文件的路径。 ```javascript crypto.sha1Sign('xx.jpg') @@ -103,6 +103,13 @@ crypto.sha1Sign('xx.jpg') > 自然这方法,也没啥好说的了。 +### sha256Sign(file) +- file `` + +> 都懂的。 + + + ### base64encode(str[, urlFriendly]) - str `` | `` | `` diff --git a/index.js b/index.js index 7862c62..61dbb95 100644 --- a/index.js +++ b/index.js @@ -116,9 +116,11 @@ module.exports = { }, // 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID - uuid() { + uuid(line = true) { let rand = CRYPTO.randomBytes(8).toString('hex') let now = (Date.now() / 1000).toString(16).slice(0, 8) + + let pipe = line ? '-' : '' if (this.__stamp__ === now) { this.__inc__++ } else { @@ -127,8 +129,15 @@ module.exports = { } rand = this.__inc__.toString(16) + rand - rand = rand.slice(0, 4) + '-' + rand.slice(4, 8) + '-' + rand.slice(8, 16) - return this.__stamp__ + '-' + rand + return ( + this.__stamp__ + + pipe + + rand.slice(0, 4) + + pipe + + rand.slice(4, 8) + + pipe + + rand.slice(8, 16) + ) }, /** @@ -205,6 +214,19 @@ module.exports = { return this.hash('sha256', str, encoding) }, + /** + * [sha256Sign 获取文件的sha256签名] + * @param {Str} file [文件路径] + */ + sha256Sign(file) { + if (!FS.existsSync(file)) { + return null + } + + let fileStream = FS.readFileSync(file) + return this.hash('sha256', fileStream) + }, + /** * [base64encode base64加密] * @param {Str/Num/Buffer} str [要加密的字符串] diff --git a/package.json b/package.json index 9d7b3e7..ef59383 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto.js", - "version": "1.2.1", + "version": "1.3.0", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", "keywords": ["md5", "sha1", "base64", "fivejs", "crypto"], "author": "yutent ",