diff --git a/Cargo.toml b/Cargo.toml index c4f170a..fb15a6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,12 @@ file = "1.1.2" read = "0.1.1" futures = "0.3" futures-util = "0.3" -futures-macro = "0.3" \ No newline at end of file +futures-macro = "0.3"signature = "2.2.0" +rand = "0.8.5" +pkcs8 = "0.10.2" +openssl = "0.10" +sha3 = "0.9.0" +ed25519-dalek = "1.0.1" +rand = "0.8.5" +rand_chacha = "0.3.1" +hex = "0.4" diff --git a/src/crypto.rs b/src/crypto.rs new file mode 100644 index 0000000..46cbafb --- /dev/null +++ b/src/crypto.rs @@ -0,0 +1,31 @@ + #[allow(unused_imports)] + /// OpenSSL is a robust, open-source implementation of the SSL (Secure Sockets Layer) + /// and TLS (Transport Layer Security) protocols. It provides a toolkit for the + /// implementation of secure communication over a computer network. +use openssl::rsa::Rsa; +use openssl::pkey::PKey; +use openssl::pkey::Private; +use sha3::{Digest, Sha3_256}; +use std::convert::TryInto; + +/// Hash the provided data using SHA3-256, i.e., Secure Hash Algorithm 3 using 256 bits +/// @param data Data to be hashed using SHA3-256 +/// @return Hashed data +pub fn hash_data(data: &str) -> String { + let mut hasher = Sha3_256::new(); + hasher.update(data.as_bytes()); + hex::encode(hasher.finalize()) +} + +/// Generates a cryptographic key pair using RSA +/// RSA is a public-key cryptosystem, meaning it uses a pair of +/// keys: a public key for encryption and a private key for decryption +/// @param key-size +/// @return tuple object containing the public and private keys +pub fn generate_keypair(key_size: usize) -> (Vec, PKey) { + //let rsa = Rsa::generate(key_size).unwrap(); + let rsa = Rsa::generate(key_size.try_into().unwrap()).unwrap(); + let pkey = PKey::from_rsa(rsa).unwrap(); + let pub_key: Vec = pkey.public_key_to_pem().unwrap(); + (pub_key, pkey) +} diff --git a/src/lib.rs b/src/lib.rs index 9e4ddb9..b2d8b81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ pub mod resdb; pub mod transaction; pub mod blocks; +pub mod crypto; /// Re-export ResDB from the resdb module for convenient use. pub use resdb::ResDB; diff --git a/src/resdb.rs b/src/resdb.rs index d41cca2..90ba663 100644 --- a/src/resdb.rs +++ b/src/resdb.rs @@ -7,6 +7,12 @@ // Imports use crate::transaction; use crate::blocks; +use crate::crypto; + +use openssl::rsa::Rsa; +use openssl::pkey::PKey; +use openssl::pkey::Private; + use std::collections::HashMap; use serde_json::Value; @@ -169,4 +175,20 @@ impl ResDB { { blocks::get_blocks_by_range_map(api_url, range_begin, range_end, map).await } -} \ No newline at end of file + + /** APIs provided to create public/private key pairs and Hashing **/ + pub fn generate_keypair(key_size: usize) -> (Vec, PKey) + //pub fn generate_keypair() -> (Vec, PKey) + where + { + crypto::generate_keypair(key_size: usize) + } + + /** APIs provided to create hashed data using SHA3-256 **/ + pub fn hash_data(data: &str) -> String + where + { + crypto::hash_data(data: &str) + } + +} diff --git a/test/src/main.rs b/test/src/main.rs index f02f588..e11b94a 100644 --- a/test/src/main.rs +++ b/test/src/main.rs @@ -231,10 +231,26 @@ async fn test_blocks_api_map() { } } +fn test_crypto() { + let keypair: ResDB::generate_keypair(); + println!("{:?}",) +} + fn main(){ - test_transaction_api(); - test_transaction_api_map(); - test_blocks_api(); - test_blocks_api_map() -} \ No newline at end of file + // test_transaction_api(); + // test_transaction_api_map(); + // test_blocks_api(); + // test_blocks_api_map(); + test_crypto() + // Testing code for crypto module + let keypair = crypto::generate_keypair(2048); + println!("Public Key: {:?}", keypair.0); + // Handle the private key securely + // Printing here for testing only + println!("Private Key: [hidden]"); + + let data = "Hello, World!"; + let hashed_data = crypto::hash_data(data); + println!("Hashed Data: {}", hashed_data); +}