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.

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