Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nicjes/38b96209edcb902f74a66d28ee9cb2c2 to your computer and use it in GitHub Desktop.
Save nicjes/38b96209edcb902f74a66d28ee9cb2c2 to your computer and use it in GitHub Desktop.
JavaScript version
function crc16(crc, buffer) {
for (let byte of buffer) {
crc ^= BigInt(byte) << BigInt(8);
for (let i = 0; i < 8; i++) {
if (crc & BigInt(0x8000)) {
crc = (crc << BigInt(1)) ^ BigInt(0x1021);
} else {
crc <<= BigInt(1);
}
crc &= BigInt(0xFFFF);
}
}
return crc;
}
function generatePassword(uid, mfg) {
let crc = crc16(BigInt(0x49A3), uid); // Calculate the UID CRC
crc = crc | crc16(crc, mfg) << BigInt(16); // Calculate the MFG CRC
crc = ((crc >> BigInt(8)) & BigInt(0x00FF00FF)) | ((crc << BigInt(8)) & BigInt(0xFF00FF00)); // Rotate the bytes
let password = crc.toString(16).toUpperCase().replace(/(..)(..)(..)(..)/g, '$1:$2:$3:$4'); // Format the password
return password;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment