Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript. Convert MD5 hash into an integer

Tags:

javascript

md5

Is it possible in Javascript to convert something like this: d131dd02c5e6eec4 693d9a0698aff95c 2fcab58712467eab 4004583eb8fb7f89, which is the result of an MD5 hash function, into an integer ?

like image 913
AndreiBogdan Avatar asked Jun 20 '13 13:06

AndreiBogdan


3 Answers

May not be perfect, but this suited my needs

export function stringToIntHash(str, upperbound, lowerbound) {
  let result = 0;
  for (let i = 0; i < str.length; i++) {
    result = result + str.charCodeAt(i);
  }

  if (!lowerbound) lowerbound = 0;
  if (!upperbound) upperbound = 500;

  return (result % (upperbound - lowerbound)) + lowerbound;
}
like image 139
Dara Java Avatar answered Oct 06 '22 01:10

Dara Java


That looks like a hexadecimal number, so you could try using the parseInt function and pass in a base of sixteen:

var num = parseInt(string, 16);

Edit: This method doesn't actually work. See the comments for details.

like image 25
Ryan Endacott Avatar answered Oct 05 '22 23:10

Ryan Endacott


Maybe this one https://github.com/lovell/farmhash ?

const farmhash = require('farmhash');
const hexDigest = crypto.createHash('md5').update().digest('hex');
farmhash.fingerprint64(hexDigest);
like image 42
junxi Avatar answered Oct 05 '22 23:10

junxi