recoverable-random
Version:
A recoverable number generator that generates pseudorandom integer or floating-point numbers within a given range.
29 lines (28 loc) • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateShortNumericHash = void 0;
const md5_1 = require("./md5");
function generateShortNumericHash(data) {
if (typeof data !== "string") {
throw new TypeError("Input data must be a string");
}
// Generate MD5 hash
const md5Hash = md5_1.default.hashStr(data, true);
// XOR the first half of the digest with the second half
const xorResult = Buffer.alloc(md5Hash.length / 2);
for (let i = 0; i < xorResult.length; i++) {
xorResult[i] = md5Hash[i] ^ md5Hash[i + xorResult.length];
}
// Get the length of the original data
const dataLength = Buffer.alloc(4);
dataLength.writeUInt32BE(data.length);
// Concatenate the XOR result and data length
const combinedBuffer = Buffer.concat([xorResult, dataLength]);
// Convert the combined buffer to a string of numeric characters
let numericString = "";
for (let i = 0; i < combinedBuffer.length; i++) {
numericString += combinedBuffer[i].toString().padStart(3, "0");
}
return numericString;
}
exports.generateShortNumericHash = generateShortNumericHash;