我想对一些内容进行md5哈希,然后生成n个点的“曲线”或“谱”。也就是说,假设在从0到1的一条线上绘制5、10或20个点,其分布方式对于md5散列是唯一的(冲突并不重要)。基本上,它看起来就像是原子的光发射光谱。


这些点(或谱中的线)以某种方式基于提供的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输入产生各种光谱,那将是很酷的。
发布于 2019-06-12 06:33:43
让我们忽略字符串数据是md5打印的部分,转而关注如何对任意长度的十六进制字符串执行此操作,以便我们可以使用任何我们喜欢的摘要(从CRC32到SHA-512):
作为可运行的代码片段:
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
);
}
将画布拉伸到100%宽度意味着你可以免费获得模糊效果。奖金!
https://stackoverflow.com/questions/56540479
复制相似问题