Show More
@@ -0,0 +1,54 b'' | |||||
|
1 | import base64 | |||
|
2 | from ecdsa import SigningKey, VerifyingKey | |||
|
3 | from django.db import models | |||
|
4 | ||||
|
5 | TYPE_ECDSA = 'ecdsa' | |||
|
6 | ||||
|
7 | APP_LABEL_BOARDS = 'boards' | |||
|
8 | ||||
|
9 | ||||
|
10 | class KeyPairManager(models.Manager): | |||
|
11 | def generate_key(self, key_type=TYPE_ECDSA): | |||
|
12 | if key_type == TYPE_ECDSA: | |||
|
13 | private = SigningKey.generate() | |||
|
14 | public = private.get_verifying_key() | |||
|
15 | ||||
|
16 | private_key_str = private.to_pem().decode() | |||
|
17 | public_key_str = public.to_pem().decode() | |||
|
18 | ||||
|
19 | return self.create(public_key=public_key_str, | |||
|
20 | private_key=private_key_str, | |||
|
21 | key_type=TYPE_ECDSA) | |||
|
22 | else: | |||
|
23 | return None | |||
|
24 | ||||
|
25 | def verify(self, public_key_str, string, signature, key_type=TYPE_ECDSA): | |||
|
26 | if key_type == TYPE_ECDSA: | |||
|
27 | public = VerifyingKey.from_pem(public_key_str) | |||
|
28 | signature_byte = base64.b64decode(signature) | |||
|
29 | try: | |||
|
30 | return public.verify(signature_byte, | |||
|
31 | string.encode()) | |||
|
32 | except BadSignatureError: | |||
|
33 | return False | |||
|
34 | else: | |||
|
35 | return False | |||
|
36 | ||||
|
37 | ||||
|
38 | class KeyPair(models.Model): | |||
|
39 | class Meta: | |||
|
40 | app_label = APP_LABEL_BOARDS | |||
|
41 | ||||
|
42 | objects = KeyPairManager() | |||
|
43 | ||||
|
44 | public_key = models.TextField() | |||
|
45 | private_key = models.TextField() | |||
|
46 | key_type = models.TextField() | |||
|
47 | ||||
|
48 | def __str__(self): | |||
|
49 | return '%s: %s' % (self.key_type, self.public_key) | |||
|
50 | ||||
|
51 | def sign(self, string): | |||
|
52 | private = SigningKey.from_pem(self.private_key) | |||
|
53 | signature_byte = private.sign(string.encode()) | |||
|
54 | return base64.b64encode(signature_byte) |
@@ -1,8 +1,16 b'' | |||||
1 | <?xml version="1.1" encoding="UTF-8" ?> |
|
1 | <?xml version="1.1" encoding="UTF-8" ?> | |
2 | <response> |
|
2 | <response> | |
|
3 | <!-- | |||
|
4 | Valid statuses are 'success' and 'error'. | |||
|
5 | --> | |||
3 | <status>success</status> |
|
6 | <status>success</status> | |
4 | <models> |
|
7 | <models> | |
5 | <model name="post"> |
|
8 | <!-- | |
|
9 | ref-id is used only to reference the model block in the | |||
|
10 | signatures block. It could be any string that is unique throughout | |||
|
11 | the file. | |||
|
12 | --> | |||
|
13 | <model name="post" ref-id="1"> | |||
6 | <id key="id1" local-id="1" /> |
|
14 | <id key="id1" local-id="1" /> | |
7 | <title>13</title> |
|
15 | <title>13</title> | |
8 | <text>Thirteen</text> |
|
16 | <text>Thirteen</text> | |
@@ -23,7 +31,7 b'' | |||||
23 | <id key="id43" local-id="5" /> |
|
31 | <id key="id43" local-id="5" /> | |
24 | </next> |
|
32 | </next> | |
25 | </model> |
|
33 | </model> | |
26 | <model name="post"> |
|
34 | <model name="post" ref-id="2"> | |
27 | <id key="id1" local-id="id2" /> |
|
35 | <id key="id1" local-id="id2" /> | |
28 | <title>13</title> |
|
36 | <title>13</title> | |
29 | <text>Thirteen</text> |
|
37 | <text>Thirteen</text> | |
@@ -34,4 +42,15 b'' | |||||
34 | </tags> |
|
42 | </tags> | |
35 | </model> |
|
43 | </model> | |
36 | </models> |
|
44 | </models> | |
|
45 | ||||
|
46 | <!-- The signature block is separate from the model block because the | |||
|
47 | signature value should not be included while getting the block sign. | |||
|
48 | When signing a model block, it should be in the canonical XML form. | |||
|
49 | See http://www.w3.org/TR/xml-c14n11/ for details. | |||
|
50 | --> | |||
|
51 | <signatures> | |||
|
52 | <signature model-ref="1" key="id1" value="dhefhtreh" /> | |||
|
53 | <signature model-ref="1" key="id45" value="dsgfgdhefhtreh" /> | |||
|
54 | <signature model-ref="2" key="id2" value="dehdfh" /> | |||
|
55 | </signatures> | |||
37 | </response> |
|
56 | </response> |
General Comments 0
You need to be logged in to leave comments.
Login now