Only calling crypto_init once

This commit is contained in:
Dennis Schwerdel 2016-02-08 20:03:43 +01:00
parent 102fedd7ea
commit 23cbbda2b0
1 changed files with 8 additions and 3 deletions

View File

@ -4,6 +4,9 @@
use std::ptr; use std::ptr;
use std::ffi::CStr; use std::ffi::CStr;
use std::sync::{Once, ONCE_INIT};
static CRYPTO_INIT: Once = ONCE_INIT;
use libc::{size_t, c_char, c_ulonglong, c_int}; use libc::{size_t, c_char, c_ulonglong, c_int};
use aligned_alloc::{aligned_alloc, aligned_free}; use aligned_alloc::{aligned_alloc, aligned_free};
@ -147,9 +150,11 @@ fn inc_nonce_12(nonce: &mut [u8; 12]) {
impl Crypto { impl Crypto {
pub fn init() { pub fn init() {
CRYPTO_INIT.call_once(|| {
if unsafe { sodium_init() } != 0 { if unsafe { sodium_init() } != 0 {
fail!("Failed to initialize crypto library"); fail!("Failed to initialize crypto library");
} }
});
} }
pub fn sodium_version() -> String { pub fn sodium_version() -> String {