pub struct TaskSigner { /* private fields */ }Expand description
Signs and verifies task messages with a shared secret using HMAC-SHA256.
A TaskSigner holds the secret key. Producers call TaskSigner::sign to
obtain a TaskSignature; consumers call TaskSigner::verify (or
TaskSigner::verify_optional) to authenticate a received message.
Implementations§
Source§impl TaskSigner
impl TaskSigner
Sourcepub fn new(key: impl AsRef<[u8]>) -> Self
pub fn new(key: impl AsRef<[u8]>) -> Self
Create a signer from a shared secret key.
Any key length is accepted (HMAC handles short and long keys), but for HMAC-SHA256 a key of at least 32 bytes of entropy is recommended.
Sourcepub fn mac(&self, fields: &SignedFields) -> [u8; 32]
pub fn mac(&self, fields: &SignedFields) -> [u8; 32]
Compute the raw MAC tag bytes over the canonical serialization.
Sourcepub fn sign(&self, fields: &SignedFields) -> TaskSignature
pub fn sign(&self, fields: &SignedFields) -> TaskSignature
Sign a set of task fields, returning a serializable TaskSignature.
Sourcepub fn verify(
&self,
fields: &SignedFields,
signature: &TaskSignature,
) -> Result<(), SignatureError>
pub fn verify( &self, fields: &SignedFields, signature: &TaskSignature, ) -> Result<(), SignatureError>
Verify a task message against its signature.
The comparison is constant-time. Any tampering with the signed fields, a wrong key, or a corrupted/forged tag yields an error.
§Errors
SignatureError::UnsupportedAlgorithmif the signature names an algorithm other than HMAC-SHA256.SignatureError::MalformedSignatureif the tag is not valid hex or has the wrong length.SignatureError::Mismatchif the recomputed MAC does not match.
Sourcepub fn verify_optional(
&self,
fields: &SignedFields,
signature: Option<&TaskSignature>,
) -> Result<(), SignatureError>
pub fn verify_optional( &self, fields: &SignedFields, signature: Option<&TaskSignature>, ) -> Result<(), SignatureError>
Verify a message whose signature may be absent.
When signature is None this returns
SignatureError::MissingSignature, which lets a caller distinguish an
unsigned message from a badly-signed one and reject both.
§Errors
Returns SignatureError::MissingSignature when no signature is
supplied, or any error from TaskSigner::verify otherwise.
Sourcepub fn is_valid(&self, fields: &SignedFields, signature: &TaskSignature) -> bool
pub fn is_valid(&self, fields: &SignedFields, signature: &TaskSignature) -> bool
Convenience predicate: true iff the message verifies.