cfx_crypto/
lib.rs

1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3//
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17// Copyright 2020 Parity Technologies
18//
19// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
20// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
21// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
22// option. This file may not be copied, modified, or distributed
23// except according to those terms.
24
25pub mod crypto;
26
27/// Trait for secret key types used in cryptographic operations.
28pub trait SecretKey: AsRef<[u8]> {
29    /// Create a secret key from an unsafe slice (may not be validated).
30    /// Returns an error if the slice is invalid.
31    fn from_unsafe_slice(bytes: &[u8]) -> Result<Self, crypto::Error>
32    where Self: Sized;
33}
34
35/// Trait for keypair types that can generate random keypairs.
36pub trait KeyPair {
37    type Secret: SecretKey;
38    type Public: AsRef<[u8]>;
39
40    /// Get the secret key.
41    fn secret(&self) -> &Self::Secret;
42    /// Get the public key.
43    fn public(&self) -> &Self::Public;
44}
45
46/// Trait for generating random keypairs.
47pub trait RandomKeyPairGenerator {
48    type KeyPair: KeyPair;
49    type Error;
50
51    /// Generate a random keypair.
52    fn generate(&mut self) -> Result<Self::KeyPair, Self::Error>;
53}