Added a way to login and register and a tools project
This commit is contained in:
16
BrightGlimmer.Tools/BrightGlimmer.Tools.csproj
Normal file
16
BrightGlimmer.Tools/BrightGlimmer.Tools.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.IdentityModel.Tokens">
|
||||
<HintPath>..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.identitymodel.tokens\5.3.0\lib\netstandard2.0\Microsoft.IdentityModel.Tokens.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.IdentityModel.Tokens.Jwt">
|
||||
<HintPath>..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\system.identitymodel.tokens.jwt\5.3.0\lib\netstandard2.0\System.IdentityModel.Tokens.Jwt.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
41
BrightGlimmer.Tools/JwtTokenCreator.cs
Normal file
41
BrightGlimmer.Tools/JwtTokenCreator.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Tools
|
||||
{
|
||||
/* TODO: WIP */
|
||||
public class JwtTokenCreator
|
||||
{
|
||||
private readonly string privateKey;
|
||||
|
||||
public JwtTokenCreator(string privateKey)
|
||||
{
|
||||
this.privateKey = privateKey;
|
||||
}
|
||||
|
||||
public string CreateToken(string username, string email)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.UTF8.GetBytes(privateKey);
|
||||
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new Claim[]
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, username),
|
||||
new Claim(ClaimTypes.Email, email)
|
||||
}),
|
||||
Expires = DateTime.UtcNow.AddDays(1),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
var tokenSecurity = tokenHandler.CreateToken(tokenDescriptor);
|
||||
var token = tokenHandler.WriteToken(tokenSecurity);
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
BrightGlimmer.Tools/PasswordHasher.cs
Normal file
53
BrightGlimmer.Tools/PasswordHasher.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace BrightGlimmer.Tools
|
||||
{
|
||||
public class PasswordHasher
|
||||
{
|
||||
private readonly int SALT_LENGTH = 16;
|
||||
private readonly int PASS_LENGTH = 20;
|
||||
private readonly int ITERATIONS = 10000;
|
||||
|
||||
public bool Verify(string password, string hash)
|
||||
{
|
||||
var storedHash = Convert.FromBase64String(hash);
|
||||
|
||||
var salt = new byte[SALT_LENGTH];
|
||||
Array.Copy(storedHash, 0, salt, 0, SALT_LENGTH);
|
||||
|
||||
var givenHash = GetHash(password, salt);
|
||||
|
||||
return hash == givenHash;
|
||||
}
|
||||
|
||||
public string GetHash(string password, byte[] salt = null)
|
||||
{
|
||||
if (salt == null)
|
||||
{
|
||||
salt = CreateSalt();
|
||||
}
|
||||
var hash = CreateHash(password, salt);
|
||||
|
||||
var completeHash = new byte[SALT_LENGTH + PASS_LENGTH];
|
||||
Array.Copy(salt, 0, completeHash, 0, SALT_LENGTH);
|
||||
Array.Copy(hash, 0, completeHash, SALT_LENGTH, PASS_LENGTH);
|
||||
|
||||
return Convert.ToBase64String(completeHash);
|
||||
}
|
||||
|
||||
private byte[] CreateHash(string password, byte[] salt)
|
||||
{
|
||||
var pbkdf2 = new Rfc2898DeriveBytes(password, salt, ITERATIONS);
|
||||
return pbkdf2.GetBytes(PASS_LENGTH);
|
||||
}
|
||||
|
||||
private byte[] CreateSalt()
|
||||
{
|
||||
var salt = new byte[SALT_LENGTH];
|
||||
new RNGCryptoServiceProvider().GetBytes(salt);
|
||||
|
||||
return salt;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user