From 4d85638d8c8d3f86b205d0b49dc692ba6028d291 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 13 Nov 2022 18:15:56 +0000 Subject: [PATCH] Move password utils to utils::passwords module --- src/activitypub/actors/attachments.rs | 2 +- src/cli.rs | 12 +++++++----- src/json_signatures/verify.rs | 2 +- src/mastodon_api/accounts/views.rs | 18 ++++++++++-------- src/mastodon_api/oauth/views.rs | 2 +- src/utils/crypto.rs | 19 +------------------ src/utils/mod.rs | 1 + src/utils/passwords.rs | 16 ++++++++++++++++ 8 files changed, 38 insertions(+), 34 deletions(-) create mode 100644 src/utils/passwords.rs diff --git a/src/activitypub/actors/attachments.rs b/src/activitypub/actors/attachments.rs index c42f33e..c92454e 100644 --- a/src/activitypub/actors/attachments.rs +++ b/src/activitypub/actors/attachments.rs @@ -58,7 +58,7 @@ pub fn parse_identity_proof( verify_minisign_signature( did_key, &message, - &signature, + signature, ).map_err(|_| ValidationError("invalid identity proof"))?; }, Did::Pkh(ref did_pkh) => { diff --git a/src/cli.rs b/src/cli.rs index d80fa7a..2bffd80 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -28,12 +28,14 @@ use crate::models::users::queries::{ set_user_password, }; use crate::monero::wallet::create_monero_wallet; -use crate::utils::crypto::{ - hash_password, - generate_private_key, - serialize_private_key, +use crate::utils::{ + crypto::{ + generate_private_key, + serialize_private_key, + }, + files::remove_files, + passwords::hash_password, }; -use crate::utils::files::remove_files; /// Admin CLI tool #[derive(Parser)] diff --git a/src/json_signatures/verify.rs b/src/json_signatures/verify.rs index 0411990..a9e6c3c 100644 --- a/src/json_signatures/verify.rs +++ b/src/json_signatures/verify.rs @@ -141,7 +141,7 @@ mod tests { use super::*; #[test] - fn test_get_json_signature_eip155() { + fn test_get_json_signature_eip191() { let signed_object = json!({ "type": "Test", "id": "https://example.org/objects/1", diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index ed4ea7e..100b50c 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -71,15 +71,17 @@ use crate::models::users::queries::{ get_user_by_did, }; use crate::models::users::types::UserCreateData; -use crate::utils::caip2::ChainId; -use crate::utils::canonicalization::canonicalize_object; -use crate::utils::crypto::{ - hash_password, - generate_private_key, - serialize_private_key, +use crate::utils::{ + caip2::ChainId, + canonicalization::canonicalize_object, + crypto::{ + generate_private_key, + serialize_private_key, + }, + currencies::Currency, + id::new_uuid, + passwords::hash_password, }; -use crate::utils::currencies::Currency; -use crate::utils::id::new_uuid; use super::helpers::get_relationship; use super::types::{ Account, diff --git a/src/mastodon_api/oauth/views.rs b/src/mastodon_api/oauth/views.rs index ef511e3..75ef7c7 100644 --- a/src/mastodon_api/oauth/views.rs +++ b/src/mastodon_api/oauth/views.rs @@ -10,8 +10,8 @@ use crate::models::users::queries::{ get_user_by_name, get_user_by_login_address, }; -use crate::utils::crypto::verify_password; use crate::utils::currencies::{validate_wallet_address, Currency}; +use crate::utils::passwords::verify_password; use super::types::{TokenRequest, TokenResponse}; use super::utils::generate_access_token; diff --git a/src/utils/crypto.rs b/src/utils/crypto.rs index 9b53d22..497b6e6 100644 --- a/src/utils/crypto.rs +++ b/src/utils/crypto.rs @@ -1,25 +1,7 @@ -use pem; -use rand; -use rand::prelude::*; use rsa::{Hash, PaddingScheme, PublicKey, RsaPrivateKey, RsaPublicKey}; use rsa::pkcs8::{FromPrivateKey, FromPublicKey, ToPrivateKey, ToPublicKey}; use sha2::{Digest, Sha256}; -pub fn hash_password(password: &str) -> Result { - let mut rng = rand::thread_rng(); - let salt: [u8; 32] = rng.gen(); - let config = argon2::Config::default(); - - argon2::hash_encoded(password.as_bytes(), &salt, &config) -} - -pub fn verify_password( - password_hash: &str, - password: &str, -) -> Result { - argon2::verify_encoded(password_hash, password.as_bytes()) -} - pub fn generate_private_key() -> Result { let mut rng = rand::rngs::OsRng; let bits = 2048; @@ -28,6 +10,7 @@ pub fn generate_private_key() -> Result { #[cfg(test)] pub fn generate_weak_private_key() -> Result { + use rand::SeedableRng; let mut rng = rand::rngs::SmallRng::seed_from_u64(0); let bits = 512; RsaPrivateKey::new(&mut rng, bits) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index fb316fb..c49745d 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -6,4 +6,5 @@ pub mod files; pub mod html; pub mod id; pub mod markdown; +pub mod passwords; pub mod urls; diff --git a/src/utils/passwords.rs b/src/utils/passwords.rs new file mode 100644 index 0000000..081862e --- /dev/null +++ b/src/utils/passwords.rs @@ -0,0 +1,16 @@ +use rand::Rng; + +pub fn hash_password(password: &str) -> Result { + let mut rng = rand::thread_rng(); + let salt: [u8; 32] = rng.gen(); + let config = argon2::Config::default(); + + argon2::hash_encoded(password.as_bytes(), &salt, &config) +} + +pub fn verify_password( + password_hash: &str, + password: &str, +) -> Result { + argon2::verify_encoded(password_hash, password.as_bytes()) +}