【发布时间】:2021-05-02 10:30:40
【问题描述】:
我正在测试一个代码以从文件生成 26 个字符的自定义 MD5 哈希,它可以正常工作,除了那些在左侧第一个位置生成带有零的哈希的文件,在这种情况下,零消失并且哈希仍然是25个字符而不是26个,代码如下:
'use strict';
var crypto = require('crypto');
var DEFAULTS = {
charSet: [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z'],
maxLength: 26,
right: false
};
var config = {
charSet: DEFAULTS.charSet,
maxLength: DEFAULTS.maxLength,
right: DEFAULTS.right
};
function add (x, y, base) {
var value, digit, carry;
var output = [];
var length = Math.max(x.length, y.length);
for (var i = 0; i < length; i++) {
value = (x[i] || 0) + (y[i] || 0) + (carry || 0);
digit = value % base;
carry = (value - digit) / base;
output.push(digit);
}
return (carry > 0) ? output.concat(carry) : output;
}
function multiply (x, y, base) {
var value, digit, carry, offset, summand;
return x.reduce(function (sum, currentX, lengthX) {
offset = Array.apply(null, new Array(lengthX)).map(function () { return 0; });
summand = y.reduce(function (previous, currentY, lengthY) {
value = currentX * currentY + previous.carry;
digit = value % base;
carry = (value - digit) / base;
if (lengthY === y.length - 1 && carry > 0) {
return { output: previous.output.concat(digit, carry), carry: carry };
} else {
return { output: previous.output.concat(digit), carry: carry };
}
}, { output: offset, carry: 0 }).output;
return add(sum, summand, base);
}, []);
}
function numberInBase (value, base) {
var output = [];
while (value > 0) {
var digit = value % base;
output.push(digit);
value = (value - digit) / base;
}
return output;
}
function configure (options) {
config = {
charSet: DEFAULTS.charSet,
maxLength: DEFAULTS.maxLength,
right: DEFAULTS.right
};
if (options && options.charSet && Array.isArray(options.charSet)) {
config.charSet = options.charSet;
}
if (options && typeof options.maxLength === 'number') {
config.maxLength = options.maxLength;
}
if (options && typeof options.right === 'boolean') {
config.right = options.right;
}
}
function digest (input) {
if (config.maxLength < 1 || config.charSet.length < 2) {
return Array.apply(null, new Array(Math.max(config.maxLength, 0)))
.map(function () { return config.charSet[0] || ''; })
.join('');
}
var md5 = crypto.createHash('md5').update(input).digest('hex')
.split('')
.map(function (digit) { return parseInt(digit, 16); });
var base = config.charSet.length;
var sixteen = numberInBase(16, base);
var output = md5.reduce(function (value, current) {
return add(multiply(value, sixteen, base), numberInBase(current, base), base);
}, []).map(function (digit) {
return config.charSet[digit];
}).reverse().join('');
if (config.maxLength >= output.length) {
return output;
}
return config.right ? output.substr(-config.maxLength) : output.substr(0, config.maxLength);
}
module.exports = {
configure: configure,
digest: digest
};
const fs = require('fs');
let result = digest(fs.readFileSync('/users/desktop/Test.PDF'));
console.log('Huella Digital MD5: ' + result)
有什么想法吗?
提前致谢!
【问题讨论】:
-
检查长度,如有必要,添加前导零。平均十六分之一的计算将需要它。 256 分之一需要两个前导零等。
-
谢谢罗森。请问代码在哪里?我尝试将 maxLength: 26 更改为 maxLength: 130,但它不起作用...
-
在获得最终字符串后进行检查。根据需要添加尽可能多的“0”字符以使长度正确。
-
对不起,我是编程和java新手,我不明白你向我解释了什么,我必须在代码的哪个部分添加零?
-
您正在生成一个“26 个字符”的哈希值。当您将哈希字符传递给程序的任何部分时,在现有代码之后添加长度检查。这样你总是会给出 26 个字符。