pubtool.js 889 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const crypto = require('crypto');
  2. /**
  3. * 生成指定位数的随机字符串
  4. * @param digit number 要生成随机字符串的位数
  5. * @return string 随机字符串
  6. */
  7. function randomChar(digit = 4) {
  8. let strs = [];
  9. do {
  10. let str = Math.random().toString(16).substr(2);
  11. if (str.length >= digit) {
  12. strs.push(str.substr(0, digit));
  13. digit = 0;
  14. } else {
  15. strs.push(str);
  16. digit -= str.length
  17. }
  18. } while(digit);
  19. return strs.join('');
  20. }
  21. /**
  22. * 用md5加密指定字符串
  23. * @param encStr string 要加密的字符串
  24. * @param pow number 要加密的次数
  25. * @return string 加密后的字符串
  26. */
  27. function hashEncryption(encStr, pow = 1) {
  28. for (let i = 0; i < pow; i++) {
  29. encStr = crypto.createHash('md5').update(encStr, 'utf8').digest('hex');
  30. }
  31. return encStr;
  32. }
  33. module.exports = exports = {
  34. randomChar, hashEncryption
  35. }