SALT and HASH password in nodejs w/ crypto

前端 未结 7 2236
野趣味
野趣味 2020-12-07 18:43

I am trying to figure out how to salt and hash a password in nodejs using the crypto module. I am able to create the hashed password doing this:

UserSchema.         


        
7条回答
  •  长情又很酷
    2020-12-07 19:38

    This is a modified version of @Matthews answer, using TypeScript

    import * as crypto from 'crypto';
    
    const PASSWORD_LENGTH = 256;
    const SALT_LENGTH = 64;
    const ITERATIONS = 10000;
    const DIGEST = 'sha256';
    const BYTE_TO_STRING_ENCODING = 'hex'; // this could be base64, for instance
    
    /**
     * The information about the password that is stored in the database
     */
    interface PersistedPassword {
        salt: string;
        hash: string;
        iterations: number;
    }
    
    /**
     * Generates a PersistedPassword given the password provided by the user. This should be called when creating a user
     * or redefining the password
     */
    export async function generateHashPassword(password: string): Promise {
        return new Promise((accept, reject) => {
            const salt = crypto.randomBytes(SALT_LENGTH).toString(BYTE_TO_STRING_ENCODING);
            crypto.pbkdf2(password, salt, ITERATIONS, PASSWORD_LENGTH, DIGEST, (error, hash) => {
                if (error) {
                    reject(error);
                } else {
                    accept({
                        salt,
                        hash: hash.toString(BYTE_TO_STRING_ENCODING),
                        iterations: ITERATIONS,
                    });
                }
            });
        });
    }
    
    /**
     * Verifies the attempted password against the password information saved in the database. This should be called when
     * the user tries to log in.
     */
    export async function verifyPassword(persistedPassword: PersistedPassword, passwordAttempt: string): Promise {
        return new Promise((accept, reject) => {
            crypto.pbkdf2(passwordAttempt, persistedPassword.salt, persistedPassword.iterations, PASSWORD_LENGTH, DIGEST, (error, hash) => {
                if (error) {
                    reject(error);
                } else {
                    accept(persistedPassword.hash === hash.toString(BYTE_TO_STRING_ENCODING));
                }
            });
        });
    }
    

提交回复
热议问题