Created
January 2, 2024 04:34
-
-
Save neruthes/9df219d524c6852e01acb6d163d6328a to your computer and use it in GitHub Desktop.
Hash numbers for order no, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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