##// END OF EJS Templates
Made tests properly override posting delay variable
Made tests properly override posting delay variable

File last commit:

r796:6e2f495a decentral
r806:3a0369a9 decentral
Show More
sync_key.py
58 lines | 1.8 KiB | text/x-python | PythonLexer
neko259
Actually added the sync_key model. Added signature block to the 'get' response...
r794 import base64
from ecdsa import SigningKey, VerifyingKey
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()
private_key_str = private.to_pem().decode()
public_key_str = public.to_pem().decode()
return self.create(public_key=public_key_str,
private_key=private_key_str,
neko259
Added 'primary' parameter for the key to indicate this key would be used in...
r796 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:
public = VerifyingKey.from_pem(public_key_str)
signature_byte = base64.b64decode(signature)
try:
return public.verify(signature_byte,
string.encode())
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
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):
return '%s: %s' % (self.key_type, self.public_key)
def sign(self, string):
private = SigningKey.from_pem(self.private_key)
signature_byte = private.sign(string.encode())
return base64.b64encode(signature_byte)