Ohm-Management - Projektarbeit B-ME
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.

crypto.js 940B

12345678910111213141516171819202122232425262728293031
  1. // Original file created by Prof.Dr. Matthias Hopf
  2. /*
  3. * Crypto routines for Authorization
  4. */
  5. const crypto = require ("crypto");
  6. const defaultHash = "sha256";
  7. const defaultSaltLen = 16; // More (e.g. 256) for extra paranoia
  8. const mod = {
  9. encodePwd: function (entry, pwd) {
  10. return crypto.createHash (entry.hash) .update (entry.salt + ":" + pwd, 'utf8') .digest ('base64');
  11. },
  12. checkLocalAuth: function (entry, pwd) {
  13. if (!entry || !entry._id || !entry.hash || !entry.salt || !entry.hash || !entry.pwd ||
  14. !pwd || pwd === '')
  15. return false;
  16. return mod.encodePwd (entry, pwd) === entry.pwd;
  17. },
  18. fillLocalAuth: function (entry, pwd) {
  19. if (!entry.hash)
  20. entry.hash = defaultHash;
  21. entry.salt = crypto.randomBytes (defaultSaltLen) .toString('base64');
  22. entry.pwd = mod.encodePwd (entry, pwd);
  23. },
  24. }
  25. module.exports = mod;