gpt4 book ai didi

string - 给定 32 字节 md5 字符串,如何获得 n 个点的一致哈希/谱

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:39:33 28 4
gpt4 key购买 nike

我想对一些内容进行 md5 散列,然后生成 n 个点的“曲线”或“频谱”。也就是说,要绘制从 0 到 1 的直线上的 5、10 或 20 个点,以某种方式分布,使其对于 md5 哈希是唯一的(碰撞无关紧要)。基本上它看起来像原子光发射光谱。

enter image description here

enter image description here

这些点(或光谱中的线)是根据提供的 md5 哈希以某种方式生成的,n 提供了您想要的线数。

所以它会像:

function generateSpecrum(md5, n) { return [ ... ] }

默认情况下,它可能只返回 0 到 1 之间的值,但也许您可以给它一个开始值和结束值,以生成范围。

想知道如何用伪代码或 JS 实现这一点。

然而,标准 md5 散列的可能性有很多种。我会这样做:

var crypto = require('crypto')
var data = 'foo'
crypto.createHash('md5').update(data).digest('hex')
// acbd18db4cc2f85cedef654fccc4a4d8

所以一个 32 字节的字符串。在我的例子中,它不需要生成全局唯一值,可能会发生一些冲突,但如果它有办法从不同的 md5 输入生成各种光谱,那就太棒了。

最佳答案

让我们忽略字符串数据是 md5 打印的部分,而是关注如何为任意长度的十六进制字符串执行此操作,因此我们可以使用我们喜欢的任何摘要(从 CRC32 到 SHA- 512):

  1. 从色调渐变背景开始(我们可以在 CSS 中实现),
  2. 把字符串转成bit print(JS内置),
  3. 涂黑任何对应于零位的区域。

作为一个可运行的片段:

function hexstr2bin(stringinput) {
// let's not be constrained by JS integer precision,
// which is only good for 53 bits. Technically we don't
// care what the "numbers" are here, we just want the
// ones and zeros that the numbers turn into.
return stringinput.split('').map(c => (
parseInt(c, 16).toString(2).padStart(4,'0')
)).join('');
}

function renderSpectrum(stringinput) {
let cvs = document.createElement('canvas');
let bits = Array.from(hexstr2bin(stringinput));

cvs.width = bits.length;
cvs.height = 1;
ctx = cvs.getContext('2d');
ctx.strokeStyle = 'black';

bits.forEach( (bit,i) => {
if (bit === "0") {
ctx.moveTo(i,0);
ctx.lineTo(i,1);
ctx.stroke();
}
});

document.body.appendChild(cvs);
};

renderSpectrum("acbd18db4fccc4a4d8");
renderSpectrum("c5887c91d0002f2a869a4b0772827701");
renderSpectrum("06956ff032d78e090d0d292aa9d8e7143ab08cf1ed444944529f79a4f937306a");
canvas {
width: 100%;
height: 40px;
background: linear-gradient(
to right,
violet, blue, cyan, green, yellow, orange, red
);
}

将 Canvas 拉伸(stretch)到 100% 宽度意味着您可以免费进行模糊处理。奖金!

关于string - 给定 32 字节 md5 字符串,如何获得 n 个点的一致哈希/谱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56540479/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com