passwordhasher, Imedia, IIocation
This commit is contained in:
parent
f401c79ae8
commit
9e2bec5a99
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
|
@ -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; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,6 +12,7 @@ namespace AyCode.Entities.Users
|
||||||
public class UserBase : IUserBase
|
public class UserBase : IUserBase
|
||||||
{
|
{
|
||||||
public UserBase() { }
|
public UserBase() { }
|
||||||
|
|
||||||
public UserBase(string email, string password) : this(Guid.NewGuid(), email, password) { }
|
public UserBase(string email, string password) : this(Guid.NewGuid(), email, password) { }
|
||||||
public UserBase(Guid id, string email, string password) : this()
|
public UserBase(Guid id, string email, string password) : this()
|
||||||
{
|
{
|
||||||
|
|
@ -20,10 +21,31 @@ namespace AyCode.Entities.Users
|
||||||
Password = password;
|
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)]
|
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
|
public string PhoneNumber { get; set; }
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
|
public string? RefreshToken { get; set; }
|
||||||
|
|
||||||
public DateTime Created { get; set; }
|
public DateTime Created { get; set; }
|
||||||
public DateTime Modified { get; set; }
|
public DateTime Modified { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
|
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="8.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue