-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ed25519 signing (not used for anything yet)
- Loading branch information
Showing
5 changed files
with
90 additions
and
34 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
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey | ||
from typing import List | ||
|
||
class SigningKeys: | ||
def __init__(self, priv_bytes: bytes, pub_bytes: bytes|List[bytes]): | ||
self.priv_key = Ed25519PrivateKey.from_private_bytes(priv_bytes) | ||
if isinstance(pub_bytes, bytes): | ||
pub_bytes = [pub_bytes] | ||
self.pub_keys = [ | ||
Ed25519PublicKey.from_public_bytes(_pub_bytes) | ||
for _pub_bytes in pub_bytes | ||
] | ||
|
||
def sign(self, data: bytes) -> bytes: | ||
return self.priv_key.sign(data) | ||
|
||
def validate(self, signature: bytes, data: bytes) -> bool: | ||
for pub_key in self.pub_keys: | ||
try: | ||
pub_key.verify(signature, data) | ||
except: | ||
continue | ||
else: | ||
return True | ||
|
||
return False | ||
|
||
def rotate(self) -> tuple[bytes, bytes]: | ||
new_priv = Ed25519PrivateKey.generate() | ||
new_pub = new_priv.public_key() | ||
|
||
self.priv_key = new_priv | ||
self.pub_keys.append(new_pub) | ||
|
||
return new_priv.private_bytes_raw(), new_pub.public_bytes_raw() |
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,6 +1,4 @@ | ||
from typing import Literal | ||
from datetime import datetime | ||
import time | ||
import sys | ||
import traceback | ||
|
||
|