aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-28 21:07:18 +0100
committerRobin Krahl <robin.krahl@ireas.org>2020-01-28 21:07:18 +0100
commit3bdc550ce31aa9a8aaec13b4901e8517d34272be (patch)
tree41b4ecbb8d2bdd72aa448790176c56b768999149 /src/util.rs
parent817409140a8778215d2d65d614d3672166fff576 (diff)
parentebe75339a3fb29c51ea22a0bb25fd113b15759f1 (diff)
downloadnitrokey-rs-3bdc550ce31aa9a8aaec13b4901e8517d34272be.tar.gz
nitrokey-rs-3bdc550ce31aa9a8aaec13b4901e8517d34272be.tar.bz2
Merge branch 'hotfix-0.5.2'
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 5a56c55..a0d0d1b 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -75,10 +75,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> {