mas_jose/jwa/
signature.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use signature::SignatureEncoding as _;
8
9#[derive(Debug, Clone)]
10pub struct Signature {
11    bytes: Box<[u8]>,
12}
13
14impl From<Signature> for Box<[u8]> {
15    fn from(val: Signature) -> Self {
16        val.bytes
17    }
18}
19
20impl<'a> From<&'a [u8]> for Signature {
21    fn from(value: &'a [u8]) -> Self {
22        Self {
23            bytes: value.into(),
24        }
25    }
26}
27
28impl signature::SignatureEncoding for Signature {
29    type Repr = Box<[u8]>;
30}
31
32impl Signature {
33    pub fn new(bytes: Vec<u8>) -> Self {
34        Self {
35            bytes: bytes.into(),
36        }
37    }
38
39    pub fn from_signature<S>(signature: &S) -> Self
40    where
41        S: signature::SignatureEncoding,
42    {
43        Self {
44            bytes: signature.to_vec().into(),
45        }
46    }
47
48    pub fn to_signature<S>(&self) -> Result<S, signature::Error>
49    where
50        S: signature::SignatureEncoding,
51    {
52        S::try_from(&self.to_bytes()).map_err(|_| signature::Error::default())
53    }
54}