2016-10-28 3 views
0

는 I 1.mongoose에서 암호 해시 및 암호를 사용하여 암호를 해독하려면 어떻게해야합니까?

  1. 를 반환 해시 암호를 만들
  2. 을 호출 해시를 제 함수를 생성하는 암호 염화칼슘을 사용하는 임의의 염을 생성 서버 측 기능 세트가 내가 가입 페이지에서 사용자 이름과 비밀번호를 발급받을 때마다

    var sha512 = function(password, salt){ 
        var hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */ 
        hash.update(password); 
        var value = hash.digest('hex'); 
        return { 
         salt:salt, 
         passwordHash:value 
        }; 
    }; 
    
    
    //--------------Function to hash password 
    function saltHashPassword(userpassword) { 
        var salt = genRandomString(16); /** Gives us salt of length 16 */ 
        var passwordData = sha512(userpassword, salt); 
        console.log('UserPassword = '+userpassword); 
        console.log('Passwordhash = '+passwordData.passwordHash); 
        console.log('\nSalt = '+passwordData.salt); 
        return passwordData; 
    } 
    //---------------------- 
    // Function to generate salt 
    var genRandomString = function(length){ 
        return crypto.randomBytes(Math.ceil(length/2)) 
         .toString('hex') /** convert to hexadecimal format */ 
         .slice(0,length); /** return required number of characters */ 
    }; 
    

, 나는 saltHas를 호출 할 수 있습니다 hPassword (passwordinstring) 해시와 소금을 가져옵니다. 그런 다음이 내용을 몽구스 스키마의 사용자 문서에 저장합니다.

사용자가 로그인하려고 할 때 어떻게 암호를 해독합니까?

이것은 현재 사용자 스키마 user.js에서 작성한 방법입니다. 나는 제공된 이메일을 위해 소금을 저장하는 방법을 고수했다.

// this authenticates the user against the database 
UserSchema.statics.authenticate = function(email, password, callback){ 
    User.findOne({email: email}) 
     .exec(function(error, user){ 
      if(error){ 
       return callback(error); 
      }else if(!user){ 
       var err = new Error("User not found"); 
       err.status = 401; 
       return callback(err); 
      } 
      // if we reach this part of the code, then there is a valid username 
      // I could call sha512(password, salt) and get the hash and then 
      // match the two calculated hashed passwords. But how do 
      // I query the salt within a mongo query? 
      // every user has email, hashed_password, salt fields in its document 

     }); 
}; 

답변

0

휠을 재발 명하려는 것 같습니다. 소금을 따로 보관할 필요가없는 bcrypt를 사용할 수 있습니다.

하지만 당신은 이미 시작 이후 :

// this authenticates the user against the database 
UserSchema.statics.authenticate = function(email, password, callback){ 
    User.findOne({email: email}) 
     .exec(function(error, user){ 
      if(error){ 
       return callback(error); 
      } 
      if(!user){ 
       var err = new Error("User not found"); 
       err.status = 401; 
       return callback(err); 
      } 
      // if we reach this part of the code, then there is a valid username 
      // I could call sha512(password, salt) and get the hash and then 
      // match the two calculated hashed passwords. But how do 
      // I query the salt within a mongo query? 
      // every user has email, hashed_password, salt fields in its document 


      var salt = user.salt; // Because you said that User has salt, email, hashed_password 

      // Do your password matching.... 
     }); 
}; 

몽구스의 발견과 findOne 방법

오류 또는 문서 중 하나를 반환합니다. 콜백에 이미 오류와 사용자가 있습니다. user.email, user.salt 등으로 속성에 액세스 할 수 있습니다.

사용자가 소금을 포함하고 있기 때문에 별도의 쿼리가 필요하지 않습니다.

+0

내가 이렇게해야한다고 말하는거야? let salt = user.salt; let givenPassObj = sha512 (password, salt); if (givenPassObj.passwordHash === user.hashed_password) { return 콜백 (null, 사용자); } else { return callback(); }' – McFiddlyWiddly

+0

예. 그렇게해야합니다. –

+0

@McFiddlyWiddly 아니요. SHA512로 해싱하는 것은 암호로 안전하지 않습니다. 게시물에 명시된 바와 같이 bcrypt를 사용하거나 SHA512를 실제로 사용하려면 PBKDF2의 프리미티브로 사용하십시오. –

관련 문제