Forked from atc1441/Philips Sonicare NFC Password generation
Last active
June 22, 2023 16:06
-
-
Save nicjes/38b96209edcb902f74a66d28ee9cb2c2 to your computer and use it in GitHub Desktop.
JavaScript version
This file contains hidden or 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
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