const crypto = require('crypto'); /** * 生成指定位数的随机字符串 * @param digit number 要生成随机字符串的位数 * @return string 随机字符串 */ function randomChar(digit = 4) { let strs = []; do { let str = Math.random().toString(16).substr(2); if (str.length >= digit) { strs.push(str.substr(0, digit)); digit = 0; } else { strs.push(str); digit -= str.length } } while(digit); return strs.join(''); } /** * 用md5加密指定字符串 * @param encStr string 要加密的字符串 * @param pow number 要加密的次数 * @return string 加密后的字符串 */ function hashEncryption(encStr, pow = 1) { for (let i = 0; i < pow; i++) { encStr = crypto.createHash('md5').update(encStr, 'utf8').digest('hex'); } return encStr; } console.log(hashEncryption('admin', 2)) module.exports = exports = { randomChar, hashEncryption }