##// END OF EJS Templates
Added GET request handler, command for key generation. Changed KeyPair format for ecdsa (use to_string instead of to_pem because it's shorter)
Added GET request handler, command for key generation. Changed KeyPair format for ecdsa (use to_string instead of to_pem because it's shorter)

File last commit:

r836:9ee107b9 decentral
r836:9ee107b9 decentral
Show More
sync_key.py
61 lines | 2.0 KiB | text/x-python | PythonLexer
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794 import base64
neko259
Added import for ecdsa singature verification error
r807 from ecdsa import SigningKey, VerifyingKey, BadSignatureError
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794 from django.db import models
TYPE_ECDSA = 'ecdsa'
APP_LABEL_BOARDS = 'boards'
class KeyPairManager(models.Manager):
neko259
Added 'primary' parameter for the key to indicate this key would be used in...
r796 def generate_key(self, key_type=TYPE_ECDSA, primary=False):
if primary and self.filter(primary=True).exists():
raise Exception('There can be only one primary key')
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794 if key_type == TYPE_ECDSA:
private = SigningKey.generate()
public = private.get_verifying_key()
neko259
Added GET request handler, command for key generation. Changed KeyPair format for ecdsa (use to_string instead of to_pem because it's shorter)
r836 private_key_str = base64.b64encode(private.to_string()).decode()
public_key_str = base64.b64encode(public.to_string()).decode()
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794
return self.create(public_key=public_key_str,
neko259
Fixed indent in key manager
r809 private_key=private_key_str,
key_type=TYPE_ECDSA, primary=primary)
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794 else:
neko259
Added 'primary' parameter for the key to indicate this key would be used in...
r796 raise Exception('Key type not supported')
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794
def verify(self, public_key_str, string, signature, key_type=TYPE_ECDSA):
if key_type == TYPE_ECDSA:
neko259
Added GET request handler, command for key generation. Changed KeyPair format for ecdsa (use to_string instead of to_pem because it's shorter)
r836 public = VerifyingKey.from_string(base64.b64decode(public_key_str))
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794 signature_byte = base64.b64decode(signature)
try:
neko259
Added import for ecdsa singature verification error
r807 return public.verify(signature_byte, string.encode())
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794 except BadSignatureError:
return False
else:
neko259
Added 'primary' parameter for the key to indicate this key would be used in...
r796 raise Exception('Key type not supported')
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794
neko259
Added GET request handler, command for key generation. Changed KeyPair format for ecdsa (use to_string instead of to_pem because it's shorter)
r836 def has_primary(self):
return self.filter(primary=True).exists()
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794
class KeyPair(models.Model):
class Meta:
app_label = APP_LABEL_BOARDS
objects = KeyPairManager()
public_key = models.TextField()
private_key = models.TextField()
key_type = models.TextField()
neko259
Added 'primary' parameter for the key to indicate this key would be used in...
r796 primary = models.BooleanField(default=False)
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794
def __str__(self):
neko259
Added GET request handler, command for key generation. Changed KeyPair format for ecdsa (use to_string instead of to_pem because it's shorter)
r836 return '%s | %s' % (self.key_type, self.public_key)
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794
def sign(self, string):
neko259
Added GET request handler, command for key generation. Changed KeyPair format for ecdsa (use to_string instead of to_pem because it's shorter)
r836 private = SigningKey.from_string(base64.b64decode(
self.private_key.encode()))
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794 signature_byte = private.sign(string.encode())
return base64.b64encode(signature_byte)