Skip to content

Instantly share code, notes, and snippets.

@neruthes
Created January 2, 2024 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neruthes/9df219d524c6852e01acb6d163d6328a to your computer and use it in GitHub Desktop.
Save neruthes/9df219d524c6852e01acb6d163d6328a to your computer and use it in GitHub Desktop.
Hash numbers for order no, etc.
const crypto = require('crypto');
function renum(num, key) {
// Convert the number to a string
const numStr = String(num);
const len = numStr.length;
// Hash the string using SHA-256
const hash = crypto.createHash('sha256');
const hashedNum = hash.update(numStr, 'utf8').digest('hex').slice(0, len + 2);
// Convert the hash value to an integer
const hashedNumInt = parseInt(hashedNum, 16);
// Convert the key to a buffer
const keyBuffer = Buffer.from(key.replace(/-/g, ''), 'hex');
// Apply XOR operation with the key
const xoredNumInt = Math.abs(hashedNumInt ^ keyBuffer.readUInt32BE(0));
return xoredNumInt.toString().slice(0, len);
}
// Example usage
const key = 'aabbccddeeff00112233445566778899';
for (let i = 0; i < 10; i++) {
console.log(i + 123456, renum(i + 123456, key));
}
for (let i = 0; i < 10; i++) {
console.log(i + 12345678, renum(i + 12345678, key));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment