Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

124 rindas
3.9 KiB

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