Skip to main content

rc_crypto/
signer.rs

1// Copyright 2026-Present Datadog, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! An abstract method of obtaining a signature.
16
17use std::sync::Arc;
18
19use crate::{Signature, keys::PublicKey};
20
21/// A [`Signer`] provides the ability to generate a [`Signature`] for provided
22/// payload data.
23///
24/// This abstraction decouples the caller from the underlying key type and
25/// storage.
26///
27/// Note that the key material used by a [`Signer`] instance MUST NOT change for
28/// the lifetime of the [`Signer`] instance.
29pub trait Signer: std::fmt::Debug + Send + Sync {
30    /// Sign `data` with this private key.
31    ///
32    /// Signatures are non-deterministic and rely on randomness on the host.
33    fn sign(&self, data: &[u8]) -> Signature;
34
35    /// Obtain the [`PublicKey`] for this [`Signer`].
36    ///
37    /// Invariant: from the caller's perspective, a single [`Signer`] instance
38    /// always returns the same [`PublicKey`] (key material).
39    fn public_key(&self) -> PublicKey<'_>;
40}
41
42impl<T> Signer for Arc<T>
43where
44    T: Signer,
45{
46    fn sign(&self, data: &[u8]) -> Signature {
47        T::sign(self, data)
48    }
49
50    fn public_key(&self) -> PublicKey<'_> {
51        T::public_key(self)
52    }
53}