You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SAS.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* eslint-disable no-bitwise */
  2. /* eslint-disable no-mixed-operators */
  3. /**
  4. * Generates a SAS composed of decimal numbers.
  5. * Borrowed from the Matrix JS SDK.
  6. *
  7. * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
  8. * @returns Array<number>
  9. */
  10. function generateDecimalSas(sasBytes) {
  11. /**
  12. * +--------+--------+--------+--------+--------+
  13. * | Byte 0 | Byte 1 | Byte 2 | Byte 3 | Byte 4 |
  14. * +--------+--------+--------+--------+--------+
  15. * bits: 87654321 87654321 87654321 87654321 87654321
  16. * \____________/\_____________/\____________/
  17. * 1st number 2nd number 3rd number
  18. */
  19. return [
  20. (sasBytes[0] << 5 | sasBytes[1] >> 3) + 1000,
  21. ((sasBytes[1] & 0x7) << 10 | sasBytes[2] << 2 | sasBytes[3] >> 6) + 1000,
  22. ((sasBytes[3] & 0x3f) << 7 | sasBytes[4] >> 1) + 1000
  23. ];
  24. }
  25. const emojiMapping = [
  26. [ '๐Ÿถ', 'dog' ],
  27. [ '๐Ÿฑ', 'cat' ],
  28. [ '๐Ÿฆ', 'lion' ],
  29. [ '๐ŸŽ', 'horse' ],
  30. [ '๐Ÿฆ„', 'unicorn' ],
  31. [ '๐Ÿท', 'pig' ],
  32. [ '๐Ÿ˜', 'elephant' ],
  33. [ '๐Ÿฐ', 'rabbit' ],
  34. [ '๐Ÿผ', 'panda' ],
  35. [ '๐Ÿ“', 'rooster' ],
  36. [ '๐Ÿง', 'penguin' ],
  37. [ '๐Ÿข', 'turtle' ],
  38. [ '๐ŸŸ', 'fish' ],
  39. [ '๐Ÿ™', 'octopus' ],
  40. [ '๐Ÿฆ‹', 'butterfly' ],
  41. [ '๐ŸŒท', 'flower' ],
  42. [ '๐ŸŒณ', 'tree' ],
  43. [ '๐ŸŒต', 'cactus' ],
  44. [ '๐Ÿ„', 'mushroom' ],
  45. [ '๐ŸŒ', 'globe' ],
  46. [ '๐ŸŒ™', 'moon' ],
  47. [ 'โ˜๏ธ', 'cloud' ],
  48. [ '๐Ÿ”ฅ', 'fire' ],
  49. [ '๐ŸŒ', 'banana' ],
  50. [ '๐ŸŽ', 'apple' ],
  51. [ '๐Ÿ“', 'strawberry' ],
  52. [ '๐ŸŒฝ', 'corn' ],
  53. [ '๐Ÿ•', 'pizza' ],
  54. [ '๐ŸŽ‚', 'cake' ],
  55. [ 'โค๏ธ', 'heart' ],
  56. [ '๐Ÿ™‚', 'smiley' ],
  57. [ '๐Ÿค–', 'robot' ],
  58. [ '๐ŸŽฉ', 'hat' ],
  59. [ '๐Ÿ‘“', 'glasses' ],
  60. [ '๐Ÿ”ง', 'spanner' ],
  61. [ '๐ŸŽ…', 'santa' ],
  62. [ '๐Ÿ‘', 'thumbs up' ],
  63. [ 'โ˜‚๏ธ', 'umbrella' ],
  64. [ 'โŒ›', 'hourglass' ],
  65. [ 'โฐ', 'clock' ],
  66. [ '๐ŸŽ', 'gift' ],
  67. [ '๐Ÿ’ก', 'light bulb' ],
  68. [ '๐Ÿ“•', 'book' ],
  69. [ 'โœ๏ธ', 'pencil' ],
  70. [ '๐Ÿ“Ž', 'paperclip' ],
  71. [ 'โœ‚๏ธ', 'scissors' ],
  72. [ '๐Ÿ”’', 'lock' ],
  73. [ '๐Ÿ”‘', 'key' ],
  74. [ '๐Ÿ”จ', 'hammer' ],
  75. [ 'โ˜Ž๏ธ', 'telephone' ],
  76. [ '๐Ÿ', 'flag' ],
  77. [ '๐Ÿš‚', 'train' ],
  78. [ '๐Ÿšฒ', 'bicycle' ],
  79. [ 'โœˆ๏ธ', 'aeroplane' ],
  80. [ '๐Ÿš€', 'rocket' ],
  81. [ '๐Ÿ†', 'trophy' ],
  82. [ 'โšฝ', 'ball' ],
  83. [ '๐ŸŽธ', 'guitar' ],
  84. [ '๐ŸŽบ', 'trumpet' ],
  85. [ '๐Ÿ””', 'bell' ],
  86. [ 'โš“๏ธ', 'anchor' ],
  87. [ '๐ŸŽง', 'headphones' ],
  88. [ '๐Ÿ“', 'folder' ],
  89. [ '๐Ÿ“Œ', 'pin' ]
  90. ];
  91. /**
  92. * Generates a SAS composed of defimal numbers.
  93. * Borrowed from the Matrix JS SDK.
  94. *
  95. * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
  96. * @returns Array<number>
  97. */
  98. function generateEmojiSas(sasBytes) {
  99. // Just like base64.
  100. const emojis = [
  101. sasBytes[0] >> 2,
  102. (sasBytes[0] & 0x3) << 4 | sasBytes[1] >> 4,
  103. (sasBytes[1] & 0xf) << 2 | sasBytes[2] >> 6,
  104. sasBytes[2] & 0x3f,
  105. sasBytes[3] >> 2,
  106. (sasBytes[3] & 0x3) << 4 | sasBytes[4] >> 4,
  107. (sasBytes[4] & 0xf) << 2 | sasBytes[5] >> 6
  108. ];
  109. return emojis.map(num => emojiMapping[num]);
  110. }
  111. const sasGenerators = {
  112. decimal: generateDecimalSas,
  113. emoji: generateEmojiSas
  114. };
  115. /**
  116. * Generates multiple SAS for the given bytes.
  117. *
  118. * @param {Uint8Array} sasBytes - The bytes from sas.generate_bytes.
  119. * @returns {object}
  120. */
  121. export function generateSas(sasBytes) {
  122. const sas = {};
  123. for (const method in sasGenerators) {
  124. if (sasGenerators.hasOwnProperty(method)) {
  125. sas[method] = sasGenerators[method](sasBytes);
  126. }
  127. }
  128. return sas;
  129. }