Add test for LD sig

pull/1/head
Thomas Sileo 2018-06-16 12:15:03 +02:00
rodzic ad7d6ca53a
commit 452e9cbc95
2 zmienionych plików z 22 dodań i 2 usunięć

Wyświetl plik

@ -56,10 +56,10 @@ def _doc_hash(doc):
return h.hexdigest()
def verify_signature(doc, pubkey):
def verify_signature(doc, key: "Key"):
to_be_signed = _options_hash(doc) + _doc_hash(doc)
signature = doc["signature"]["signatureValue"]
signer = PKCS1_v1_5.new(pubkey)
signer = PKCS1_v1_5.new(key.pubkey or key.privkey)
digest = SHA256.new()
digest.update(to_be_signed.encode("utf-8"))
return signer.verify(digest, base64.b64decode(signature))

Wyświetl plik

@ -0,0 +1,20 @@
import json
import logging
from little_boxes import linked_data_sig
from little_boxes.key import Key
logging.basicConfig(level=logging.DEBUG)
DOC = """{"type": "Create", "actor": "https://microblog.pub", "object": {"type": "Note", "sensitive": false, "cc": ["https://microblog.pub/followers"], "to": ["https://www.w3.org/ns/activitystreams#Public"], "content": "<p>Hello world!</p>", "tag": [], "source": {"mediaType": "text/markdown", "content": "Hello world!"}, "attributedTo": "https://microblog.pub", "published": "2018-05-21T15:51:59Z", "id": "https://microblog.pub/outbox/988179f13c78b3a7/activity", "url": "https://microblog.pub/note/988179f13c78b3a7", "replies": {"type": "OrderedCollection", "totalItems": 0, "first": "https://microblog.pub/outbox/988179f13c78b3a7/replies?page=first", "id": "https://microblog.pub/outbox/988179f13c78b3a7/replies"}, "likes": {"type": "OrderedCollection", "totalItems": 2, "first": "https://microblog.pub/outbox/988179f13c78b3a7/likes?page=first", "id": "https://microblog.pub/outbox/988179f13c78b3a7/likes"}, "shares": {"type": "OrderedCollection", "totalItems": 3, "first": "https://microblog.pub/outbox/988179f13c78b3a7/shares?page=first", "id": "https://microblog.pub/outbox/988179f13c78b3a7/shares"}}, "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1", {"Hashtag": "as:Hashtag", "sensitive": "as:sensitive"}], "published": "2018-05-21T15:51:59Z", "to": ["https://www.w3.org/ns/activitystreams#Public"], "cc": ["https://microblog.pub/followers"], "id": "https://microblog.pub/outbox/988179f13c78b3a7"}""" # noqa: E501
def test_linked_data_sig():
doc = json.loads(DOC)
k = Key("https://lol.com")
k.new()
linked_data_sig.generate_signature(doc, k)
assert linked_data_sig.verify_signature(doc, k)