CodeLiturgy.Dashboard/BlueWest.Api/Users/Auth/Crypto/BaseCryptoItem.cs

98 lines
2.9 KiB
C#
Raw Normal View History

2022-09-10 00:33:17 +03:00
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace BlueWest.Cryptography
{
2022-09-11 01:22:04 +03:00
internal abstract class BaseCryptoItem
2022-09-10 00:33:17 +03:00
{
public enum HashAlgorithm
{
// ReSharper disable once InconsistentNaming
SHA2_512 = 1,
// ReSharper disable once InconsistentNaming
PBKDF2_SHA512 = 2,
// ReSharper disable once InconsistentNaming
SHA3_512 = 3
}
2022-09-11 01:22:04 +03:00
/// <summary>
/// HexStringToByteArray
/// </summary>
/// <param name="stringInHexFormat"></param>
/// <returns></returns>
2022-09-10 00:33:17 +03:00
protected byte[] HexStringToByteArray(string stringInHexFormat)
{
var converted = Enumerable.Range(0, stringInHexFormat.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(stringInHexFormat.Substring(x, 2), 16))
.ToArray();
return converted;
}
2022-09-11 01:22:04 +03:00
/// <summary>
/// ByteArrayToString
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
2022-09-10 00:33:17 +03:00
protected string ByteArrayToString(byte[] bytes)
{
var sb = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
sb.Append(bytes[i].ToString("X2"));
}
return sb.ToString();
}
/// <summary>
/// Generates a random string
/// </summary>
/// <param name="length">The length of the random string</param>
/// <returns></returns>
protected string CreateRandomString(int length)
{
var rng = RandomNumberGenerator.Create();
var buffer = new byte[length / 2];
rng.GetBytes(buffer);
var randomString = BitConverter.ToString(buffer).Replace("-", "");
return randomString;
}
// TODO Refactor me
/// <summary>
/// Get Cryptographic algorithm
/// </summary>
/// <param name="cipherText"></param>
/// <param name="algorithm"></param>
/// <param name="keyIndex"></param>
/// <param name="trimmedCipherText"></param>
protected void GetAlgorithm(string cipherText, out int? algorithm, out int? keyIndex,
out string trimmedCipherText)
{
algorithm = null;
keyIndex = null;
trimmedCipherText = cipherText;
2022-09-18 04:00:24 +03:00
if (cipherText.Length <= 5 ) return;
2022-09-10 00:33:17 +03:00
2022-09-18 04:00:24 +03:00
var cipherInfo = cipherText[0].ToString();
2022-09-10 00:33:17 +03:00
2022-09-18 04:00:24 +03:00
if (int.TryParse(cipherInfo, out int foundAlgorithm))
2022-09-10 00:33:17 +03:00
{
algorithm = foundAlgorithm;
}
2022-09-18 04:00:24 +03:00
if (int.TryParse(cipherInfo, out int foundKeyIndex))
2022-09-10 00:33:17 +03:00
keyIndex = foundKeyIndex;
2022-09-18 04:00:24 +03:00
trimmedCipherText = cipherText.Substring(cipherText.IndexOf(cipherInfo, StringComparison.Ordinal) + 1);
2022-09-10 00:33:17 +03:00
}
}
}