Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

php - Make MD5 raw in JavaScript

I am converting PHP code to Node JS. I am facing a issue with MD5 binary raw that is MD5('String',true) and getting ???S?????????<]zVC..zv?????? . I have tried in Node.js but I am not getting same string on Node js.

Here is my code:

var md5 = require('md5');
var requestArray = [{
    "fname": "mobilenumber",
    "fvalue": '1234568970'
}, {
    "fname": "phone",
    "fvalue": '000000000'
}];

var post_data  = JSON.stringify(requestArray);
var m = md5(post_data);

I have tried to pass true parameter but it's not working.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

since you already have the hex, just use a hex2bin function,

var m = hex2bin(md5(post_data)); 

/

function hex2bin (s) {
  //  discuss at: http://locutus.io/php/hex2bin/
  // original by: Dumitru Uzun (http://duzun.me)
  //   example 1: hex2bin('44696d61')
  //   returns 1: 'Dima'
  //   example 2: hex2bin('00')
  //   returns 2: 'x00'
  //   example 3: hex2bin('2f1q')
  //   returns 3: false

  var ret = []
  var i = 0
  var l

  s += ''

  for (l = s.length; i < l; i += 2) {
    var c = parseInt(s.substr(i, 1), 16)
    var k = parseInt(s.substr(i + 1, 1), 16)
    if (isNaN(c) || isNaN(k)) return false
    ret.push((c << 4) | k)
  }

  return String.fromCharCode.apply(String, ret)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...