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.
 
 
 
 
 
 

84 lines
2.9 KiB

  1. import crypto from 'crypto';
  2. export default class lesspass {
  3. static createPassword(masterPassword, entry) {
  4. var hash = lesspass._createHash(masterPassword, entry);
  5. var template = lesspass._getTemplate(entry.password.settings);
  6. return lesspass._encode(hash, template);
  7. }
  8. static createMasterPassword(email, password) {
  9. return new Promise((resolve, reject) => {
  10. var iterations = 8192;
  11. var keylen = 32;
  12. crypto.pbkdf2(password, email, iterations, keylen, 'sha256', function (error, key) {
  13. if (error) {
  14. reject('error in pbkdf2');
  15. } else {
  16. resolve(key.toString('hex'));
  17. }
  18. });
  19. });
  20. }
  21. static _createHash(masterPassword, {site, password={length: 12}, counter=1}) {
  22. var salt = site + counter.toString();
  23. var hash = crypto.createHmac('sha256', masterPassword).update(salt).digest('hex');
  24. return hash.substring(0, password.length);
  25. }
  26. static _getTemplate(passwordTypes = ['strong']) {
  27. var passwordTypesInfo = {
  28. lowercase: {value: 'vc', order: 1},
  29. uppercase: {value: 'VC', order: 2},
  30. numbers: {value: 'n', order: 3},
  31. symbols: {value: 's', order: 4},
  32. strong: {value: 'Cvcvns', order: 5}
  33. };
  34. return passwordTypes
  35. .map(passwordType => passwordTypesInfo[passwordType])
  36. .sort((passwordType1, passwordType2) => passwordType1.order > passwordType2.order)
  37. .map(passwordType => passwordType.value)
  38. .join('');
  39. }
  40. static _string2charCodes(text) {
  41. var charCodes = [];
  42. for (let i = 0; i < text.length; i++) {
  43. charCodes.push(text.charCodeAt(i));
  44. }
  45. return charCodes;
  46. }
  47. static _getCharType(template, index) {
  48. return template[index % template.length];
  49. }
  50. static _getPasswordChar(charType, index) {
  51. var passwordsChars = {
  52. V: "AEIOUY",
  53. C: "BCDFGHJKLMNPQRSTVWXZ",
  54. v: "aeiouy",
  55. c: "bcdfghjklmnpqrstvwxz",
  56. A: "AEIOUYBCDFGHJKLMNPQRSTVWXZ",
  57. a: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz",
  58. n: "0123456789",
  59. s: "@&%?,=[]_:-+*$#!'^~;()/.",
  60. x: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz0123456789@&%?,=[]_:-+*$#!'^~;()/."
  61. };
  62. var passwordChar = passwordsChars[charType];
  63. return passwordChar[index % passwordChar.length];
  64. }
  65. static _encode(hash, template) {
  66. var password = '';
  67. this._string2charCodes(hash).map(
  68. (charCode, index) => {
  69. let charType = this._getCharType(template, index);
  70. password += this._getPasswordChar(charType, charCode);
  71. }
  72. );
  73. return password;
  74. }
  75. }