#crypt #posix #binding #struct #libcrypt #encryptions

libcrypt-rs

POSIX crypt 库 (libcrypt) 的 Rust 绑定

1 个不稳定版本

0.1.2 2022年9月24日
0.1.1 2022年9月24日
0.1.0 2022年9月24日

#6 in #crypt

自定义许可协议

9KB
123

libcrypt-rs - POSIX crypt 库 (libcrypt) 的 Rust 绑定

如何使用它

将以下内容添加到您的 Cargo.toml 中的 dependencies 部分

libcrypt-rs = "0.1.2"

文档

cargo 提供了生成文档的酷炫功能

  • 运行
cargo doc
  • 在网页浏览器中打开 target/doc/libcrypt_rs/index.html

示例

use libcrypt_rs::{Crypt, Encryptions};

fn main() {
	let mut crypt_engine = Crypt::new();

	crypt_engine.gen_salt(Encryptions::Sha256).expect("Salt generating failed");
	crypt_engine.encrypt("1234".to_string()).expect("Encryption failed");

	println!("Encrypted data: {}", crypt_engine.encrypted);
}

注意事项

  • 不要同时加密多个数据,否则可能会造成段错误。

lib.rs:

POSIX crypt 库 (libcrypt) 的 Rust 绑定

示例

// Import Crypt struct and Encryptions enum.
use libcrypt_rs::{Crypt, Encryptions};

fn main() {
	// Create new instance of Crypt object.
	let mut engine = Crypt::new();

	// Generate new encryption salt for sha256crypt encryption algorithm.
	engine.gen_salt(Encryptions::Sha256).expect("Salt generation failed");
	
	// Encrypt "1234" string.
	engine.encrypt("1234".to_string()).expect("Encryption failed");
	
	// Encrypted data is stored in engine.encrypted. Let's print it to stdout.
	println!("Encrypted data: '{}'", engine.encrypted);
}

没有运行时依赖