crypto.js/src/index.js

221 lines
4.1 KiB
JavaScript

/**
* 加密类 md5/sha1/base64
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 18:11:51
*/
import os from 'os'
import fs from 'fs'
import {
crypto,
hash,
hmac,
cipher,
decipher,
cipheriv,
decipheriv
} from './helper.js'
const MAC = (function (ns) {
for (let k in ns) {
if (k === 'lo') {
continue
}
let _ = ns[k].pop()
return _.mac.replace(/:/g, '').slice(-4)
}
return Math.random().toString(16).slice(-4)
})(os.networkInterfaces())
let __inc__ = 4096
/**
* [base64encode base64编码]
* @param {Str/Num/Buffer} str [要编码的字符串]
* @param {bool} urlFriendly [是否对URL友好, 默认否, 是则会把+转成-, /转成_]
*/
export function base64encode(str, urlFriendly) {
let buf, str64
if (Buffer.isBuffer(str)) {
buf = str
} else {
buf = Buffer.from(str + '')
}
str64 = buf.toString('base64')
if (urlFriendly) {
return str64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
}
return str64
}
/**
* [base64decode base64解码, 返回Buffer对象]
* @param {Str} str [要解码的字符串]
* @param {bool} urlFriendly [之前是否对结果采用了URL友好处理]
*/
export function base64decode(str, urlFriendly) {
if (urlFriendly) {
str = str
.replace(/-/g, '+')
.replace(/_/g, '/')
.replace(/[^A-Za-z0-9\+\/]/g, '')
}
return Buffer.from(str, 'base64')
}
/**
* [rand 生成指定长度的随机字符串]
* @param {[type]} len [要得到的字符串长度]
* @param {[type]} forceNum [是否强制返回纯数字]
*/
export function rand(len, forceNum) {
let str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'
if (forceNum) {
str = '0123456789'
}
let max = str.length
let tmp = ''
for (let i = 0; i < len; i++) {
tmp += str[crypto.randomInt(max)]
}
return tmp
}
// 返回一个如下格式的 xxxxxxxx-xxxx-xxxx-xxxxxxxx 的唯一ID
export function uuid(pipe = '-') {
let str = crypto.randomUUID()
let now = (~~(Date.now() / 1000)).toString(16)
if (__inc__ > 65535) {
__inc__ = 4096
}
str = __inc__.toString(16) + str
__inc__++
return now + pipe + MAC + pipe + str.slice(0, 4) + pipe + str.slice(-8)
}
/**
* [md5 md5加密]
* @param {Str/Num} str [要加密的字符串]
* @param {Str} encode [hex/base64]
*/
export function md5(str, encode) {
if (typeof str === 'number') {
str += ''
}
if (typeof str === 'string' || Buffer.isBuffer(str)) {
return hash('md5', str, encode)
}
return str
}
/**
* [md5Sign 获取文件的md5签名]
* @param {Str} file [文件路径]
*/
export function md5Sign(file) {
try {
let buf = fs.readFileSync(file)
return hash('md5', buf)
} catch (e) {
return null
}
}
/**
* [sha1 sha1加密]
* @param {Str/Num} str [要加密的字符串]
* @param {Str} encode [hex/base64]
*/
export function sha1(str, encode) {
if (typeof str === 'number') {
str += ''
}
if (typeof str === 'string' || Buffer.isBuffer(str)) {
return hash('sha1', str, encode)
}
return str
}
/**
* [sha1Sign 获取文件的sha1签名]
* @param {Str} file [文件路径]
*/
export function sha1Sign(file) {
try {
let buf = fs.readFileSync(file)
return hash('sha1', buf)
} catch (e) {
return null
}
}
/**
* [sha256 sha256加密]
* @param {Str/Num} str [要加密的字符串]
* @param {Str} encoding [hex/base64]
*/
export function sha256(str, encoding) {
if (typeof str === 'number') {
str += ''
}
if (typeof str === 'string' || Buffer.isBuffer(str)) {
return hash('sha256', str, encoding)
}
return str
}
/**
* [sha256Sign 获取文件的sha256签名]
* @param {Str} file [文件路径]
*/
export function sha256Sign(file) {
try {
let buf = fs.readFileSync(file)
return hash('sha256', buf)
} catch (e) {
return null
}
}
export {
crypto,
crypto as origin,
hash,
hmac,
cipher,
decipher,
cipheriv,
decipheriv
}
export default {
base64encode,
base64decode,
rand,
uuid,
md5,
md5Sign,
sha1,
sha1Sign,
sha256,
sha256Sign,
crypto,
origin: crypto,
hash,
hmac,
cipher,
decipher,
cipheriv,
decipheriv
}
base on crypto module
JavaScript 100%