indy_crypto/utils/
mod.rs

1#[macro_use]
2pub mod ctypes;
3pub mod commitment;
4pub mod rsa;
5#[macro_use]
6pub mod logger;
7
8use bn::BigNumber;
9use errors::prelude::*;
10
11pub fn get_hash_as_int(nums: &Vec<Vec<u8>>) -> IndyCryptoResult<BigNumber> {
12    trace!("Helpers::get_hash_as_int: >>> nums: {:?}", nums);
13
14    let hash = BigNumber::from_bytes(&BigNumber::hash_array(&nums)?);
15
16    trace!("Helpers::get_hash_as_int: <<< hash: {:?}", hash);
17
18    hash
19}
20
21pub fn clone_option_bignum(b: &Option<BigNumber>) -> IndyCryptoResult<Option<BigNumber>> {
22    match *b {
23        Some(ref bn) => Ok(Some(bn.clone()?)),
24        None => Ok(None)
25    }
26}
27
28macro_rules! hashset {
29    ( $( $x:expr ),* ) => {
30        {
31            let mut set = ::std::collections::HashSet::new();
32            $(
33                set.insert($x);
34            )*
35            set
36        }
37    }
38}
39
40macro_rules! hashmap {
41    ($( $key: expr => $val: expr ),*) => {
42        {
43            let mut map = ::std::collections::HashMap::new();
44            $(
45                map.insert($key, $val);
46            )*
47            map
48        }
49    }
50}
51
52macro_rules! btreeset {
53    ( $( $x:expr ),* ) => {
54        {
55            let mut set = ::std::collections::BTreeSet::new();
56            $(
57                set.insert($x);
58            )*
59            set
60        }
61    }
62}
63
64macro_rules! btreemap {
65    ($( $key: expr => $val: expr ),*) => {
66        {
67            let mut map = ::std::collections::BTreeMap::new();
68            $(
69                map.insert($key, $val);
70            )*
71            map
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn get_hash_as_int_works() {
82        let mut nums = vec![
83            BigNumber::from_hex("ff9d2eedfee9cffd9ef6dbffedff3fcbef4caecb9bffe79bfa94d3fdf6abfbff").unwrap().to_bytes().unwrap(),
84            BigNumber::from_hex("ff9d2eedfee9cffd9ef6dbffedff3fcbef4caecb9bffe79bfa9168615ccbc546").unwrap().to_bytes().unwrap()
85        ];
86        let res = get_hash_as_int(&mut nums);
87
88        assert!(res.is_ok());
89        assert_eq!("2C2566C22E04AB3F18B3BA693823175002F10F400811363D26BBB33633AC8BAD", res.unwrap().to_hex().unwrap());
90    }
91}