# HG changeset patch # User neko259 # Date 2014-08-14 14:33:54 # Node ID e6c8be6a02f1b9c74c08994f03b484570f6c8f3a # Parent 8a6f4ef57ab6405dc66dbf3e50eb0dfe3255b1d9 Actually added the sync_key model. Added signature block to the 'get' response sample. Clarified how the message should be signed. diff --git a/boards/models/sync_key.py b/boards/models/sync_key.py new file mode 100644 --- /dev/null +++ b/boards/models/sync_key.py @@ -0,0 +1,54 @@ +import base64 +from ecdsa import SigningKey, VerifyingKey +from django.db import models + +TYPE_ECDSA = 'ecdsa' + +APP_LABEL_BOARDS = 'boards' + + +class KeyPairManager(models.Manager): + def generate_key(self, key_type=TYPE_ECDSA): + 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, + key_type=TYPE_ECDSA) + else: + return None + + 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: + return False + + +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() + + 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) diff --git a/docs/samples/response_get.xml b/docs/samples/response_get.xml --- a/docs/samples/response_get.xml +++ b/docs/samples/response_get.xml @@ -1,8 +1,16 @@ + success - + + 13 Thirteen @@ -23,7 +31,7 @@ - + 13 Thirteen @@ -34,4 +42,15 @@ + + + + + + +