passwordhasher, Imedia, IIocation

This commit is contained in:
Adam 2023-11-25 03:15:46 +01:00
parent f401c79ae8
commit 9e2bec5a99
9 changed files with 157 additions and 1 deletions

View File

@ -0,0 +1,11 @@
using AyCode.Entities.Interfaces;
using AyCode.Interfaces.TimeStampInfo;
namespace AyCode.Entities.Locations;
public interface ILocationBase : IEntityGuid
{
double Longitude { get; }
double Latitude { get; }
string? Address { get; }
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AyCode.Entities.Locations
{
[Table("Locations")]
public class LocationBase : ILocationBase
{
public LocationBase() { }
public LocationBase(double longitude, double latitude, string? address) : this(Guid.NewGuid(), longitude, latitude, address) { }
public LocationBase(Guid id, double longitude, double latitude , string address) : this()
{
Id = id;
Longitude = longitude;
Latitude = latitude;
Address = address;
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid Id { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
public string? Address { get; set; }
}
}

View File

@ -12,18 +12,40 @@ namespace AyCode.Entities.Users
public class UserBase : IUserBase
{
public UserBase() { }
public UserBase(string email, string password) : this(Guid.NewGuid(), email, password) { }
public UserBase(Guid id, string email, string password) : this()
{
Id = id;
Email = email;
Email = email;
Password = password;
}
public UserBase(string email, string phoneNumber, string password) : this(Guid.NewGuid(), email, phoneNumber, password) { }
public UserBase(Guid id, string email, string phoneNumber, string password) : this()
{
Id = id;
Email = email;
PhoneNumber = phoneNumber;
Password = password;
}
public UserBase(string email, string phoneNumber, string password, string refreshToken) : this(Guid.NewGuid(), email, phoneNumber, password, refreshToken) { }
public UserBase(Guid id, string email, string phoneNumber, string password, string refreshToken) : this()
{
Id = id;
Email = email;
PhoneNumber = phoneNumber;
Password = password;
RefreshToken = refreshToken;
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid Id { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Password { get; set; }
public string? RefreshToken { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AyCode.Interfaces.MediaInfo
{
internal interface IMediaInfo : IMediaThumbnailUrl, IMediaUserMediaId
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AyCode.Interfaces.MediaInfo
{
internal interface IMediaThumbnailUrl
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AyCode.Interfaces.MediaInfo
{
internal interface IMediaUserMediaId
{
}
}

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="8.0.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,52 @@
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using System.Security.Cryptography;
namespace AyCode.Utils.Helpers
{
public class PasswordHasher
{
public string HashPassword(string password)
{
// Generate a random salt
byte[] salt = new byte[16];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(salt);
}
// Hash the password with the salt
string hashedPassword = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: password,
salt: salt,
prf: KeyDerivationPrf.HMACSHA512,
iterationCount: 10000,
numBytesRequested: 32));
// Combine the salt and hashed password
string combinedHash = $"$bcrypt$v=1$salt={Convert.ToBase64String(salt)}$hash={hashedPassword}";
return combinedHash;
}
public bool VerifyPassword(string password, string hashedPassword)
{
// Extract the salt and hashed password from the combined hash
string[] parts = hashedPassword.Split('$');
byte[] salt = Convert.FromBase64String(parts[3]);
string storedHash = parts[5];
// Hash the provided password with the extracted salt
string hashedProvidedPassword = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: password,
salt: salt,
prf: KeyDerivationPrf.HMACSHA512,
iterationCount: 10000,
numBytesRequested: 32));
// Compare the hashed passwords
return storedHash == hashedProvidedPassword;
}
}
}