feat: generate AGE keys

This commit is contained in:
EETagent 2022-10-28 15:24:07 +02:00
parent 3abf84e163
commit 2ae22c7ec7
2 changed files with 17 additions and 0 deletions

View file

@ -16,6 +16,7 @@ futures = "0.3.25"
# crypto # crypto
argon2 = { version = "0.4", features = ["std"] } argon2 = { version = "0.4", features = ["std"] }
age = { version = "0.9", features = ["async"] } age = { version = "0.9", features = ["async"] }
secrecy = { version = "0.8.0" }
base64 = "0.13" base64 = "0.13"
[dependencies.sea-orm] [dependencies.sea-orm]

View file

@ -1,6 +1,7 @@
use argon2::{ use argon2::{
Argon2, PasswordHasher as ArgonPasswordHasher, PasswordVerifier as ArgonPasswordVerifier, Argon2, PasswordHasher as ArgonPasswordHasher, PasswordVerifier as ArgonPasswordVerifier,
}; };
use secrecy::ExposeSecret;
use futures::io::{AsyncReadExt, AsyncWriteExt}; use futures::io::{AsyncReadExt, AsyncWriteExt};
use rand::Rng; use rand::Rng;
use std::iter; use std::iter;
@ -109,6 +110,13 @@ pub async fn decrypt_password(
Ok(String::from_utf8(decrypt_buffer)?) Ok(String::from_utf8(decrypt_buffer)?)
} }
pub fn create_identity() -> (String, String){
let identity = age::x25519::Identity::generate();
// Public Key & Private Key
(identity.to_public().to_string(), identity.to_string().expose_secret().to_string())
}
pub async fn encrypt_password_with_recipients( pub async fn encrypt_password_with_recipients(
password_plain_text: &str, password_plain_text: &str,
recipients: Vec<&str>, recipients: Vec<&str>,
@ -272,6 +280,14 @@ mod tests {
assert_eq!(PASSWORD, decrypted); assert_eq!(PASSWORD, decrypted);
} }
#[test]
fn test_create_identity() {
let identity = super::create_identity();
assert!(identity.0.contains("age"));
assert!(identity.1.contains("AGE-SECRET-KEY-"));
}
#[tokio::test] #[tokio::test]
async fn test_encrypt_password_with_recipients_is_valid_base64() { async fn test_encrypt_password_with_recipients_is_valid_base64() {
const PASSWORD: &str = "test"; const PASSWORD: &str = "test";