-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
latentvector
committed
Aug 19, 2024
1 parent
9585b93
commit ec1fc19
Showing
2 changed files
with
43 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
import commune as c | ||
from cryptography.hazmat.primitives import hashes | ||
from cryptography.hazmat.primitives.asymmetric import dh | ||
from cryptography.hazmat.primitives.kdf.hkdf import HKDF | ||
print('FAM') | ||
|
||
# Generate some parameters. These can be reused. | ||
parameters = dh.generate_parameters(generator=2, key_size=2048) | ||
# Generate a private key for use in the exchange. | ||
server_private_key = parameters.generate_private_key() | ||
# In a real handshake the peer is a remote client. For this | ||
# example we'll generate another local private key though. Note that in | ||
# a DH handshake both peers must agree on a common set of parameters. | ||
peer_private_key = parameters.generate_private_key() | ||
shared_key = server_private_key.exchange(peer_private_key.public_key()) | ||
# Perform key derivation. | ||
derived_key = HKDF( | ||
algorithm=hashes.SHA256(), | ||
length=32, | ||
salt=None, | ||
info=b'handshake data', | ||
).derive(shared_key) | ||
# And now we can demonstrate that the handshake performed in the | ||
# opposite direction gives the same final value | ||
same_shared_key = peer_private_key.exchange( | ||
server_private_key.public_key() | ||
) | ||
same_derived_key = HKDF( | ||
algorithm=hashes.SHA256(), | ||
length=32, | ||
salt=None, | ||
info=b'handshake data', | ||
).derive(same_shared_key) | ||
derived_key == same_derived_key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters