1 个不稳定版本

0.1.0 2023年2月13日

8#c-strings

Download history 20/week @ 2024-04-01 3/week @ 2024-06-03

57 每月下载量

MIT 许可证

98KB
2.5K SLoC

C 2.5K SLoC // 0.1% comments Rust 55 SLoC // 0.0% comments

crypt3-sys

静态链接 musl 中本地拉取的 crypt(3) 的绑定。

crate.io

use std::ffi::{CString, CStr};

fn main() {
    let password = "mypassword";
    let sha256_setting = "$5$";
    let salt = "rDxsrps6AeTwJLRK";
    let settings = format!("{sha256_setting}{salt}");

    let mut output = vec![0_i8; 256];

    let ret_str = unsafe {
        let csetting = CString::new(settings).unwrap();
        let cpassword = CString::new(password).unwrap();

        let _ret = crypt3_sys::crypt_r(cpassword.as_ptr(), csetting.as_ptr(), output.as_mut_ptr());

        let ret_cstr = CStr::from_ptr(output.as_ptr());
        ret_cstr.to_str().unwrap()
    };

    // mkpasswd -m sha256crypt "mypassword"
    // $5$rDxsrps6AeTwJLRK$CHafsXkpg6bi5Z.kdTYhlWzmhqe9Q.RRPm0LWi/bckC

    let ret_assumed = format!(
        "{}{}${}",
        sha256_setting, salt, "CHafsXkpg6bi5Z.kdTYhlWzmhqe9Q.RRPm0LWi/bckC"
    );
    assert!(ret_str == ret_assumed);

    println!("ret_str {ret_str:?}");
}

无运行时依赖

~0–2MB
~40K SLoC