Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. UserSchema.pre('save', function(next) {
  2. var user = this;
  3.  
  4. var salt = crypto.randomBytes(128).toString('base64');
  5. crypto.pbkdf2(user.password, salt, 10000, 512, function(err, derivedKey) {
  6. user.password = derivedKey;
  7. next();
  8. });
  9. });
  10.  
  11. UserSchema.methods.validPassword = function(password) {
  12. // need to salt and hash this password I think to compare
  13. // how to I get the salt?
  14. }
  15.  
  16. function hashPassword(password) {
  17. var salt = crypto.randomBytes(128).toString('base64');
  18. var iterations = 10000;
  19. var hash = pbkdf2(password, salt, iterations);
  20.  
  21. return {
  22. salt: salt,
  23. hash: hash,
  24. iterations: iterations
  25. };
  26. }
  27.  
  28. function isPasswordCorrect(savedHash, savedSalt, savedIterations, passwordAttempt) {
  29. return savedHash == pbkdf2(passwordAttempt, savedSalt, savedIterations);
  30. }
  31.  
  32. var bcrypt = require('bcrypt');
  33. var salt = bcrypt.genSaltSync(10);
  34. var hash = bcrypt.hashSync("B4c0//", salt);
  35. // Store hash in your password DB.
  36.  
  37. // Load hash from your password DB.
  38. bcrypt.compareSync("B4c0//", hash); // true
  39. bcrypt.compareSync("not_bacon", hash); // false
  40.  
  41. import * as crypto from 'crypto';
  42.  
  43. const PASSWORD_LENGTH = 256;
  44. const SALT_LENGTH = 64;
  45. const ITERATIONS = 10000;
  46. const DIGEST = 'sha256';
  47. const BYTE_TO_STRING_ENCODING = 'hex'; // this could be base64, for instance
  48.  
  49. /**
  50. * The information about the password that is stored in the database
  51. */
  52. interface PersistedPassword {
  53. salt: string;
  54. hash: string;
  55. iterations: number;
  56. }
  57.  
  58. /**
  59. * Generates a PersistedPassword given the password provided by the user. This should be called when creating a user
  60. * or redefining the password
  61. */
  62. export async function generateHashPassword(password: string): Promise<PersistedPassword> {
  63. return new Promise<PersistedPassword>((accept, reject) => {
  64. const salt = crypto.randomBytes(SALT_LENGTH).toString(BYTE_TO_STRING_ENCODING);
  65. crypto.pbkdf2(password, salt, ITERATIONS, PASSWORD_LENGTH, DIGEST, (error, hash) => {
  66. if (error) {
  67. reject(error);
  68. } else {
  69. accept({
  70. salt,
  71. hash: hash.toString(BYTE_TO_STRING_ENCODING),
  72. iterations: ITERATIONS,
  73. });
  74. }
  75. });
  76. });
  77. }
  78.  
  79. /**
  80. * Verifies the attempted password against the password information saved in the database. This should be called when
  81. * the user tries to log in.
  82. */
  83. export async function verifyPassword(persistedPassword: PersistedPassword, passwordAttempt: string): Promise<boolean> {
  84. return new Promise<boolean>((accept, reject) => {
  85. crypto.pbkdf2(passwordAttempt, persistedPassword.salt, persistedPassword.iterations, PASSWORD_LENGTH, DIGEST, (error, hash) => {
  86. if (error) {
  87. reject(error);
  88. } else {
  89. accept(persistedPassword.hash === hash.toString(BYTE_TO_STRING_ENCODING));
  90. }
  91. });
  92. });
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement