Begin script abstraction
This commit is contained in:
40
ReallifeGamemode.Server.Common/PasswordHasher.cs
Normal file
40
ReallifeGamemode.Server.Common/PasswordHasher.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace ReallifeGamemode.Server.Common
|
||||
{
|
||||
public class PasswordHasher
|
||||
{
|
||||
public static byte[] GetNewSalt(int length = 256)
|
||||
{
|
||||
if ((length % 8) != 0)
|
||||
{
|
||||
throw new ArgumentException("Length mus be completely divisble through 8");
|
||||
}
|
||||
|
||||
var salt = new byte[length / 8];
|
||||
|
||||
using (var rng = RandomNumberGenerator.Create())
|
||||
{
|
||||
rng.GetBytes(salt);
|
||||
}
|
||||
|
||||
return salt;
|
||||
}
|
||||
|
||||
public static string HashPassword(string password, byte[] salt, int length = 512)
|
||||
{
|
||||
if ((length % 8) != 0)
|
||||
{
|
||||
throw new ArgumentException("Length mus be completely divisble through 8");
|
||||
}
|
||||
|
||||
var hashedPassword = KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA512, 50000, length / 8);
|
||||
|
||||
return Convert.ToBase64String(hashedPassword);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="3.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ReallifeGamemode.Server.Types\ReallifeGamemode.Server.Types.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\Import\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
114
ReallifeGamemode.Server.Common/TypeExtensions.cs
Normal file
114
ReallifeGamemode.Server.Common/TypeExtensions.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using ReallifeGamemode.Server.Types;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace ReallifeGamemode.Server.Common
|
||||
{
|
||||
public static class TypeExtensions
|
||||
{
|
||||
private static readonly JsonSerializerSettings settings = new JsonSerializerSettings()
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
||||
DateParseHandling = DateParseHandling.DateTime
|
||||
};
|
||||
|
||||
static TypeExtensions()
|
||||
{
|
||||
settings.Converters.Add(new StringEnumConverter());
|
||||
}
|
||||
|
||||
public static string SerializeJson(this object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
return JsonConvert.SerializeObject(obj, settings);
|
||||
}
|
||||
|
||||
public static T DeserializeJson<T>(this string str)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(str));
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(str, settings);
|
||||
}
|
||||
|
||||
public static bool IsNullOrEmpty(this string str)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (str.Trim().Length == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsNotNullOrEmpty(this string str) => !str.IsNullOrEmpty();
|
||||
|
||||
public static T GetEnumAttribute<T>(this Enum @enum) where T : Attribute
|
||||
{
|
||||
var type = @enum?.GetType();
|
||||
var memberInfo = type?.GetMember(@enum.ToString()).First();
|
||||
return memberInfo?.GetCustomAttribute<T>();
|
||||
}
|
||||
|
||||
public static string GetValue(this Enum @enum)
|
||||
{
|
||||
return @enum?.GetEnumAttribute<EnumMemberAttribute>()?.Value;
|
||||
}
|
||||
|
||||
public static string GetPrefix(this ChatPrefix prefix)
|
||||
{
|
||||
return prefix.GetValue();
|
||||
}
|
||||
|
||||
public static bool TryParseEnum(this string str, Type type, out object @enum)
|
||||
{
|
||||
var members = Enum.GetValues(type).Cast<Enum>();
|
||||
var directEnumMember = members.Where(e => e.ToString().ToLower() == str.ToLower()).FirstOrDefault();
|
||||
|
||||
if (directEnumMember != null)
|
||||
{
|
||||
@enum = directEnumMember;
|
||||
return true;
|
||||
}
|
||||
|
||||
var memberValueMember = members.Where(e => e.GetValue()?.ToLower() == str.ToLower()).FirstOrDefault();
|
||||
|
||||
if (memberValueMember != null)
|
||||
{
|
||||
@enum = memberValueMember;
|
||||
return true;
|
||||
}
|
||||
|
||||
var nativeResult = Enum.TryParse(type, str, true, out var result);
|
||||
@enum = result ?? (default);
|
||||
return nativeResult;
|
||||
}
|
||||
|
||||
public static long ToLong(this object obj)
|
||||
{
|
||||
return long.Parse(obj.ToString());
|
||||
}
|
||||
|
||||
public static int ToInt(this object obj)
|
||||
{
|
||||
return (int)obj.ToLong();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user