How does it support rotating to a new secret key while old keys still work?
The library supports key rotation by accepting an iterable of secret keys and storing them as a list ordered from oldest to newest.
-
Key Storage: Both
SignerandSerializeracceptsecret_keyas a string, bytes, or an iterable. The helper function_make_keys_listconverts this into alist[bytes]stored inself.secret_keys. The documentation explicitly states this list is ordered "from oldest to newest" . -
Signing: When signing data, the system uses the newest key. The
secret_keyproperty returns the last item in the list (self.secret_keys[-1]), which is used by default inderive_key. -
Verification: When verifying a signature, the system iterates through the keys in reverse order (newest to oldest). The
verify_signaturemethod loops throughreversed(self.secret_keys), deriving a key for each and checking if the signature matches. It returnsTrueas soon as a valid key is found . This allows old signatures to remain valid as long as their corresponding key is still in the list. -
Serializer Support: The
Serializerclass implements the same logic, storingsecret_keysvia_make_keys_listand providing asecret_keyproperty that returns the newest key .