Skip to main content

laron_crypto/
lib.rs

1// This file is part of the laron-crypto
2//
3// Copyright 2023 Ade M Ramdani
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18extern crate core;
19
20mod private_key;
21pub use private_key::PrivateKey;
22
23mod public_key;
24pub use public_key::PublicKey;
25
26pub mod aes;
27pub mod k256;
28pub mod totp;
29
30pub mod error {
31
32    /// Error type for this crate.
33    #[derive(Debug, PartialEq, Eq)]
34    pub enum Error {
35        LengthError(String),
36        HexError(String),
37        DecryptError(String),
38        EncryptError(String),
39        RecoveryError(String),
40        InvalidSignature,
41        PrivateKeyError,
42        InvalidPublicKey,
43    }
44
45    impl std::fmt::Display for Error {
46        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
47            match self {
48                Error::LengthError(s) => write!(f, "LengthError: {}", s),
49                Error::HexError(s) => write!(f, "HexError: {}", s),
50                Error::DecryptError(s) => write!(f, "DecryptError: {}", s),
51                Error::EncryptError(s) => write!(f, "EncryptError: {}", s),
52                Error::RecoveryError(s) => write!(f, "RecoveryError: {}", s),
53                Error::InvalidSignature => write!(f, "InvalidSignature"),
54                Error::PrivateKeyError => write!(f, "PrivateKeyError"),
55                Error::InvalidPublicKey => write!(f, "InvalidPublicKey"),
56            }
57        }
58    }
59
60    impl std::error::Error for Error {}
61
62    impl From<hex::FromHexError> for Error {
63        fn from(value: hex::FromHexError) -> Self {
64            Error::HexError(value.to_string())
65        }
66    }
67
68    /// Result type for this crate.
69    pub type Result<T> = std::result::Result<T, Error>;
70}