diff options
| author | Ellie Huxtable <e@elm.sh> | 2021-04-21 18:13:51 +0100 |
|---|---|---|
| committer | Ellie Huxtable <e@elm.sh> | 2021-04-21 21:26:44 +0100 |
| commit | 4a50ce366639ca9dac7324d6a47d6a0e6c7fccdf (patch) | |
| tree | 7ffd8848f675e1377f750cc0757768d074a5ac05 /atuin-client/src/encryption.rs | |
| parent | Bump rusqlite from 0.25.0 to 0.25.1 (#35) (diff) | |
| download | atuin-4a50ce366639ca9dac7324d6a47d6a0e6c7fccdf.zip | |
Bugfixes, show time ago, perf improvements
Also allow unique listing and more ergonomic cwd usage
Diffstat (limited to 'atuin-client/src/encryption.rs')
| -rw-r--r-- | atuin-client/src/encryption.rs | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/atuin-client/src/encryption.rs b/atuin-client/src/encryption.rs index 37153f94..19b773ab 100644 --- a/atuin-client/src/encryption.rs +++ b/atuin-client/src/encryption.rs @@ -29,20 +29,51 @@ pub fn load_key(settings: &Settings) -> Result<secretbox::Key> { let path = settings.key_path.as_str(); if PathBuf::from(path).exists() { - let bytes = std::fs::read(path)?; - let key: secretbox::Key = rmp_serde::from_read_ref(&bytes)?; + let key = std::fs::read_to_string(path)?; + let key = decode_key(key)?; Ok(key) } else { let key = secretbox::gen_key(); - let buf = rmp_serde::to_vec(&key)?; + let encoded = encode_key(key.clone())?; let mut file = File::create(path)?; - file.write_all(&buf)?; + file.write_all(encoded.as_bytes())?; Ok(key) } } +pub fn load_encoded_key(settings: &Settings) -> Result<String> { + let path = settings.key_path.as_str(); + + if PathBuf::from(path).exists() { + let key = std::fs::read_to_string(path)?; + Ok(key) + } else { + let key = secretbox::gen_key(); + let encoded = encode_key(key)?; + + let mut file = File::create(path)?; + file.write_all(encoded.as_bytes())?; + + Ok(encoded) + } +} + +pub fn encode_key(key: secretbox::Key) -> Result<String> { + let buf = rmp_serde::to_vec(&key)?; + let buf = base64::encode(buf); + + Ok(buf) +} + +pub fn decode_key(key: String) -> Result<secretbox::Key> { + let buf = base64::decode(key)?; + let buf: secretbox::Key = rmp_serde::from_read_ref(&buf)?; + + Ok(buf) +} + pub fn encrypt(history: &History, key: &secretbox::Key) -> Result<EncryptedHistory> { // serialize with msgpack let buf = rmp_serde::to_vec(history)?; |
