Rust实现字符串sha1、sha256、sha512加密


本文将在Rust语言中使用sha1、sha256、sha512等安全散列算法对字符串进行加密;

首先,在Cargo.toml文件中添加依赖:

[dependencies]
rust-crypto = "0.2.36"

sha1加密

use crypto::sha1::Sha1;
use crypto::digest::Digest;

fn main(){

    let mut hasher = Sha1::new();

    let text = String::from("123456");

    hasher.input_str(&text);
    
    println!("{} => {}",text,hasher.result_str())
    
}

程序输出:

123456 => 7c4a8d09ca3762af61e59520943dc26494f8941b

sha256加密

use crypto::sha2::Sha256;
use crypto::digest::Digest;

fn main(){

    let mut hasher = Sha256::new();

    let text = String::from("123456");

    hasher.input_str(&text);
    
    println!("{} => {}",text,hasher.result_str())
    
}

程序输出:

123456 => 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92

sha512加密

将前文中的Sha256替换成Sha512即可。

如需转载,请注明出处;本文地址:https://www.perfcode.com/p/rust-implements-string-sha1-sha2-encryption.html