Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
554ca58e
Commit
554ca58e
authored
6 years ago
by
Erik Johnston
Browse files
Options
Downloads
Patches
Plain Diff
Make add_hashes_and_signatures operate on dicts
parent
073f6c2e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
synapse/crypto/event_signing.py
+6
-10
6 additions, 10 deletions
synapse/crypto/event_signing.py
tests/crypto/test_event_signing.py
+26
-30
26 additions, 30 deletions
tests/crypto/test_event_signing.py
with
32 additions
and
40 deletions
synapse/crypto/event_signing.py
+
6
−
10
View file @
554ca58e
...
...
@@ -131,12 +131,12 @@ def compute_event_signature(event_dict, signature_name, signing_key):
return
redact_json
[
"
signatures
"
]
def
add_hashes_and_signatures
(
event
,
signature_name
,
signing_key
,
def
add_hashes_and_signatures
(
event
_dict
,
signature_name
,
signing_key
,
hash_algorithm
=
hashlib
.
sha256
):
"""
Add content hash and sign the event
Args:
event_dict (
EventBuilder
): The event to add hashes to and sign
event_dict (
dict
): The event to add hashes to and sign
signature_name (str): The name of the entity signing the event
(typically the server
'
s hostname).
signing_key (syutil.crypto.SigningKey): The key to sign with
...
...
@@ -144,16 +144,12 @@ def add_hashes_and_signatures(event, signature_name, signing_key,
to hash the event
"""
name
,
digest
=
compute_content_hash
(
event
.
get_pdu_json
(),
hash_algorithm
=
hash_algorithm
,
)
name
,
digest
=
compute_content_hash
(
event_dict
,
hash_algorithm
=
hash_algorithm
)
if
not
hasattr
(
event
,
"
hashes
"
):
event
.
hashes
=
{}
event
.
hashes
[
name
]
=
encode_base64
(
digest
)
event_dict
.
setdefault
(
"
hashes
"
,
{})[
name
]
=
encode_base64
(
digest
)
event
.
signatures
=
compute_event_signature
(
event
.
get_pdu_json
()
,
event
_dict
[
"
signatures
"
]
=
compute_event_signature
(
event
_dict
,
signature_name
=
signature_name
,
signing_key
=
signing_key
,
)
This diff is collapsed.
Click to expand it.
tests/crypto/test_event_signing.py
+
26
−
30
View file @
554ca58e
...
...
@@ -18,7 +18,7 @@ import nacl.signing
from
unpaddedbase64
import
decode_base64
from
synapse.crypto.event_signing
import
add_hashes_and_signatures
from
synapse.events
.builder
import
EventBuilder
from
synapse.events
import
FrozenEvent
from
tests
import
unittest
...
...
@@ -40,20 +40,18 @@ class EventSigningTestCase(unittest.TestCase):
self
.
signing_key
.
version
=
KEY_VER
def
test_sign_minimal
(
self
):
builder
=
EventBuilder
(
{
'
event_id
'
:
"
$0:domain
"
,
'
origin
'
:
"
domain
"
,
'
origin_server_ts
'
:
1000000
,
'
signatures
'
:
{},
'
type
'
:
"
X
"
,
'
unsigned
'
:
{
'
age_ts
'
:
1000000
},
}
)
event_dict
=
{
'
event_id
'
:
"
$0:domain
"
,
'
origin
'
:
"
domain
"
,
'
origin_server_ts
'
:
1000000
,
'
signatures
'
:
{},
'
type
'
:
"
X
"
,
'
unsigned
'
:
{
'
age_ts
'
:
1000000
},
}
add_hashes_and_signatures
(
builder
,
HOSTNAME
,
self
.
signing_key
)
add_hashes_and_signatures
(
event_dict
,
HOSTNAME
,
self
.
signing_key
)
event
=
builder
.
build
(
)
event
=
FrozenEvent
(
event_dict
)
self
.
assertTrue
(
hasattr
(
event
,
'
hashes
'
))
self
.
assertIn
(
'
sha256
'
,
event
.
hashes
)
...
...
@@ -71,23 +69,21 @@ class EventSigningTestCase(unittest.TestCase):
)
def
test_sign_message
(
self
):
builder
=
EventBuilder
(
{
'
content
'
:
{
'
body
'
:
"
Here is the message content
"
},
'
event_id
'
:
"
$0:domain
"
,
'
origin
'
:
"
domain
"
,
'
origin_server_ts
'
:
1000000
,
'
type
'
:
"
m.room.message
"
,
'
room_id
'
:
"
!r:domain
"
,
'
sender
'
:
"
@u:domain
"
,
'
signatures
'
:
{},
'
unsigned
'
:
{
'
age_ts
'
:
1000000
},
}
)
add_hashes_and_signatures
(
builder
,
HOSTNAME
,
self
.
signing_key
)
event
=
builder
.
build
()
event_dict
=
{
'
content
'
:
{
'
body
'
:
"
Here is the message content
"
},
'
event_id
'
:
"
$0:domain
"
,
'
origin
'
:
"
domain
"
,
'
origin_server_ts
'
:
1000000
,
'
type
'
:
"
m.room.message
"
,
'
room_id
'
:
"
!r:domain
"
,
'
sender
'
:
"
@u:domain
"
,
'
signatures
'
:
{},
'
unsigned
'
:
{
'
age_ts
'
:
1000000
},
}
add_hashes_and_signatures
(
event_dict
,
HOSTNAME
,
self
.
signing_key
)
event
=
FrozenEvent
(
event_dict
)
self
.
assertTrue
(
hasattr
(
event
,
'
hashes
'
))
self
.
assertIn
(
'
sha256
'
,
event
.
hashes
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment