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
47
48
49
50
use serde::de::DeserializeOwned;
use serde::ser::Serialize;

pub trait SignatureAlgorithm {
    type SignKey: Serialize + DeserializeOwned;
    type VerifyKey: Serialize + DeserializeOwned;
    const SIGNATURE_LENGTH: usize;
    const SIGN_KEY_LENGTH: usize;

    fn sign(plain: &[u8], sign_key: &Self::SignKey) -> Vec<u8>;

    fn verify(plain: &[u8], sign: &[u8], verify_key: &Self::VerifyKey) -> bool;

    fn from_bytes(bytes: &[u8]) -> Option<Self::SignKey>;

    fn to_bytes(sign_key: &Self::SignKey) -> Vec<u8>;
}

pub trait PublicKeyAlgorithm {
    type PublicKey: Serialize + DeserializeOwned;
    type SecretKey: Serialize + DeserializeOwned;
    const SECRET_KEY_LENGTH: usize;

    fn encrypt(plain: &[u8], public_key: &Self::PublicKey) -> Vec<u8>;

    fn decrypt(cipher: &[u8], secret_key: &Self::SecretKey) -> Vec<u8>;

    fn from_bytes(bytes: &[u8]) -> Option<Self::SecretKey>;

    fn to_bytes(secret_key: &Self::SecretKey) -> Vec<u8>;
}

pub trait SymmetricAlgorithm {
    type Key: Serialize + DeserializeOwned + Default;
    const KEY_LENGTH: usize;

    fn encrypt(plain: &[u8], session_key: &Self::Key) -> Vec<u8>;

    fn decrypt(cipher: &[u8], session_key: &Self::Key) -> Vec<u8>;

    fn from_bytes(bytes: &[u8]) -> Option<Self::Key>;

    fn to_bytes(&self) -> Vec<u8>;
}

pub trait HashAlgorithm {
    const HASH_LENGTH: usize;

    fn hash(data: &[u8]) -> Vec<u8>;
}