node.js how to repreduce PHP MD5 encryption

半城伤御伤魂 提交于 2019-12-05 16:06:57

please try these:

    var crypto = require('crypto');
var salt = 'd2g6IOP(U(&§)%U§VUIPU(HN%V/§§URerjh0ürfqw4zoöqe54gß0äQ"LOU$3wer'
var password = 'pass';

var hashMd5 = crypto.createHash('md5').update(salt + password).digest("hex");
var hasSha1 = crypto.createHash('sha1').update(hasMd5).digest("hex");
console.log(hashSha1);

as file: hash.js

And as hash.php these code:

<?php

$_passwordSalt = 'd2g6IOP(U(&§)%U§VUIPU(HN%V/§§URerjh0ürfqw4zoöqe54gß0äQ"LOU$3wer';
$password = 'pass';

//echo md5("phinware");
echo sha1(md5($_passwordSalt.$password));
echo "\n";

And than execute both files:

  • > php hash.php
  • > node hash.js

My results:

both: 3cbd1242e8e510a16f39d7e0bfd18a0e03d0de3f

php:

$a = 'a';
$b = 'b';
echo md5($a.$b);

equals to node.js:

var crypto = require('crypto');
var a = 'b', b = 'b';
var md5 = crypto.createHash('md5');
md5.update(xml, 'utf8');
md5.update(config.secret, 'utf8');
console.log(md5.digest('hex'));

Try this using crypto module:

var crypto = require('crypto');

UserSchema.methods.hashPassword = function(password) {
    var salt = 'd2g6IOP(U(&§)%U§VUIPU(HN%V/§§URerjh0ürfqw4zoöqe54gß0äQ"LOU$3wer';
    var hashStr = password + salt;
    var md5Hash = crypto.createHash('md5').update(hashStr).digest('hex');
    var sha1 = crypto.createHash('sha1').update(md5Hash).digest('hex');
    console.log(sha1); 
    return sha1;
};
jishi

You need to consider that a hash algorithm works on byte values, and you are using string values. This means that encoding will come in to play here, and from what I know PHP uses latin1 by default, while node.js uses utf-8.

crypto.createHash('md5').update(hashStr, 'ascii').digest('hex')

I'm not sure if ascii only handles 7-bit ascii or actual extended charsets like latin1, but it seems to be the only one supported directly in the update() method. If you need to control the extended charset, you should create a Buffer from the correct encoding, and use that as parameter to update() instead. The built in support in node.js is rather limited:

Buffer.isEncoding = function(encoding) {
  switch ((encoding + '').toLowerCase()) {
    case 'hex':
    case 'utf8':
    case 'utf-8':
    case 'ascii':
    case 'binary':
    case 'base64':
    case 'ucs2':
    case 'ucs-2':
    case 'utf16le':
    case 'utf-16le':
    case 'raw':
      return true;

    default:
      return false;
  }
};

And you should consider using some other tool to convert it. This thread (List of encodings that Node.js supports) suggests using iconv or iconv-lite.

Of course, the same applies to SHA1, but since you are using SHA1 on a hex representation of an MD5, it would never fall out of 7-bit ascii (where latin1 and utf-8 would produce the same byte sequence).

here is the currect solution

var md5 = require('MD5'),
    sha1 = require('sha1');

var salt = 'd2g6IOP(U(&§)%U§VUIPU(HN%V/§§URerjh0ürfqw4zoöqe54gß0äQ"LOU$3wer';
var hash = sha1(md5(salt+password));
return hash;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!