Created
April 3, 2018 08:34
-
-
Save rust-play/6509bead4c6e13345f6535faf2db06bf to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or 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
extern crate openssl; | |
use openssl::rsa::Rsa; | |
use openssl::pkey::PKey; | |
use std::str; | |
use openssl::x509::{X509, X509Name}; | |
use openssl::hash::MessageDigest; | |
use openssl::nid::Nid; | |
fn main() { | |
let rsa = Rsa::generate(2048).unwrap(); | |
let pkey = PKey::from_rsa(rsa).unwrap(); | |
let mut name = X509Name::builder().unwrap(); | |
name.append_entry_by_nid(Nid::COMMONNAME, "foobar.com").unwrap(); | |
let name = name.build(); | |
let mut builder = X509::builder().unwrap(); | |
builder.set_version(2).unwrap(); | |
builder.set_subject_name(&name).unwrap(); | |
builder.set_issuer_name(&name).unwrap(); | |
builder.set_pubkey(&pkey).unwrap(); | |
builder.sign(&pkey, MessageDigest::sha256()).unwrap(); | |
let certificate: X509 = builder.build(); | |
let pub_key: Vec<u8> = pkey.public_key_to_pem().unwrap(); | |
println!("{}", str::from_utf8(pub_key.as_slice()).unwrap()); | |
let pri_key: Vec<u8> = pkey.private_key_to_pem_pkcs8().unwrap(); | |
println!("{}", str::from_utf8(pri_key.as_slice()).unwrap()); | |
let pem: Vec<u8> = certificate.to_pem().unwrap(); | |
println!("{}", str::from_utf8(pem.as_slice()).unwrap()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment