Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. const moment = require('moment')
  2. const base64 = require('rfc4648').base64
  3. const NodeRSA = require('node-rsa')
  4. const { crc32 } = require('crc')
  5. const rjust = require('string-just').rjust
  6. const chunk = require('lodash.chunk')
  7. const forEach = require('lodash.foreach')
  8. const times = require('lodash.times')
  9. //const Buffer = require('Buffer')
  10.  
  11. function sign_request(request, adp_token, private_key) {
  12. path = request.baseUrl + request.url
  13. query = request.qs
  14. url = path
  15. if (query) url += `?${query}`
  16. request.headers = request.headers || {}
  17.  
  18. each(_sign_request(url, request.method, request.body, adp_token, private_key), function(key, value) {
  19. request.headers[key] = value
  20. })
  21.  
  22. return request
  23. }
  24.  
  25. function _sign_request(url, method, body, adp_token, private_key, date) {
  26. date = date || moment().utc().format('YYYY-MM-DDTHH:mm:ssZ')
  27. data = `${method}\n${url}\n${date}\n`
  28. if (body) data += body
  29. data += "\n"
  30. data += adp_token
  31.  
  32. // digest = OpenSSL::Digest.new("sha256")
  33. signed = private_key.sign(data, 'base64')
  34. signature = `${signed}:${date}`
  35.  
  36. return {
  37. "x-adp-token": adp_token,
  38. "x-adp-alg": "SHA256withRSA:1.0",
  39. "x-adp-signature": signature
  40. }
  41. }
  42.  
  43. function _toHex(int) {
  44. //return Buffer.from(str, 'utf8').toString('hex');
  45. return int.toString(16)
  46. }
  47.  
  48. function _ToInteger(x) {
  49. x = Number(x)
  50. return x < 0 ? Math.ceil(x) : Math.floor(x)
  51. }
  52. function _modulo(a, b) {
  53. return a - Math.floor(a/b)*b
  54. }
  55. function _ToUint32(x) {
  56. return _modulo(_ToInteger(x), Math.pow(2, 32))
  57. }
  58. function _ToUint16(x) {
  59. return _modulo(_ToInteger(x), Math.pow(2, 8))
  60. }
  61. function _ToUint8(x) {
  62. return _modulo(_ToInteger(x), Math.pow(2, 8))
  63. }
  64.  
  65. function _ord(char) {
  66. return char ? char.charCodeAt(0) : (0 & 255)
  67. }
  68.  
  69. function encrypt_metadata(metadata) {
  70. const crc = _toHex(crc32(JSON.stringify(metadata,null,0).toString(16)))
  71. const checksum = rjust(crc, 8, '0').toUpperCase() // Ensure it is always 8 chars
  72. const object = `${checksum}#${JSON.stringify(metadata)}`
  73.  
  74. const rounds = Math.ceil(object.length / 4);
  75. let temp2 = []
  76.  
  77. for (let i = 0; i < rounds; i++) {
  78. temp2[i] = (object.charCodeAt(i * 4) & 255) +
  79. ((object.charCodeAt(i * 4 + 1) & 255) << 8) +
  80. ((object.charCodeAt(i * 4 + 2) & 255) << 16) +
  81. ((object.charCodeAt(i * 4 + 3) & 255) << 24);
  82. }
  83.  
  84. const wrap = 2654435769;
  85. const temp3 = [1888420705, 2576816180, 2347232058, 874813317];
  86.  
  87. let minor_rounds = Math.floor(6 + (52 / rounds));
  88. let first = temp2[0];
  89. let last = temp2[rounds - 1];
  90.  
  91. let inner_roll = 0;
  92. while (minor_rounds-- > 0) {
  93. inner_roll += wrap;
  94. const inner_variable = inner_roll >>> 2 & 3;
  95.  
  96. for (var i = 0; i < rounds; i++) {
  97. first = temp2[(i + 1) % rounds];
  98. last = temp2[i] +=
  99. (last >>> 5 ^ first << 2) + (first >>> 3 ^ last << 4) ^
  100. (inner_roll ^ first) + (temp3[i & 3 ^ inner_variable] ^ last);
  101. }
  102. }
  103.  
  104. let final_round = [];
  105.  
  106. for (let i = 0; i < rounds; i++) {
  107. final_round[i] = String.fromCharCode(
  108. temp2[i] & 255,
  109. temp2[i] >>> 8 & 255,
  110. temp2[i] >>> 16 & 255,
  111. temp2[i] >>> 24 & 255
  112. );
  113. }
  114.  
  115. final_round = final_round.join("")
  116.  
  117. const base64_encoded = base64encode(final_round);
  118. const final = 'ECdITeCs:' + base64_encoded;
  119. return final
  120. }
  121.  
  122. function base64decode(data) {
  123. const buf = Buffer.from(base64.parse(data)).toString('utf8')
  124. return buf
  125. }
  126.  
  127. function base64encode(data) {
  128. let out = []
  129. for (let i = 0; i < data.length; ++i) {
  130. out[i] = data.charCodeAt(i)
  131. }
  132. return base64.stringify(out)
  133. }
  134.  
  135. function decrypt_metadata(encrypted_metadata) {
  136. // Rmove the key
  137. encrypted_metadata = encrypted_metadata.replace(/ECdITeCs:/, "")
  138. const final_round = base64decode(encrypted_metadata)
  139.  
  140. const chunks = chunk(final_round, 4)
  141. let temp2 = []
  142. for (let i = 0; i < chunks.length; i++) {
  143. let chars = chunks[i]
  144. temp2[i] = (chars[0]) +
  145. (chars[1] << 8) +
  146. (chars[2] << 16) +
  147. (chars[3] << 24)
  148. }
  149.  
  150. const rounds = temp2.length
  151. let minor_rounds = Math.floor(6 + (52 / rounds))
  152.  
  153. const wrap_constant = 2654435769
  154. const constants = [1888420705, 2576816180, 2347232058, 874813317]
  155.  
  156. let inner_roll = 0
  157. let inner_variable = 0
  158.  
  159. times((minor_rounds + 1), function() {
  160. inner_roll += wrap_constant
  161. inner_variable = inner_roll >> 2 & 3
  162. })
  163.  
  164. while (minor_rounds > 0) {
  165. minor_rounds -= 1
  166.  
  167. inner_roll -= wrap_constant
  168. inner_variable = inner_roll >> 2 & 3
  169.  
  170. for (let i = 0; i < rounds; i++) {
  171. i = rounds - i - 1
  172.  
  173. first = temp2[(i + 1) % rounds]
  174. last = temp2[(i - 1) % rounds]
  175.  
  176. last = temp2[i] -=
  177. ((last >> 5 ^ first << 2) + (first >> 3 ^ last << 4) ^
  178. (inner_roll ^ first) + (constants[i & 3 ^ inner_variable] ^ last))
  179. }
  180. }
  181.  
  182. let object = ""
  183. for (let i = 0; i < temp2.length; i++) {
  184. let block = temp2[i]
  185. forEach([0, 8, 16, 24], function(align) {
  186. if ((block >> align & 255) > 0) {
  187. const charCode = (block >> align & 255)
  188. object += String.fromCharCode(charCode)
  189. }
  190. })
  191. }
  192. console.log(object)
  193.  
  194. return object
  195. }
  196.  
  197. module.exports = {
  198. sign_request,
  199. encrypt_metadata,
  200. decrypt_metadata,
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement