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 ?
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;
}
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.
Maybe this one https://github.com/lovell/farmhash ?
const farmhash = require('farmhash');
const hexDigest = crypto.createHash('md5').update().digest('hex');
farmhash.fingerprint64(hexDigest);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With