Created
April 10, 2015 17:22
-
-
Save Averroes/67a6e12bbf2730fecc78 to your computer and use it in GitHub Desktop.
simple authentication of clients
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
| # auth.py | |
| import hmac | |
| import os | |
| def client_authenticate(connection, secret_key): | |
| ''' | |
| Authenticate client to a remote service. | |
| connection represents a network connection. | |
| secret_key is a key known only to both client/server. | |
| ''' | |
| message = connection.recv(32) | |
| hash = hmac.new(secret_key, message) | |
| digest = hash.digest() | |
| connection.send(digest) | |
| def server_authenticate(connection, secret_key): | |
| ''' | |
| Request client authentication. | |
| ''' | |
| message = os.urandom(32) | |
| connection.send(message) | |
| hash = hmac.new(secret_key, message) | |
| digest = hash.digest() | |
| response = connection.recv(len(digest)) | |
| return hmac.compare_digest(digest,response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment