Some information:
- fingerprint is 5 bytes, and is at position 0 on device. There's no fingerprint on user file.
- a token is 128 bytes
-
on device, fseek to position 133 (i.e. after the fingerprint + token). Write the fingerprint + the old token. If power is cut during the writing, or after, the next login will be fine. (successful login if there's a failure = old token at pos 5 on device + old token at pos 0 on user file)
-
once the old token is written, fseek back to position 5 (right after the fingerprint), and write the new token. If power is cut during the writing or after, the next login will fail, look for a fingerprint, if there is, compare with the token after. Since it's the old one, the login will be fine. (successful login if there's a failure = old token at pos 138 on device + old token at position 0 on user file)