1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
mod asymmetric;
mod symmetric;

use crate::error::HttpSigResult;

pub use asymmetric::{PublicKey, SecretKey};
pub use symmetric::SharedKey;

/// Algorithm names
pub enum AlgorithmName {
  HmacSha256,
  EcdsaP256Sha256,
  EcdsaP384Sha384,
  Ed25519,
}

impl AlgorithmName {
  pub fn as_str(&self) -> &str {
    match self {
      AlgorithmName::HmacSha256 => "hmac-sha256",
      AlgorithmName::EcdsaP256Sha256 => "ecdsa-p256-sha256",
      AlgorithmName::EcdsaP384Sha384 => "ecdsa-p384-sha384",
      AlgorithmName::Ed25519 => "ed25519",
    }
  }
}

impl std::fmt::Display for AlgorithmName {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.as_str())
  }
}

/// SigningKey trait
pub trait SigningKey {
  fn sign(&self, data: &[u8]) -> HttpSigResult<Vec<u8>>;
  fn key_id(&self) -> String;
  fn alg(&self) -> AlgorithmName;
}

/// VerifyingKey trait
pub trait VerifyingKey {
  fn verify(&self, data: &[u8], signature: &[u8]) -> HttpSigResult<()>;
  fn key_id(&self) -> String;
  fn alg(&self) -> AlgorithmName;
}