-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
155 additions
and
24 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
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,12 @@ | ||
class TimeStampRequest: ... | ||
|
||
def create_timestamp_request( | ||
data: bytes, | ||
) -> TimeStampRequest: ... | ||
|
||
|
||
class TimeStampResponse: ... | ||
|
||
def parse_timestamp_response( | ||
data: bytes, | ||
) -> TimeStampResponse: ... |
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,26 +1,98 @@ | ||
"""Base implementation.""" | ||
|
||
from __future__ import annotations | ||
|
||
import enum | ||
|
||
from sigstore_tsp import _rust | ||
|
||
|
||
class HashAlgorithm(enum.Enum): | ||
"""Hash algorithms.""" | ||
|
||
SHA512 = "SHA512" | ||
|
||
|
||
_AllowedHashTypes = HashAlgorithm | ||
|
||
|
||
class TimestampRequestBuilder: | ||
def __init__(self, data: bytes| None = None, hash_algorithm: None = None, req_policy: None = None, nonce: int | None = None, cert_req : bool = False, extensions: None = None): | ||
self._data = data | ||
self._algorithm = hash_algorithm | ||
"""Timestamp Request Builder class.""" | ||
|
||
def __init__( | ||
self, | ||
data: bytes | None = None, | ||
hash_algorithm: _AllowedHashTypes | None = None, | ||
req_policy: None | int = None, | ||
cert_req: bool = False, # noqa: FBT001, FBT002 | ||
extensions: None = None, | ||
) -> None: | ||
"""Init method.""" | ||
self._data: bytes | None = data | ||
self._algorithm: _AllowedHashTypes | None = hash_algorithm | ||
self._req_policy = req_policy | ||
self._nonce = nonce | ||
self._cert_req = cert_req | ||
self._cert_req: bool = cert_req | ||
self._extensions = extensions | ||
|
||
def data(self, data: bytes) -> TimestampRequestBuilder: | ||
"""Set the data to be timestamped.""" | ||
if not data: | ||
raise ValueError("The data to timestamp cannot be empty.") | ||
msg = "The data to timestamp cannot be empty." | ||
raise ValueError(msg) | ||
if self._data is not None: | ||
raise ValueError("The data may only be set once.") | ||
msg = "The data may only be set once." | ||
raise ValueError(msg) | ||
return TimestampRequestBuilder( | ||
data, self._algorithm, self._req_policy, self._nonce, self._cert_req, self._extensions | ||
data, self._algorithm, self._req_policy, self._cert_req, self._extensions | ||
) | ||
|
||
def add_extension(self, extension: None) -> TimestampRequestBuilder: | ||
raise NotImplemented("Adding extensions is not yet supported.") | ||
def add_extension(self, _extension: None) -> TimestampRequestBuilder: | ||
"""Add an extension.""" | ||
msg = "Adding extensions is not yet supported." | ||
raise NotImplementedError(msg) | ||
|
||
def hash_algorithm(self, hash_algorihtm: _AllowedHashTypes) -> TimestampRequestBuilder: | ||
"""Set the Hash algorithm used.""" | ||
if hash_algorihtm not in HashAlgorithm: | ||
msg = f"{hash_algorihtm} is not a supported hash." | ||
raise TypeError(msg) | ||
|
||
return TimestampRequestBuilder( | ||
self._data, hash_algorihtm, self._req_policy, self._cert_req, self._extensions | ||
) | ||
|
||
def cert_request(self, *, cert_request: bool = False) -> TimestampRequestBuilder: | ||
"""Set the cert request field.""" | ||
if not isinstance(cert_request, bool): | ||
msg = "Cert request must be a boolean." | ||
raise TypeError(msg) | ||
|
||
return TimestampRequestBuilder( | ||
self._data, self._algorithm, self._req_policy, cert_request, self._extensions | ||
) | ||
|
||
def request_policy(self, request_policy: int) -> TimestampRequestBuilder: | ||
"""Set the request policy field.""" | ||
if not isinstance(request_policy, int): | ||
msg = "Request policy must be an integer." | ||
raise TypeError(msg) | ||
|
||
return TimestampRequestBuilder( | ||
self._data, self._algorithm, request_policy, self._cert_req, self._extensions | ||
) | ||
|
||
def build(self) -> _rust.TimeStampRequest: | ||
"""Build a TimestampRequest.""" | ||
if self._data is None: | ||
msg = "Data must be for a Timestamp Request." | ||
raise ValueError(msg) | ||
|
||
if self._algorithm is None: | ||
self._algorithm = HashAlgorithm.SHA512 | ||
|
||
return _rust.create_timestamp_request(self._data) | ||
|
||
|
||
def build(self): | ||
raise NotImplemented("Building is not yet supported.") | ||
def decode_timestamp_response(data: bytes) -> _rust.TimeStampResponse: | ||
"""Decode a Timestamp response.""" | ||
return _rust.parse_timestamp_response(data) |
Empty file.