简体   繁体   中英

Password hashing with bcrypt and object assigning

I need to swap passwords that are in the clear to ones that are hashed. I am using bcryptjs to help me with this.

I have tried assigning the passwords that are in the clear the hashed passwords but I am getting an error on my bash.

My code that I am trying to make work:

const bcrypt = require('bcryptjs');
const students = require('./students1.json');
const fs = require('fs');

let secureUsers = [];
for (let student of students) {
    let salt = bcrypt.genSaltSync(10);
    let passHash = bcrypt.hashSync(student.password, salt);
    Object.assign(student.password, passHash);
    secureUsers.push(secStudent);
}
fs.writeFileSync('secStudents.json', JSON.stringify(secureUsers, null, 2));
console.log('wrote file!');

The error that I am getting:

$ node bcryptExample.js
C:\Users\mziad\assignment-mziadeh1\servers\bcryptExample.js:13
    Object.assign(student.password, passHash);
           ^

TypeError: Cannot assign to read only property '0' of object '[object String]'
    at Function.assign (<anonymous>)
    at Object.<anonymous> (C:\Users\mziad\assignment-mziadeh1\servers\bcryptExample.js:13:12)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

An example of what I want to hash:

{
    "netid": "ky4531",
    "firstName": "Frankie",
    "lastName": "Griffith",
    "email": "erasement1803@outlook.com",
    "password": "t'|x/)$g"
  },
  {
    "netid": "tw0199",
    "firstName": "Julietta",
    "lastName": "Vargas",
    "email": "ezekiel1960@outlook.com",
    "password": "Rc*pKe$w"
  }

I need the passwords to be swapped with hash code hence why I am trying to assign it. But I am getting an error that I don't understand and I cannot really spot any problems with my code right now.

It seems like you misunderstood how Object.assign function works. What Object.assign function does is, it goes through each property of source arguments (arguments following the first argument) and overwrite it in the first argument.

The problem in your example is that you tried to call Object.assign with string as its arguments Object.assign('abc', 'def') . String literal in JavaScript is actually an array of characters, and array in an object with indices as properties. By default string properties (the indices) cannot be reassigned (writable: false).

Here's a demonstration:

var a = 'abc';
console.log(a[0]) // outputs 'a'

var descriptor = Object.getOwnPropertyDescriptor(a, 0)
console.log(descriptor)
//outputs
/*
{ value: 'a',
  writable: false,
  enumerable: true,
  configurable: false }
*/

Object.assign('abc', 'def');// throws Cannot assign to read only property '0' of object '[object String]'

As you can see, writable is set to false which means you cannot reassign each character in the string. This explains why the error message says property '0' of string 'abc' cannot be assigned with new value.

So the solution is to do student.password = passHash instead of Object.assign(student.password, passHash);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM