Skip to content

Commit

Permalink
Crypto (#6)
Browse files Browse the repository at this point in the history
* added crypto.rs

* Update Cargo.toml

Added dependencies for crypto module.

* Update crypto.rs

Revamped for complete functionality along with extensive comments.  Also following:

Added key_size parameter in generate_keypair(key_size: usize) function
Created hash data using SHA3-256

* Update resdb.rs

Updated to add key_size 
/** APIs provided to create public/private key pairs and Hashing **/
    pub fn generate_keypair(key_size: usize) -> (Vec<u8>, PKey<Private>)
    //pub fn generate_keypair() -> (Vec<u8>, PKey<Private>)
    where
    {
        crypto::generate_keypair(key_size: usize)
    }
    
Added
     /** APIs provided to create hashed data using SHA3-256 **/
    pub fn hash_data(data: &str) -> String
    where
    {
        crypto::hash_data(data: &str)
    }

* Update main.rs

// Testing code for crypto module in main
    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);

* Changed comments on crypto.rs

---------

Co-authored-by: dharora <[email protected]>
  • Loading branch information
dhruvsangamwar and dharora authored Dec 8, 2023
1 parent edf02aa commit 69bb62d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ file = "1.1.2"
read = "0.1.1"
futures = "0.3"
futures-util = "0.3"
futures-macro = "0.3"
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"
31 changes: 31 additions & 0 deletions src/crypto.rs
Original file line number Diff line number Diff line change
@@ -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<u8>, PKey<Private>) {
//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<u8> = pkey.public_key_to_pem().unwrap();
(pub_key, pkey)
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
24 changes: 23 additions & 1 deletion src/resdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -169,4 +175,20 @@ impl ResDB {
{
blocks::get_blocks_by_range_map(api_url, range_begin, range_end, map).await
}
}

/** APIs provided to create public/private key pairs and Hashing **/
pub fn generate_keypair(key_size: usize) -> (Vec<u8>, PKey<Private>)
//pub fn generate_keypair() -> (Vec<u8>, PKey<Private>)
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)
}

}
26 changes: 21 additions & 5 deletions test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
// 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);
}

0 comments on commit 69bb62d

Please sign in to comment.