2018-05-25 00:09:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* 加密类 md5/sha1/base64
|
2020-09-16 20:01:35 +08:00
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
|
* @date 2020/09/16 18:11:51
|
2018-05-25 00:09:29 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
const fs = require('fs')
|
|
|
|
|
const Helper = require('./lib/helper.js')
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2021-06-23 17:25:04 +08:00
|
|
|
|
const PID = process.pid
|
|
|
|
|
const PPID = process.ppid
|
2020-09-16 20:01:35 +08:00
|
|
|
|
var __inc__ = 1024
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [base64encode base64编码]
|
|
|
|
|
* @param {Str/Num/Buffer} str [要编码的字符串]
|
|
|
|
|
* @param {bool} urlFriendly [是否对URL友好,默认否,是则会把+转成-,/转成_]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.base64encode = function(str, urlFriendly) {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
var buf, str64
|
2019-06-17 17:11:53 +08:00
|
|
|
|
|
2020-09-24 18:02:13 +08:00
|
|
|
|
if (Buffer.isBuffer(str)) {
|
|
|
|
|
buf = str
|
|
|
|
|
} else {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
buf = Buffer.from(str + '')
|
|
|
|
|
}
|
2020-09-24 18:02:13 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
str64 = buf.toString('base64')
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
if (urlFriendly) {
|
|
|
|
|
return str64
|
|
|
|
|
.replace(/\+/g, '-')
|
|
|
|
|
.replace(/\//g, '_')
|
|
|
|
|
.replace(/=/g, '')
|
|
|
|
|
}
|
|
|
|
|
return str64
|
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [base64decode base64解码, 返回Buffer对象]
|
|
|
|
|
* @param {Str} str [要解码的字符串]
|
|
|
|
|
* @param {bool} urlFriendly [之前是否对结果采用了URL友好处理]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.base64decode = function(str, urlFriendly) {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
if (urlFriendly) {
|
|
|
|
|
str = str
|
|
|
|
|
.replace(/-/g, '+')
|
|
|
|
|
.replace(/_/g, '/')
|
|
|
|
|
.replace(/[^A-Za-z0-9\+\/]/g, '')
|
|
|
|
|
}
|
|
|
|
|
return Buffer.from(str, 'base64')
|
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [rand 生成指定长度的随机字符串]
|
|
|
|
|
* @param {[type]} len [要得到的字符串长度]
|
|
|
|
|
* @param {[type]} forceNum [是否强制返回纯数字]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.rand = function(len, forceNum) {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
let str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'
|
|
|
|
|
if (forceNum) {
|
|
|
|
|
str = '0123456789'
|
|
|
|
|
}
|
|
|
|
|
let max = str.length
|
|
|
|
|
let tmp = ''
|
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
|
let r = (Math.random() * max) >> 0
|
|
|
|
|
tmp += str[r]
|
|
|
|
|
}
|
|
|
|
|
return tmp
|
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.uuid = function(pipe = '-') {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
var rand = Helper.origin.randomBytes(8).toString('hex')
|
2020-09-17 16:30:45 +08:00
|
|
|
|
var now = (~~(Date.now() / 1000)).toString(16)
|
2020-09-16 20:01:35 +08:00
|
|
|
|
var inc
|
|
|
|
|
|
2021-06-23 17:25:04 +08:00
|
|
|
|
__inc__++
|
|
|
|
|
if (__inc__ > 65535) {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
__inc__ = 1024
|
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2021-06-23 17:25:04 +08:00
|
|
|
|
inc = (__inc__ + PID + PPID).toString(16).padStart(4, '0')
|
|
|
|
|
|
|
|
|
|
return now + pipe + inc + pipe + rand.slice(0, 4) + pipe + rand.slice(-8)
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [md5 md5加密]
|
|
|
|
|
* @param {Str/Num} str [要加密的字符串]
|
|
|
|
|
* @param {Str} encode [hex/base64]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.md5 = function(str, encode) {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
if (typeof str === 'number') {
|
|
|
|
|
str += ''
|
|
|
|
|
}
|
2020-09-24 18:02:13 +08:00
|
|
|
|
if (typeof str === 'string' || Buffer.isBuffer(str)) {
|
|
|
|
|
return Helper.hash('md5', str, encode)
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-24 18:02:13 +08:00
|
|
|
|
return str
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [md5Sign 获取文件的md5签名]
|
|
|
|
|
* @param {Str} file [文件路径]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.md5Sign = function(file) {
|
2020-09-24 18:02:13 +08:00
|
|
|
|
if (fs.accessSync(file, fs.constants.R_OK)) {
|
|
|
|
|
var buf = fs.readFileSync(file)
|
|
|
|
|
return Helper.hash('md5', buf)
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2020-09-24 18:02:13 +08:00
|
|
|
|
return null
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [sha1 sha1加密]
|
|
|
|
|
* @param {Str/Num} str [要加密的字符串]
|
|
|
|
|
* @param {Str} encode [hex/base64]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.sha1 = function(str, encode) {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
if (typeof str === 'number') {
|
|
|
|
|
str += ''
|
|
|
|
|
}
|
2020-09-24 18:02:13 +08:00
|
|
|
|
if (typeof str === 'string' || Buffer.isBuffer(str)) {
|
|
|
|
|
return Helper.hash('sha1', str, encode)
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-24 18:02:13 +08:00
|
|
|
|
return str
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [sha1Sign 获取文件的sha1签名]
|
|
|
|
|
* @param {Str} file [文件路径]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.sha1Sign = function(file) {
|
2020-09-24 18:02:13 +08:00
|
|
|
|
if (fs.accessSync(file, fs.constants.R_OK)) {
|
|
|
|
|
var buf = fs.readFileSync(file)
|
|
|
|
|
return Helper.hash('sha1', buf)
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2020-09-24 18:02:13 +08:00
|
|
|
|
return null
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2019-06-17 17:11:53 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [sha256 sha256加密]
|
|
|
|
|
* @param {Str/Num} str [要加密的字符串]
|
|
|
|
|
* @param {Str} encoding [hex/base64]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.sha256 = function(str, encoding) {
|
2020-09-16 20:01:35 +08:00
|
|
|
|
if (typeof str === 'number') {
|
|
|
|
|
str += ''
|
|
|
|
|
}
|
2020-09-24 18:02:13 +08:00
|
|
|
|
if (typeof str === 'string' || Buffer.isBuffer(str)) {
|
|
|
|
|
return Helper.hash('sha256', str, encoding)
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-24 18:02:13 +08:00
|
|
|
|
return str
|
2020-09-16 20:01:35 +08:00
|
|
|
|
}
|
2018-05-25 00:09:29 +08:00
|
|
|
|
|
2020-09-16 20:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* [sha256Sign 获取文件的sha256签名]
|
|
|
|
|
* @param {Str} file [文件路径]
|
|
|
|
|
*/
|
2020-09-17 16:30:45 +08:00
|
|
|
|
Helper.sha256Sign = function(file) {
|
2020-09-24 18:02:13 +08:00
|
|
|
|
if (fs.accessSync(file, fs.constants.R_OK)) {
|
|
|
|
|
var buf = fs.readFileSync(file)
|
|
|
|
|
return Helper.hash('sha256', buf)
|
2018-05-25 00:09:29 +08:00
|
|
|
|
}
|
2020-09-24 18:02:13 +08:00
|
|
|
|
return null
|
2018-05-25 00:09:29 +08:00
|
|
|
|
}
|
2020-09-16 20:01:35 +08:00
|
|
|
|
|
|
|
|
|
module.exports = Helper
|