-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
edf02aa
commit 69bb62d
Showing
5 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters