index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var block = 1;
  2. var random = 0;
  3. var data = "Hello World";
  4. var int = 3;
  5. var hash = "NULL";
  6. function repeat(src, n) {
  7. return new Array(n + 1).join(src);
  8. }
  9. function writeLocal() {
  10. json = `{"block": "${block}", "random": "${random}", "data": "${data}", "int": "${int}", "hash": "${hash}"}`;
  11. localStorage.calc_hash = json;
  12. document.getElementById("block").value = block;
  13. document.getElementById("random").value = random;
  14. document.getElementById("data").value = data;
  15. document.getElementById("int").value = int;
  16. document.getElementById("hash").value = hash;
  17. }
  18. function readLocal() {
  19. json = localStorage.calc_hash;
  20. if (json != null) {
  21. json = JSON.parse(json);
  22. } else {
  23. json = null;
  24. }
  25. console.log(json);
  26. return json;
  27. }
  28. function calc() {
  29. block = document.getElementById("block").value;
  30. random = document.getElementById("random").value;
  31. data = document.getElementById("data").value;
  32. var input = `block=${block};random=${random};data=${data}`;
  33. var output = CryptoJS.SHA256(input);
  34. hash = output.toString(CryptoJS.enc.Hex).toUpperCase();
  35. document.getElementById("block").value = block;
  36. document.getElementById("random").value = random;
  37. document.getElementById("data").value = data;
  38. document.getElementById("hash").value = hash;
  39. }
  40. function start() {
  41. block = document.getElementById("block").value;
  42. random = document.getElementById("random").value;
  43. data = document.getElementById("data").value;
  44. int = Number(document.getElementById("int").value);
  45. if (int < 2) {
  46. alert("取0的个数太少了!");
  47. int = 2;
  48. document.getElementById("int").value = int;
  49. return;
  50. } else if (int > 9) {
  51. alert("取0的个数太多了!\n你想让你的计算机燃烧吗??");
  52. int = 4;
  53. document.getElementById("int").value = int;
  54. return;
  55. }
  56. random = 0;
  57. var now, target, input, output;
  58. target = repeat("0", int);
  59. document.getElementById("random").value = "0";
  60. document.getElementById("hash").value = "计算中...\n计算量过大页面会卡死...";
  61. setTimeout(() => {
  62. while (now != target) {
  63. input = `block=${block};random=${random};data=${data}`;
  64. output = CryptoJS.SHA256(input);
  65. hash = output.toString(CryptoJS.enc.Hex);
  66. now = hash.slice(0, int);
  67. console.log(now);
  68. random += 1;
  69. }
  70. random -= 1;
  71. hash = hash.toUpperCase();
  72. writeLocal();
  73. }, 500);
  74. }
  75. function onLoad() {
  76. localData = readLocal();
  77. if (localData != null) {
  78. block = localData.block;
  79. random = localData.random;
  80. data = localData.data;
  81. int = localData.int;
  82. hash = localData.hash;
  83. document.getElementById("block").value = block;
  84. document.getElementById("random").value = random;
  85. document.getElementById("data").value = data;
  86. document.getElementById("int").value = int;
  87. document.getElementById("hash").value = hash;
  88. } else {
  89. writeLocal();
  90. calc();
  91. }
  92. }