diff options
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/util.rs b/src/util.rs index 1b52c3d..08946d6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -76,10 +76,16 @@ pub fn get_last_error() -> Error { }) } -pub fn generate_password(length: usize) -> Result<Vec<u8>, Error> { - let mut data = vec![0u8; length]; - OsRng.fill_bytes(&mut data[..]); - Ok(data) +pub fn generate_password(length: usize) -> Result<CString, Error> { + loop { + // Randomly generate a password until we get a string *without* null bytes. Otherwise + // the string would be cut off prematurely due to null-termination in C. + let mut data = vec![0u8; length]; + OsRng.fill_bytes(&mut data[..]); + if let Ok(s) = CString::new(data) { + return Ok(s); + } + } } pub fn get_cstring<T: Into<Vec<u8>>>(s: T) -> Result<CString, Error> { |