bluUtil.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //TID号加密解密算法描述
  2. //466fe489d=>70537421
  3. const szKey3 = [0x35, 0x41, 0x32, 0x42, 0x33, 0x43, 0x36, 0x44, 0x39, 0x45,
  4. 0x38, 0x46, 0x37, 0x34, 0x31, 0x30
  5. ];
  6. //解密 ,参数16进制
  7. const encrypt = (number) => {
  8. number = number.toUpperCase()
  9. let out_number = '';
  10. if (!number) return;
  11. let len = number.length;
  12. if (len > 16) return;
  13. for (let i = 0; i < len; i++) {
  14. for (let j = 0; j < 16; j++) {
  15. if (number.charAt(i).charCodeAt() == szKey3[j]) //字符转ascii码
  16. out_number += String.fromCharCode(0x2A + j);
  17. else
  18. continue;
  19. }
  20. }
  21. return out_number
  22. }
  23. // ArrayBuffer转16进度字符串示例
  24. const ab2hex = (buffer) => {
  25. var hexArr = Array.prototype.map.call(
  26. new Uint8Array(buffer),
  27. function (bit) {
  28. return ('00' + bit.toString(16)).slice(-2)
  29. }
  30. )
  31. return hexArr.join('');
  32. }
  33. //字符串平均切割成数组
  34. const strAverage2Arr = (str, no) => {
  35. let arr = [];
  36. for (let i = 0; i < str.length; i += no) {
  37. arr.push(str.slice(i, i + 2));
  38. }
  39. return arr;
  40. }
  41. //数组的每个元素加前缀
  42. const addFlagBeforeArr = (arr) => {
  43. return Array.prototype.map.call(arr, function (value) {
  44. return `0x${value}`;
  45. })
  46. }
  47. //字符转ArrayBuffer
  48. const hexStringToArrayBuffer = (str) => {
  49. // 将16进制转化为ArrayBuffer
  50. return new Uint8Array(str.match(/[\da-f]{2}/gi).map(function (h) {
  51. return parseInt(h, 16)
  52. })).buffer
  53. }
  54. module.exports = {
  55. encrypt: encrypt,
  56. ab2hex: ab2hex,
  57. strAverage2Arr: strAverage2Arr,
  58. addFlagBeforeArr: addFlagBeforeArr,
  59. hexStringToArrayBuffer: hexStringToArrayBuffer
  60. }