Rust
Rust
Generate DSA SSH Key
See more SSH Key Examples
Generates a DSA SSH key and saves to various public and private key file formats (OpenSSH and PuTTY).Chilkat Rust Downloads
let key = chilkat::SshKey::new();
// numBits should be a minimum of 1024 and
// a multiple of 64. Typical values are
// 1024 or 2048.
let num_bits = 2048;
if key.generate_dsa_key(num_bits).is_err() {
println!("Invalid numBits passed to DSA key generation method.");
return;
}
// Note: Generating a public/private key pair is CPU intensive
// and may take a short amount of time (more than few seconds,
// but less than a minute).
let mut exported_key = String::new();
let mut export_encrypted = false;
// Export the DSA private key to OpenSSH, PuTTY, and XML and save.
export_encrypted = false;
exported_key = key.to_open_ssh_private_key(export_encrypted).unwrap_or_default();
// Chilkat provides a SaveText method for convenience...
let _ = key.save_text(&exported_key, "privkey_openssh_unencrypted.pem").is_ok();
// Export with encryption to OpenSSH private key format:
key.set_password("secret");
export_encrypted = true;
exported_key = key.to_open_ssh_private_key(export_encrypted).unwrap_or_default();
let _ = key.save_text(&exported_key, "privkey_openssh_encrypted.pem").is_ok();
// Export the DSA private key to unencrypted PuTTY format:
export_encrypted = false;
exported_key = key.to_putty_private_key(export_encrypted).unwrap_or_default();
let _ = key.save_text(&exported_key, "privkey_putty_unencrypted.ppk").is_ok();
// Export the DSA private key to encrypted PuTTY format:
key.set_password("secret");
export_encrypted = true;
exported_key = key.to_putty_private_key(export_encrypted).unwrap_or_default();
let _ = key.save_text(&exported_key, "privkey_putty_encrypted.ppk").is_ok();
// Export private key to XML:
exported_key = key.to_xml().unwrap_or_default();
let _ = key.save_text(&exported_key, "privkey.xml").is_ok();
// ----------------------------------------------------
// Now for the public key....
// ----------------------------------------------------
// The Secure Shell (SSH) Public Key File Format
// is documented in RFC 4716.
exported_key = key.to_rfc4716_public_key().unwrap_or_default();
let _ = key.save_text(&exported_key, "pubkey_rfc4716.pub").is_ok();
// OpenSSH has a separate public-key file format, which
// is also supported by Chilkat SshKey:
exported_key = key.to_open_ssh_public_key().unwrap_or_default();
let _ = key.save_text(&exported_key, "pubkey_openSsh.pub").is_ok();
println!("Finished.");