aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-28 21:23:08 +0100
committerRobin Krahl <robin.krahl@ireas.org>2020-01-28 21:23:13 +0100
commit395ace0131ef6cd655a222734f2cc8d037395a3b (patch)
tree5ad0947a2d183f5f0b16be4aca885397b2613718 /src/util.rs
parent24eebcdaaa32d55bf49d069d8320be5dbd6fdab9 (diff)
parentebe75339a3fb29c51ea22a0bb25fd113b15759f1 (diff)
downloadnitrokey-rs-395ace0131ef6cd655a222734f2cc8d037395a3b.tar.gz
nitrokey-rs-395ace0131ef6cd655a222734f2cc8d037395a3b.tar.bz2
Merge branch 'hotfix-0.5.2' into next
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs14
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> {