2.0.0
parent
95f5b85bb6
commit
c9824a10e0
|
@ -0,0 +1,11 @@
|
|||
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
._*
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
dist/
|
37
Readme.md
37
Readme.md
|
@ -4,30 +4,49 @@
|
|||
|
||||
## 安装
|
||||
|
||||
- 使用npm安装
|
||||
|
||||
```bash
|
||||
npm install crypto.js
|
||||
```
|
||||
|
||||
- 从github获取
|
||||
|
||||
```bash
|
||||
git clone https://github.com/yutent/crypto.js.git
|
||||
## 说明
|
||||
> 本模块内置2种不同的引入方式。
|
||||
|
||||
```js
|
||||
// 1、 传统的 commonJS引入, 所有的方法都在上面
|
||||
var crypto = require('crypto.js')
|
||||
|
||||
|
||||
// 2、 全新的 ESM 方式
|
||||
import crypto from 'crypto.js'
|
||||
import {
|
||||
uuid,
|
||||
rand,
|
||||
md5,
|
||||
md5Sign,
|
||||
sha1,
|
||||
sha1Sign,
|
||||
sha256,
|
||||
sha256Sign,
|
||||
base64encode,
|
||||
base64decode,
|
||||
} from 'crypto.js'
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 属性
|
||||
> 其实就一个属性,即 `origin`,即为原生的`crypto`对象,方便在封装的方法中无法满足需求时,可以自行调用原生的`crypto`实现。
|
||||
|
||||
## 常用API方法
|
||||
> 对使用频率非常高的几种加密/编码进行更加简便的封装。
|
||||
|
||||
### rand(len[, forceNum])
|
||||
- len `<Number>`
|
||||
- forceNum `<Boolean>` 可选
|
||||
### rand(len[, onlyNumber])
|
||||
- len `<Number>` 需要的字符长度
|
||||
- onlyNumber `<Boolean>` 返回纯数字字符串 [可选]
|
||||
|
||||
> 该方法用于生成指定长度的随机字符串`[a-z-A-z0-9]`,参数`len`即为要生成的字符串长度了; 而`forceNum` 顾名思义,就是是否强制返回纯数字字符串。
|
||||
> 该方法用于生成指定长度的随机字符串`[a-z-A-z0-9]`
|
||||
|
||||
```javascript
|
||||
let crypto = require('crypto.js')
|
||||
|
|
|
@ -67,9 +67,9 @@ export function rand(len, forceNum) {
|
|||
}
|
||||
|
||||
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
||||
export function uuid(pipe = '') {
|
||||
export function uuid(pipe = '-') {
|
||||
var rand = Helper.origin.randomBytes(8).toString('hex')
|
||||
var now = ~~(Date.now() / 1000).toString(16)
|
||||
var now = (~~(Date.now() / 1000)).toString(16)
|
||||
var inc
|
||||
|
||||
if (__stamp__ === now) {
|
||||
|
|
22
index.js
22
index.js
|
@ -15,7 +15,7 @@ var __inc__ = 1024
|
|||
* @param {Str/Num/Buffer} str [要编码的字符串]
|
||||
* @param {bool} urlFriendly [是否对URL友好,默认否,是则会把+转成-,/转成_]
|
||||
*/
|
||||
exports.base64encode = function(str, urlFriendly) {
|
||||
Helper.base64encode = function(str, urlFriendly) {
|
||||
var buf, str64
|
||||
|
||||
if (!Buffer.isBuffer(str)) {
|
||||
|
@ -37,7 +37,7 @@ exports.base64encode = function(str, urlFriendly) {
|
|||
* @param {Str} str [要解码的字符串]
|
||||
* @param {bool} urlFriendly [之前是否对结果采用了URL友好处理]
|
||||
*/
|
||||
exports.base64decode = function(str, urlFriendly) {
|
||||
Helper.base64decode = function(str, urlFriendly) {
|
||||
if (urlFriendly) {
|
||||
str = str
|
||||
.replace(/-/g, '+')
|
||||
|
@ -52,7 +52,7 @@ exports.base64decode = function(str, urlFriendly) {
|
|||
* @param {[type]} len [要得到的字符串长度]
|
||||
* @param {[type]} forceNum [是否强制返回纯数字]
|
||||
*/
|
||||
exports.rand = function(len, forceNum) {
|
||||
Helper.rand = function(len, forceNum) {
|
||||
let str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'
|
||||
if (forceNum) {
|
||||
str = '0123456789'
|
||||
|
@ -67,9 +67,9 @@ exports.rand = function(len, forceNum) {
|
|||
}
|
||||
|
||||
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
||||
exports.uuid = function(pipe = '') {
|
||||
Helper.uuid = function(pipe = '-') {
|
||||
var rand = Helper.origin.randomBytes(8).toString('hex')
|
||||
var now = ~~(Date.now() / 1000).toString(16)
|
||||
var now = (~~(Date.now() / 1000)).toString(16)
|
||||
var inc
|
||||
|
||||
if (__stamp__ === now) {
|
||||
|
@ -90,7 +90,7 @@ exports.uuid = function(pipe = '') {
|
|||
* @param {Str/Num} str [要加密的字符串]
|
||||
* @param {Str} encode [hex/base64]
|
||||
*/
|
||||
exports.md5 = function(str, encode) {
|
||||
Helper.md5 = function(str, encode) {
|
||||
if (typeof str === 'number') {
|
||||
str += ''
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ exports.md5 = function(str, encode) {
|
|||
* [md5Sign 获取文件的md5签名]
|
||||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
exports.md5Sign = function(file) {
|
||||
Helper.md5Sign = function(file) {
|
||||
if (!fs.existsSync(file)) {
|
||||
return null
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ exports.md5Sign = function(file) {
|
|||
* @param {Str/Num} str [要加密的字符串]
|
||||
* @param {Str} encode [hex/base64]
|
||||
*/
|
||||
exports.sha1 = function(str, encode) {
|
||||
Helper.sha1 = function(str, encode) {
|
||||
if (typeof str === 'number') {
|
||||
str += ''
|
||||
}
|
||||
|
@ -134,7 +134,7 @@ exports.sha1 = function(str, encode) {
|
|||
* [sha1Sign 获取文件的sha1签名]
|
||||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
exports.sha1Sign = function(file) {
|
||||
Helper.sha1Sign = function(file) {
|
||||
if (!fs.existsSync(file)) {
|
||||
return null
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ exports.sha1Sign = function(file) {
|
|||
* @param {Str/Num} str [要加密的字符串]
|
||||
* @param {Str} encoding [hex/base64]
|
||||
*/
|
||||
exports.sha256 = function(str, encoding) {
|
||||
Helper.sha256 = function(str, encoding) {
|
||||
if (typeof str === 'number') {
|
||||
str += ''
|
||||
}
|
||||
|
@ -163,7 +163,7 @@ exports.sha256 = function(str, encoding) {
|
|||
* [sha256Sign 获取文件的sha256签名]
|
||||
* @param {Str} file [文件路径]
|
||||
*/
|
||||
exports.sha256Sign = function(file) {
|
||||
Helper.sha256Sign = function(file) {
|
||||
if (!fs.existsSync(file)) {
|
||||
return null
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue