sha256.js.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // Created by xcbosa on 2023-01-31
  3. //
  4. #include "../../processor/processor.h"
  5. #include "../../utils/utils.h"
  6. #include "../../webuiconf.h"
  7. using namespace std;
  8. using namespace xc::processor;
  9. using namespace xc::utils;
  10. using namespace xc::processor::templates;
  11. namespace xc::controller {
  12. static string ControllerPath = "/sha256.js";
  13. static ResponseData *controllerResponse(RequestData request) {
  14. return new TextResponseData(200,
  15. string("/*\n") +
  16. string("* A JavaScript implementation of the SHA256 hash function.\n") +
  17. string("*\n") +
  18. string("* FILE: sha256.js\n") +
  19. string("* VERSION: 0.8\n") +
  20. string("* AUTHOR: Christoph Bichlmeier <informatik@zombiearena.de>\n") +
  21. string("*\n") +
  22. string("* NOTE: This version is not tested thoroughly!\n") +
  23. string("*\n") +
  24. string("* Copyright (c) 2003, Christoph Bichlmeier\n") +
  25. string("* All rights reserved.\n") +
  26. string("*\n") +
  27. string("* Redistribution and use in source and binary forms, with or without\n") +
  28. string("* modification, are permitted provided that the following conditions\n") +
  29. string("* are met:\n") +
  30. string("* 1. Redistributions of source code must retain the above copyright\n") +
  31. string("* notice, this list of conditions and the following disclaimer.\n") +
  32. string("* 2. Redistributions in binary form must reproduce the above copyright\n") +
  33. string("* notice, this list of conditions and the following disclaimer in the\n") +
  34. string("* documentation and/or other materials provided with the distribution.\n") +
  35. string("* 3. Neither the name of the copyright holder nor the names of contributors\n") +
  36. string("* may be used to endorse or promote products derived from this software\n") +
  37. string("* without specific prior written permission.\n") +
  38. string("*\n") +
  39. string("* ======================================================================\n") +
  40. string("*\n") +
  41. string("* THIS SOFTWARE IS PROVIDED BY THE AUTHORS \'\'AS IS\'\' AND ANY EXPRESS\n") +
  42. string("* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n") +
  43. string("* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n") +
  44. string("* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE\n") +
  45. string("* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n") +
  46. string("* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n") +
  47. string("* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR\n") +
  48. string("* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n") +
  49. string("* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n") +
  50. string("* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\n") +
  51. string("* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n") +
  52. string("*/\n") +
  53. string("\n") +
  54. string("/* SHA256 logical functions */\n") +
  55. string("function rotateRight(n,x) {\n") +
  56. string(" return ((x >>> n) | (x << (32 - n)));\n") +
  57. string("}\n") +
  58. string("function choice(x,y,z) {\n") +
  59. string(" return ((x & y) ^ (~x & z));\n") +
  60. string("}\n") +
  61. string("function majority(x,y,z) {\n") +
  62. string(" return ((x & y) ^ (x & z) ^ (y & z));\n") +
  63. string("}\n") +
  64. string("function sha256_Sigma0(x) {\n") +
  65. string(" return (rotateRight(2, x) ^ rotateRight(13, x) ^ rotateRight(22, x));\n") +
  66. string("}\n") +
  67. string("function sha256_Sigma1(x) {\n") +
  68. string(" return (rotateRight(6, x) ^ rotateRight(11, x) ^ rotateRight(25, x));\n") +
  69. string("}\n") +
  70. string("function sha256_sigma0(x) {\n") +
  71. string(" return (rotateRight(7, x) ^ rotateRight(18, x) ^ (x >>> 3));\n") +
  72. string("}\n") +
  73. string("function sha256_sigma1(x) {\n") +
  74. string(" return (rotateRight(17, x) ^ rotateRight(19, x) ^ (x >>> 10));\n") +
  75. string("}\n") +
  76. string("function sha256_expand(W, j) {\n") +
  77. string(" return (W[j&0x0f] += sha256_sigma1(W[(j+14)&0x0f]) + W[(j+9)&0x0f] +\n") +
  78. string(" sha256_sigma0(W[(j+1)&0x0f]));\n") +
  79. string("}\n") +
  80. string("\n") +
  81. string("/* Hash constant words K: */\n") +
  82. string("let K256 = new Array(\n") +
  83. string(" 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\n") +
  84. string(" 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\n") +
  85. string(" 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\n") +
  86. string(" 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\n") +
  87. string(" 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\n") +
  88. string(" 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\n") +
  89. string(" 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\n") +
  90. string(" 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\n") +
  91. string(" 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\n") +
  92. string(" 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\n") +
  93. string(" 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\n") +
  94. string(" 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\n") +
  95. string(" 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\n") +
  96. string(" 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\n") +
  97. string(" 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\n") +
  98. string(" 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2\n") +
  99. string(");\n") +
  100. string("\n") +
  101. string("/* global arrays */\n") +
  102. string("let ihash, count, buffer;\n") +
  103. string("let sha256_hex_digits = \"0123456789abcdef\";\n") +
  104. string("\n") +
  105. string("/* Add 32-bit integers with 16-bit operations (bug in some JS-interpreters:\n") +
  106. string("overflow) */\n") +
  107. string("function safe_add(x, y)\n") +
  108. string("{\n") +
  109. string(" let lsw = (x & 0xffff) + (y & 0xffff);\n") +
  110. string(" let msw = (x >> 16) + (y >> 16) + (lsw >> 16);\n") +
  111. string(" return (msw << 16) | (lsw & 0xffff);\n") +
  112. string("}\n") +
  113. string("\n") +
  114. string("/* Initialise the SHA256 computation */\n") +
  115. string("function sha256_init() {\n") +
  116. string(" ihash = new Array(8);\n") +
  117. string(" count = new Array(2);\n") +
  118. string(" buffer = new Array(64);\n") +
  119. string(" count[0] = count[1] = 0;\n") +
  120. string(" ihash[0] = 0x6a09e667;\n") +
  121. string(" ihash[1] = 0xbb67ae85;\n") +
  122. string(" ihash[2] = 0x3c6ef372;\n") +
  123. string(" ihash[3] = 0xa54ff53a;\n") +
  124. string(" ihash[4] = 0x510e527f;\n") +
  125. string(" ihash[5] = 0x9b05688c;\n") +
  126. string(" ihash[6] = 0x1f83d9ab;\n") +
  127. string(" ihash[7] = 0x5be0cd19;\n") +
  128. string("}\n") +
  129. string("\n") +
  130. string("/* Transform a 512-bit message block */\n") +
  131. string("function sha256_transform() {\n") +
  132. string(" let a, b, c, d, e, f, g, h, T1, T2;\n") +
  133. string(" let W = new Array(16);\n") +
  134. string("\n") +
  135. string(" /* Initialize registers with the previous intermediate value */\n") +
  136. string(" a = ihash[0];\n") +
  137. string(" b = ihash[1];\n") +
  138. string(" c = ihash[2];\n") +
  139. string(" d = ihash[3];\n") +
  140. string(" e = ihash[4];\n") +
  141. string(" f = ihash[5];\n") +
  142. string(" g = ihash[6];\n") +
  143. string(" h = ihash[7];\n") +
  144. string("\n") +
  145. string(" /* make 32-bit words */\n") +
  146. string(" for(let i=0; i<16; i++)\n") +
  147. string(" W[i] = ((buffer[(i<<2)+3]) | (buffer[(i<<2)+2] << 8) | (buffer[(i<<2)+1]\n") +
  148. string(" << 16) | (buffer[i<<2] << 24));\n") +
  149. string("\n") +
  150. string(" for(let j=0; j<64; j++) {\n") +
  151. string(" T1 = h + sha256_Sigma1(e) + choice(e, f, g) + K256[j];\n") +
  152. string(" if(j < 16) T1 += W[j];\n") +
  153. string(" else T1 += sha256_expand(W, j);\n") +
  154. string(" T2 = sha256_Sigma0(a) + majority(a, b, c);\n") +
  155. string(" h = g;\n") +
  156. string(" g = f;\n") +
  157. string(" f = e;\n") +
  158. string(" e = safe_add(d, T1);\n") +
  159. string(" d = c;\n") +
  160. string(" c = b;\n") +
  161. string(" b = a;\n") +
  162. string(" a = safe_add(T1, T2);\n") +
  163. string(" }\n") +
  164. string("\n") +
  165. string(" /* Compute the current intermediate hash value */\n") +
  166. string(" ihash[0] += a;\n") +
  167. string(" ihash[1] += b;\n") +
  168. string(" ihash[2] += c;\n") +
  169. string(" ihash[3] += d;\n") +
  170. string(" ihash[4] += e;\n") +
  171. string(" ihash[5] += f;\n") +
  172. string(" ihash[6] += g;\n") +
  173. string(" ihash[7] += h;\n") +
  174. string("}\n") +
  175. string("\n") +
  176. string("/* Read the next chunk of data and update the SHA256 computation */\n") +
  177. string("function sha256_update(data, inputLen) {\n") +
  178. string(" let i, index, curpos = 0;\n") +
  179. string(" /* Compute number of bytes mod 64 */\n") +
  180. string(" index = ((count[0] >> 3) & 0x3f);\n") +
  181. string(" let remainder = (inputLen & 0x3f);\n") +
  182. string("\n") +
  183. string(" /* Update number of bits */\n") +
  184. string(" if ((count[0] += (inputLen << 3)) < (inputLen << 3)) count[1]++;\n") +
  185. string(" count[1] += (inputLen >> 29);\n") +
  186. string("\n") +
  187. string(" /* Transform as many times as possible */\n") +
  188. string(" for(i=0; i+63<inputLen; i+=64) {\n") +
  189. string(" for(let j=index; j<64; j++)\n") +
  190. string(" buffer[j] = data.charCodeAt(curpos++);\n") +
  191. string(" sha256_transform();\n") +
  192. string(" index = 0;\n") +
  193. string(" }\n") +
  194. string("\n") +
  195. string(" /* Buffer remaining input */\n") +
  196. string(" for(let j=0; j<remainder; j++)\n") +
  197. string(" buffer[j] = data.charCodeAt(curpos++);\n") +
  198. string("}\n") +
  199. string("\n") +
  200. string("/* Finish the computation by operations such as padding */\n") +
  201. string("function sha256_final() {\n") +
  202. string(" let index = ((count[0] >> 3) & 0x3f);\n") +
  203. string(" buffer[index++] = 0x80;\n") +
  204. string(" if(index <= 56) {\n") +
  205. string(" for(let i=index; i<56; i++)\n") +
  206. string(" buffer[i] = 0;\n") +
  207. string(" } else {\n") +
  208. string(" for(let i=index; i<64; i++)\n") +
  209. string(" buffer[i] = 0;\n") +
  210. string(" sha256_transform();\n") +
  211. string(" for(let i=0; i<56; i++)\n") +
  212. string(" buffer[i] = 0;\n") +
  213. string(" }\n") +
  214. string(" buffer[56] = (count[1] >>> 24) & 0xff;\n") +
  215. string(" buffer[57] = (count[1] >>> 16) & 0xff;\n") +
  216. string(" buffer[58] = (count[1] >>> 8) & 0xff;\n") +
  217. string(" buffer[59] = count[1] & 0xff;\n") +
  218. string(" buffer[60] = (count[0] >>> 24) & 0xff;\n") +
  219. string(" buffer[61] = (count[0] >>> 16) & 0xff;\n") +
  220. string(" buffer[62] = (count[0] >>> 8) & 0xff;\n") +
  221. string(" buffer[63] = count[0] & 0xff;\n") +
  222. string(" sha256_transform();\n") +
  223. string("}\n") +
  224. string("\n") +
  225. string("/* Split the internal hash values into an array of bytes */\n") +
  226. string("function sha256_encode_bytes() {\n") +
  227. string(" let j=0;\n") +
  228. string(" let output = new Array(32);\n") +
  229. string(" for(let i=0; i<8; i++) {\n") +
  230. string(" output[j++] = ((ihash[i] >>> 24) & 0xff);\n") +
  231. string(" output[j++] = ((ihash[i] >>> 16) & 0xff);\n") +
  232. string(" output[j++] = ((ihash[i] >>> 8) & 0xff);\n") +
  233. string(" output[j++] = (ihash[i] & 0xff);\n") +
  234. string(" }\n") +
  235. string(" return output;\n") +
  236. string("}\n") +
  237. string("\n") +
  238. string("/* Get the internal hash as a hex string */\n") +
  239. string("function sha256_encode_hex() {\n") +
  240. string(" let output = new String();\n") +
  241. string(" for(let i=0; i<8; i++) {\n") +
  242. string(" for(let j=28; j>=0; j-=4)\n") +
  243. string(" output += sha256_hex_digits.charAt((ihash[i] >>> j) & 0x0f);\n") +
  244. string(" }\n") +
  245. string(" return output;\n") +
  246. string("}\n") +
  247. string("\n") +
  248. string("/* Main function: returns a hex string representing the SHA256 value of the\n") +
  249. string("given data */\n") +
  250. string("function sha256_digest(data) {\n") +
  251. string(" sha256_init();\n") +
  252. string(" sha256_update(data, data.length);\n") +
  253. string(" sha256_final();\n") +
  254. string(" return sha256_encode_hex();\n") +
  255. string("}\n")
  256. , mimeTypeOfFile(ControllerPath));
  257. }
  258. ContentGeneratorDefineS(request.getURLPath() == ControllerPath, controllerResponse(request))
  259. }