rust_rsm/alg/
hash_alg.rs

1#![allow(non_camel_case_types)]
2#![allow(non_snake_case)]
3#![allow(non_upper_case_globals)]
4
5use rand::rngs::OsRng;
6use rand::RngCore;
7use sha2::{Digest, Sha256};
8
9//根据三个数字,计算出来一个128bit的Hash值
10pub fn hash_3value_128(v1: &[u8], v2: &[u8], v3: &[u8]) -> [u8; 16] {
11    let mut buf: Vec<u8> = Vec::new();
12    buf.extend_from_slice(v1);
13    buf.push(0x20);
14    buf.extend_from_slice(v2);
15    buf.push(0x20);
16    buf.extend_from_slice(v3);
17
18    let mut hasher = Sha256::new();
19    hasher.update(&buf.as_slice());
20    let res = hasher.finalize();
21    let mut hv: [u8; 16] = [0; 16];
22    unsafe {
23        std::ptr::copy(&res[0] as *const u8, &mut hv[0] as *mut u8, 16);
24    }
25    return hv;
26}
27
28//生成一个128bit的随机数
29pub fn get_rand_128() -> [u8; 16] {
30    let mut nonce: [u8; 16] = [0; 16];    
31    OsRng.fill_bytes(&mut nonce[0..16]);
32    return nonce;
33}