| 172 | } |
| 173 | |
| 174 | pub fn symmetric_crypt(data: &[u8], encrypt: bool) -> Result<Vec<u8>, ()> { |
| 175 | use sodiumoxide::crypto::secretbox; |
| 176 | use std::convert::TryInto; |
| 177 | |
| 178 | let mut keybuf = crate::get_uuid(); |
| 179 | keybuf.resize(secretbox::KEYBYTES, 0); |
| 180 | let key = secretbox::Key(keybuf.try_into().map_err(|_| ())?); |
| 181 | let nonce = secretbox::Nonce([0; secretbox::NONCEBYTES]); |
| 182 | |
| 183 | if encrypt { |
| 184 | Ok(secretbox::seal(data, &nonce, &key)) |
| 185 | } else { |
| 186 | secretbox::open(data, &nonce, &key) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | mod test { |
| 191 | |