Skip to content

Instantly share code, notes, and snippets.

@fionn
Last active October 7, 2021 18:22
Show Gist options
  • Save fionn/ee8669294912640a3931f8002f72e67e to your computer and use it in GitHub Desktop.
Save fionn/ee8669294912640a3931f8002f72e67e to your computer and use it in GitHub Desktop.
Dumb idea for intrinsic timing-attack-resistant Python object comparison
#!/usr/bin/env python3
import hmac
class SecureObject(bytes):
"""Bytes that can be compared safely"""
def __eq__(self, other: object) -> bool:
return hmac.compare_digest(self, other) # type: ignore
def main() -> None:
"""Entry point"""
a = SecureObject([12, 33])
b = SecureObject([12, 33])
print(a == b)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment