Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

112 строки
3.5 KiB

  1. 'use strict';
  2. var _crypto = require('crypto');
  3. var _crypto2 = _interopRequireDefault(_crypto);
  4. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  5. module.exports = {
  6. encryptLogin: _encryptLogin,
  7. renderPassword: _renderPassword,
  8. _deriveEncryptedLogin: _deriveEncryptedLogin,
  9. _getPasswordTemplate: _getPasswordTemplate,
  10. _prettyPrint: _prettyPrint,
  11. _string2charCodes: _string2charCodes,
  12. _getCharType: _getCharType,
  13. _getPasswordChar: _getPasswordChar,
  14. _createHmac: _createHmac
  15. };
  16. function _encryptLogin(login, masterPassword) {
  17. return new Promise(function (resolve, reject) {
  18. if (!login || !masterPassword) {
  19. reject('login and master password parameters could not be empty');
  20. }
  21. var iterations = 8192;
  22. var keylen = 32;
  23. _crypto2.default.pbkdf2(masterPassword, login, iterations, keylen, 'sha256', function (error, key) {
  24. if (error) {
  25. reject('error in pbkdf2');
  26. } else {
  27. resolve(key.toString('hex'));
  28. }
  29. });
  30. });
  31. }
  32. function _renderPassword(encryptedLogin, site, passwordOptions) {
  33. return _deriveEncryptedLogin(encryptedLogin, site, passwordOptions).then(function (derivedEncryptedLogin) {
  34. var template = _getPasswordTemplate(passwordOptions);
  35. return _prettyPrint(derivedEncryptedLogin, template);
  36. });
  37. }
  38. function _createHmac(encryptedLogin, salt) {
  39. return new Promise(function (resolve) {
  40. resolve(_crypto2.default.createHmac('sha256', encryptedLogin).update(salt).digest('hex'));
  41. });
  42. }
  43. function _deriveEncryptedLogin(encryptedLogin, site) {
  44. var passwordOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { length: 12, counter: 1 };
  45. var salt = site + passwordOptions.counter.toString();
  46. return _createHmac(encryptedLogin, salt).then(function (derivedHash) {
  47. return derivedHash.substring(0, passwordOptions.length);
  48. });
  49. }
  50. function _getPasswordTemplate(passwordTypes) {
  51. var templates = {
  52. lowercase: 'vc',
  53. uppercase: 'VC',
  54. numbers: 'n',
  55. symbols: 's'
  56. };
  57. var template = '';
  58. for (var templateKey in templates) {
  59. if (passwordTypes.hasOwnProperty(templateKey) && passwordTypes[templateKey]) {
  60. template += templates[templateKey];
  61. }
  62. }
  63. return template;
  64. }
  65. function _prettyPrint(hash, template) {
  66. var password = '';
  67. _string2charCodes(hash).forEach(function (charCode, index) {
  68. var charType = _getCharType(template, index);
  69. password += _getPasswordChar(charType, charCode);
  70. });
  71. return password;
  72. }
  73. function _string2charCodes(text) {
  74. var charCodes = [];
  75. for (var i = 0; i < text.length; i++) {
  76. charCodes.push(text.charCodeAt(i));
  77. }
  78. return charCodes;
  79. }
  80. function _getCharType(template, index) {
  81. return template[index % template.length];
  82. }
  83. function _getPasswordChar(charType, index) {
  84. var passwordsChars = {
  85. V: 'AEIOUY',
  86. C: 'BCDFGHJKLMNPQRSTVWXZ',
  87. v: 'aeiouy',
  88. c: 'bcdfghjklmnpqrstvwxz',
  89. A: 'AEIOUYBCDFGHJKLMNPQRSTVWXZ',
  90. a: 'AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz',
  91. n: '0123456789',
  92. s: '@&%?,=[]_:-+*$#!\'^~;()/.',
  93. x: 'AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz0123456789@&%?,=[]_:-+*$#!\'^~;()/.'
  94. };
  95. var passwordChar = passwordsChars[charType];
  96. return passwordChar[index % passwordChar.length];
  97. }