Password encryption, Claim groups
This commit is contained in:
105
FoodsharingSiegen.Contracts/Helper/Cryptor.cs
Normal file
105
FoodsharingSiegen.Contracts/Helper/Cryptor.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace FoodsharingSiegen.Contracts.Helper
|
||||
{
|
||||
public static class Cryptor
|
||||
{
|
||||
/// <summary>
|
||||
/// The signing key
|
||||
/// </summary>
|
||||
private const string SigningKey = "2uasw2§$%1nd47n9s43&%Zs3529s23&/%AW";
|
||||
|
||||
#region Public Method TryDecrypt
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts the crypted text (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="cryptedText">The crypted text</param>
|
||||
/// <param name="plainText"></param>
|
||||
/// <returns>The string</returns>
|
||||
public static bool TryDecrypt(string cryptedText, out string plainText)
|
||||
{
|
||||
plainText = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
CreateAlgorithm(out var tripleDes);
|
||||
|
||||
var toEncryptArray = Convert.FromBase64String(cryptedText);
|
||||
|
||||
var cTransform = tripleDes.CreateDecryptor();
|
||||
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||||
|
||||
tripleDes.Clear();
|
||||
|
||||
plainText = Encoding.UTF8.GetString(resultArray);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method Encrypt
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts the plain text (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="plainText">The plain text</param>
|
||||
/// <returns>The string</returns>
|
||||
public static string Encrypt(string plainText)
|
||||
{
|
||||
CreateAlgorithm(out var tripleDes);
|
||||
|
||||
var toEncryptArray = Encoding.UTF8.GetBytes(plainText );
|
||||
|
||||
|
||||
|
||||
var cTransform = tripleDes.CreateEncryptor();
|
||||
|
||||
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
||||
|
||||
tripleDes.Clear();
|
||||
|
||||
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method GetSigningKey
|
||||
|
||||
/// <summary>
|
||||
/// Gets the signing key (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <returns>The security key</returns>
|
||||
public static SecurityKey GetSigningKey() => new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SigningKey));
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method CreateAlgorithm
|
||||
|
||||
/// <summary>
|
||||
/// Creates the algorithm using the specified triple des (a. beging, 04.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="tripleDes">The triple des</param>
|
||||
private static void CreateAlgorithm(out TripleDES tripleDes)
|
||||
{
|
||||
var md5 = MD5.Create();
|
||||
var keyArray = md5.ComputeHash(Encoding.UTF8.GetBytes(SigningKey));
|
||||
md5.Clear();
|
||||
|
||||
tripleDes = TripleDES.Create();
|
||||
tripleDes.Key = keyArray;
|
||||
tripleDes.Mode = CipherMode.ECB;
|
||||
tripleDes.Padding = PaddingMode.PKCS7;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
38
FoodsharingSiegen.Contracts/Helper/EntityExtensions.cs
Normal file
38
FoodsharingSiegen.Contracts/Helper/EntityExtensions.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using FoodsharingSiegen.Contracts.Entity;
|
||||
|
||||
namespace FoodsharingSiegen.Contracts.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// The entity extensions class (a. beging, 08.04.2022)
|
||||
/// </summary>
|
||||
public static class EntityExtensions
|
||||
{
|
||||
#region Public Method IsAdmin
|
||||
|
||||
/// <summary>
|
||||
/// Describes whether is admin
|
||||
/// </summary>
|
||||
/// <param name="user">The user</param>
|
||||
/// <returns>The bool</returns>
|
||||
public static bool IsAdmin(this User user) => user.Type == UserType.Admin;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method IsInGroup
|
||||
|
||||
/// <summary>
|
||||
/// Describes whether is in group
|
||||
/// </summary>
|
||||
/// <param name="user">The user</param>
|
||||
/// <param name="groups">The groups</param>
|
||||
/// <returns>The bool</returns>
|
||||
public static bool IsInGroup(this User user, params UserGroup[] groups)
|
||||
{
|
||||
if (user.Type == UserType.Admin) return true;
|
||||
if (groups.Any(x => user.GroupsList.Contains(x))) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user