简体   繁体   English

Javascript。 将 MD5 哈希转换为整数

[英]Javascript. Convert MD5 hash into an integer

是否可以在 Javascript d131dd02c5e6eec4 693d9a0698aff95c 2fcab58712467eab 4004583eb8fb7f89转换为整数: d131dd02c5e6eec4 693d9a0698aff95c 2fcab58712467eab 4004583eb8fb7f89 ,这是 MD5 哈希函数的结果?

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:这看起来像一个十六进制数,因此您可以尝试使用parseInt函数并传入十六进制数:

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 ?也许这个https://github.com/lovell/farmhash

const farmhash = require('farmhash');
const hexDigest = crypto.createHash('md5').update().digest('hex');
farmhash.fingerprint64(hexDigest);

ES6 版本,字符串到 0-9 之间的整数:

string.split('').map(i => i.charCodeAt(0)).reduce((a, b) => a + b, 0) % 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM