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.
 
 
 
 
 
 

71 lines
2.5 KiB

  1. import crypto from 'crypto';
  2. export class lesspass {
  3. static create_password(master_password, site_information) {
  4. var hash = this._create_hash(master_password, site_information);
  5. var template = this._getTemplate(site_information.password_type);
  6. return this._encode(hash, template);
  7. }
  8. static _create_hash(master_password, {site_name, password_length=12, counter=1}) {
  9. var salt = site_name + counter.toString();
  10. var password = crypto.createHmac('sha256', master_password).update(salt).digest('hex');
  11. return password.substring(0, password_length);
  12. }
  13. static _string2charCodes(text) {
  14. var buffer = new ArrayBuffer(text.length);
  15. var charCodes = new Uint8Array(buffer);
  16. for (let i = 0; i < text.length; i++) {
  17. charCodes[i] = text.charCodeAt(i);
  18. }
  19. return charCodes;
  20. }
  21. static _getTemplate(passwordTypes = ['strong']) {
  22. var passwordTypesInfo = {
  23. lowercase: {value: 'vc', order: 1},
  24. uppercase: {value: 'VC', order: 2},
  25. number: {value: 'n', order: 3},
  26. symbols: {value: 's', order: 4},
  27. strong: {value: 'Cvcvns', order: 5}
  28. };
  29. return passwordTypes
  30. .map(passwordType => passwordTypesInfo[passwordType])
  31. .sort((pt1, pt2) => pt1.order > pt2.order)
  32. .map(passwordType => passwordType.value)
  33. .join('');
  34. }
  35. static _getCharType(template, index) {
  36. return template[index % template.length];
  37. }
  38. static _getPasswordChar(charType, index) {
  39. var passwordsChars = {
  40. V: "AEIOUY",
  41. C: "BCDFGHJKLMNPQRSTVWXZ",
  42. v: "aeiouy",
  43. c: "bcdfghjklmnpqrstvwxz",
  44. A: "AEIOUYBCDFGHJKLMNPQRSTVWXZ",
  45. a: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz",
  46. n: "0123456789",
  47. s: "@&%?,=[]_:-+*$#!'^~;()/.",
  48. x: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz0123456789@&%?,=[]_:-+*$#!'^~;()/."
  49. };
  50. var passwordChar = passwordsChars[charType];
  51. return passwordChar[index % passwordChar.length];
  52. }
  53. static _encode(hash, template) {
  54. var password = '';
  55. this._string2charCodes(hash).map(
  56. (charCode, index) => {
  57. let charType = this._getCharType(template, index);
  58. password += this._getPasswordChar(charType, charCode);
  59. }
  60. );
  61. return password;
  62. }
  63. }