Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
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.

index.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. // Crypto module import.
  3. const crypto = require('crypto');
  4. // Hash generation string.
  5. const itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  6. // To 64 bit version.
  7. function to64(index, count) {
  8. let result = '';
  9. while (--count >= 0) { // Result char count.
  10. result += itoa64[index & 63]; // Get corresponding char.
  11. index = index >> 6; // Move to next one.
  12. }
  13. return result;
  14. }
  15. // Returns salt.
  16. function getSalt(inputSalt) {
  17. let salt = '';
  18. if (inputSalt) {
  19. // Remove $apr1$ token and extract salt.
  20. salt = inputSalt.split('$')[2];
  21. } else {
  22. while(salt.length < 8) { // Random 8 chars.
  23. let rchIndex = Math.floor((Math.random() * 64));
  24. salt += itoa64[rchIndex];
  25. }
  26. }
  27. return salt;
  28. }
  29. // Returns password.
  30. function getPassword(final) {
  31. // Encrypted pass.
  32. let epass = '';
  33. epass += to64((final.charCodeAt(0) << 16) | (final.charCodeAt(6) << 8) | final.charCodeAt(12), 4);
  34. epass += to64((final.charCodeAt(1) << 16) | (final.charCodeAt(7) << 8) | final.charCodeAt(13), 4);
  35. epass += to64((final.charCodeAt(2) << 16) | (final.charCodeAt(8) << 8) | final.charCodeAt(14), 4);
  36. epass += to64((final.charCodeAt(3) << 16) | (final.charCodeAt(9) << 8) | final.charCodeAt(15), 4);
  37. epass += to64((final.charCodeAt(4) << 16) | (final.charCodeAt(10) << 8) | final.charCodeAt(5), 4);
  38. epass += to64(final.charCodeAt(11), 2);
  39. return epass;
  40. }
  41. // Exporting old style.
  42. module.exports = (password, salt) => {
  43. let magic = '';
  44. if (salt && salt.split('$')[1] === '1') {
  45. magic = '$1$';
  46. } else {
  47. magic = '$apr1$';
  48. }
  49. salt = getSalt(salt);
  50. let ctx = password + magic + salt;
  51. let final = crypto.createHash('md5').update(password + salt + password, 'ascii').digest('binary');
  52. for (let pl = password.length; pl > 0; pl -= 16) {
  53. ctx += final.substr(0, (pl > 16) ? 16 : pl);
  54. }
  55. for (let i = password.length; i; i >>= 1) {
  56. if (i % 2) {
  57. ctx += String.fromCharCode(0);
  58. } else {
  59. ctx += password.charAt(0);
  60. }
  61. }
  62. final = crypto.createHash('md5').update(ctx, 'ascii').digest('binary');
  63. // 1000 loop.
  64. for (let i = 0; i < 1000; ++i) {
  65. // Weird stuff.
  66. let ctxl = '';
  67. if (i % 2) {
  68. ctxl += password;
  69. } else {
  70. ctxl += final.substr(0, 16);
  71. }
  72. if (i % 3) {
  73. ctxl += salt;
  74. }
  75. if (i % 7) {
  76. ctxl += password;
  77. }
  78. if (i % 2) {
  79. ctxl += final.substr(0, 16);
  80. } else {
  81. ctxl += password;
  82. }
  83. // Final assignment after each loop.
  84. final = crypto.createHash('md5').update(ctxl, 'ascii').digest('binary');
  85. }
  86. return magic + salt + '$' + getPassword(final);
  87. };