bluUtil.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. console.log(number)
  9. number = number.toUpperCase()
  10. console.log(number)
  11. let out_number = '';
  12. if (!number) return;
  13. let len = number.length;
  14. console.log(len)
  15. if (len > 16) return;
  16. for (let i = 0; i < len; i++) {
  17. for (let j = 0; j < 16; j++) {
  18. if (number.charAt(i).charCodeAt() == szKey3[j]) //字符转ascii码
  19. out_number += String.fromCharCode(0x2A + j);
  20. else
  21. continue;
  22. }
  23. }
  24. console.log(out_number)
  25. return out_number
  26. }
  27. // ArrayBuffer转16进度字符串示例
  28. const ab2hex = (buffer) => {
  29. var hexArr = Array.prototype.map.call(
  30. new Uint8Array(buffer),
  31. function (bit) {
  32. return ('00' + bit.toString(16)).slice(-2)
  33. }
  34. )
  35. return hexArr.join('');
  36. }
  37. //字符串平均切割成数组
  38. const strAverage2Arr = (str, no) => {
  39. let arr = [];
  40. for (let i = 0; i < str.length; i += no) {
  41. arr.push(str.slice(i, i + 2));
  42. }
  43. return arr;
  44. }
  45. //数组的每个元素加前缀
  46. const addFlagBeforeArr = (arr) => {
  47. return Array.prototype.map.call(arr, function (value) {
  48. return `0x${value}`;
  49. })
  50. }
  51. //字符转ArrayBuffer
  52. const hexStringToArrayBuffer = (str) => {
  53. // 将16进制转化为ArrayBuffer
  54. return new Uint8Array(str.match(/[\da-f]{2}/gi).map(function (h) {
  55. return parseInt(h, 16)
  56. })).buffer
  57. }
  58. const CRC = (dataBuf) =>{
  59. var temp = 0;
  60. var crc = 0xffff;
  61. for (var i = 0; i < dataBuf.length; i++) {
  62. crc ^= parseInt(dataBuf[i].toString());
  63. for (var j = 0; j < 8; j++) {
  64. temp = 1 & crc;
  65. crc >>= 1;
  66. if (temp == 1) {
  67. crc ^= 0xa001;
  68. }
  69. }
  70. }
  71. crc ^= 0xffff;
  72. crc = ('0000' + crc.toString(16)).substr(-4);
  73. return crc;
  74. }
  75. module.exports = {
  76. encrypt: encrypt,
  77. ab2hex: ab2hex,
  78. strAverage2Arr: strAverage2Arr,
  79. addFlagBeforeArr: addFlagBeforeArr,
  80. hexStringToArrayBuffer: hexStringToArrayBuffer,
  81. CRC:CRC
  82. }