UNPKG

hwplugins

Version:

二次封装扩展随机数、手机号码/身份证号码脱敏、时间格式化等...

163 lines (162 loc) 4.22 kB
module.exports = class SplitKey { constructor(option) { new Promise((res, rej) => { this.option = { section: 4, // 拆分4 等分 method: ["aaa", "bbb", "ccc", "ddd"], // 方法名 methodRandom: false, // 调用方法是否随机 ...option, }; res("初始成功"); }); } // 随机数 random(min = 0, max = 10) { if (min <= Number.MAX_SAFE_INTEGER && max <= Number.MAX_SAFE_INTEGER) { return Math.floor(Math.random() * (max - min + 1)) + min; } } // 拆分str sliceStr(key, option) { this.option = Object.assign(this.option, option); if (!key || key.length <= 0) { return console.error("没有可处理的值"); } const result = []; const publicKeyLen = key.length; for (let i = 0; i < publicKeyLen; i += publicKeyLen / this.option.section) { result.push(key.slice(i, i + publicKeyLen / this.option.section)); } // 处理后数据 const afterArr = []; // 等分字符串数组 result.forEach((v, i) => { let index = 0; if (this.option.methodRandom) index = this.random(0, 3); else index = i % 4; const val = this[this.option.method[index]](v); afterArr.push(val); }); return afterArr; } /** * 混淆 * **/ // 混淆一 bbb(str) { const result = []; const halfLength = Math.ceil(str.length / 2); const firstHalf = str.slice(0, halfLength); const secondHalf = str.slice(halfLength); for (let i = 0; i < halfLength; i++) { result.push( (firstHalf[i] || "") + (secondHalf[secondHalf.length - 1 - i] || "") ); } return { state: "bbb", result }; } // 混淆二 ccc(str) { const result = []; const halfLength = Math.ceil(str.length / 2); const firstHalf = str.slice(0, halfLength); const secondHalf = str.slice(halfLength); for (let i = 0; i < halfLength; i++) { result.push( (secondHalf[secondHalf.length - 1 - i] || "") + (firstHalf[i] || "") ); } return { state: "ccc", result }; } // 混淆三 ddd(str) { const result = []; const halfLength = Math.ceil(str.length / 2); const firstHalf = str.slice(0, halfLength); const secondHalf = str.slice(halfLength); for (let i = 0; i < halfLength; i++) { result.push((secondHalf[i] || "") + (firstHalf[i] || "")); } return { state: "ddd", result }; } // 混淆四 aaa(str) { const result = []; const halfLength = Math.ceil(str.length / 2); const firstHalf = str.slice(0, halfLength); const secondHalf = str.slice(halfLength); for (let i = 0; i < halfLength; i++) { result.push((firstHalf[i] || "") + (secondHalf[i] || "")); } return { state: "aaa", result }; } /** * 解析混淆 * **/ // 合并密钥 mergeStr(str, methodName = "aaa") { return this[methodName + "1"](str); } /** * 解析混淆 * **/ // 合并密钥 newMergeStr(arr) { const sortArr = arr.sort((a, b) => a.index - b.index); let str = ""; sortArr.forEach((v) => { str += this[v.state + "1"](v.result); }); return str; } // 解析一 aaa1(newArr) { let new1 = ""; let new2 = ""; newArr.forEach((item) => { const ab = item.split(""); if ((ab.length & 2) == 0) { ab.push(""); } new1 += ab[0] || ""; new2 += ab[1] || ""; }); return new1 + new2; } // 解析二 bbb1(newArr) { let new1 = ""; let new2 = ""; newArr.forEach((item) => { const ab = item.split(""); new1 += ab[0] || ""; new2 = (ab[1] || "") + (new2 || ""); }); return new1 + new2; } // 解析三 ccc1(newArr) { let new1 = ""; let new2 = ""; newArr.forEach((item) => { const ab = item.split(""); new1 += ab[1] || ""; new2 = (ab[0] || "") + (new2 || ""); }); return new1 + new2; } // 解析四 ddd1(newArr) { let new1 = ""; let new2 = ""; newArr.forEach((item) => { const ab = item.split(""); if ((ab.length & 2) == 0) { ab.unshift(""); } new1 += ab[1] || ""; new2 += ab[0] || ""; }); return new1 + new2; } };