Skip to content

Commit

Permalink
Clean-up on ConverterTool
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Jan 18, 2023
1 parent 116cf89 commit 329abda
Showing 1 changed file with 3 additions and 107 deletions.
110 changes: 3 additions & 107 deletions Hi3Helper.Core/Classes/Data/Tools/ConverterTool.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Force.Crc32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading.Tasks;

Expand All @@ -18,8 +15,8 @@ public class ConverterTool
public static async Task<string> BytesToCRC32SimpleAsync(Stream buffer) => HexTool.BytesToHexUnsafe(await Task.Run(() => CRCEncoder.ComputeHash(buffer)).ConfigureAwait(false));
public static string CreateMD5(Stream fs)
{
MD5Hash.Initialize();
ReadOnlySpan<byte> res = MD5Hash.ComputeHash(fs);
MD5 md5Instance = MD5.Create();
ReadOnlySpan<byte> res = md5Instance.ComputeHash(fs);
return HexTool.BytesToHexUnsafe(res);
}
public static async Task<string> CreateMD5Async(Stream fs)
Expand All @@ -37,14 +34,8 @@ public static uint BytesToUInt32Big(byte[] buffer) =>

public static ushort BytesToUInt16Big(byte[] buffer) => (ushort)((BitConverter.ToUInt16(buffer, 0) & 0xFFU) << 8 | (BitConverter.ToUInt16(buffer, 0) & 0xFF00U) >> 8);

public static uint ToUInt32Big(uint value) =>
(value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
(value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;

public static double GetPercentageNumber(double cur, double max, int round = 2) => Math.Round((100 * cur) / max, round);

public static ushort ToUInt16Big(ushort value) => (ushort)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8);

public static string NormalizePath(string i) =>
Path.Combine(Path.GetDirectoryName(i), Path.GetFileName(i));

Expand All @@ -57,24 +48,11 @@ public static uint ConcatUint(uint a, uint b)
return a + b;
}

public static uint SumBinaryUint(uint a, uint b)
{
uint mask = uint.MaxValue;

while ((mask & b) != 0)
{
mask <<= 1;
a <<= 1;
}

return a | b;
}

public static string SummarizeSizeSimple(double value, int decimalPlaces = 2)
{
byte mag = (byte)Math.Log(value, 1000);

return $"{Math.Round(value / (1L << (mag * 10)), decimalPlaces)} {SizeSuffixes[mag]}";
return string.Format("{0} {1}", Math.Round(value / (1L << (mag * 10)), decimalPlaces), SizeSuffixes[mag]);
}

// Reference:
Expand All @@ -83,88 +61,6 @@ public static string SummarizeSizeSimple(double value, int decimalPlaces = 2)

public static int UnixTimestamp() => (int)Math.Truncate(DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);

// Reference:
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/7f5765cc-3edc-44b4-92c6-7b9680e778ed/getting-md5sha-as-number-instead-of-string?forum=csharpgeneral
public static BigInteger HexToNumber(HashAlgorithm algorithm, byte[] data) => new BigInteger(algorithm.ComputeHash(data));

// Reference:
// https://makolyte.com/csharp-hex-string-to-byte-array
internal readonly static Dictionary<char, byte> hexmap = new Dictionary<char, byte>()
{
{ 'a', 0xA },{ 'b', 0xB },{ 'c', 0xC },{ 'd', 0xD },
{ 'e', 0xE },{ 'f', 0xF },{ 'A', 0xA },{ 'B', 0xB },
{ 'C', 0xC },{ 'D', 0xD },{ 'E', 0xE },{ 'F', 0xF },
{ '0', 0x0 },{ '1', 0x1 },{ '2', 0x2 },{ '3', 0x3 },
{ '4', 0x4 },{ '5', 0x5 },{ '6', 0x6 },{ '7', 0x7 },
{ '8', 0x8 },{ '9', 0x9 }
};

public static byte[] HexToBytes(string hex)
{
if (string.IsNullOrWhiteSpace(hex))
throw new ArgumentException("Hex cannot be null/empty/whitespace");

if (hex.Length % 2 != 0)
throw new FormatException("Hex must have an even number of characters");

bool startsWithHexStart = hex.StartsWith("0x", StringComparison.OrdinalIgnoreCase);

if (startsWithHexStart && hex.Length == 2)
throw new ArgumentException("There are no characters in the hex string");


int startIndex = startsWithHexStart ? 2 : 0;

byte[] bytesArr = new byte[(hex.Length - startIndex) / 2];

char left;
char right;

try
{
int x = 0;
for (int i = startIndex; i < hex.Length; i += 2, x++)
{
left = hex[i];
right = hex[i + 1];
bytesArr[x] = (byte)((hexmap[left] << 4) | hexmap[right]);
}
return bytesArr;
}
catch (KeyNotFoundException)
{
throw new FormatException("Hex string has non-hex character");
}
}

/// <summary>
/// Convert an integer to a string of hexidecimal numbers.
/// </summary>
/// <param name="n">The int to convert to Hex representation</param>
/// <param name="len">number of digits in the hex string. Pads with leading zeros.</param>
/// <returns></returns>
public static string NumberToHexString(long n, int len = 8) => new string(StringToChars(n, len));

private static char[] StringToChars(long n, int len)
{
char[] ch = new char[len--];
for (int i = len; i >= 0; i--) ch[len - i] = ByteToHexChar((byte)((ulong)(n >> 4 * i) & 15));

return ch;
}

/// <summary>
/// Convert a byte to a hexidecimal char
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
private static char ByteToHexChar(byte b)
{
if (b < 0 || b > 15)
throw new Exception("IntToHexChar: input out of range for Hex value");
return b < 10 ? (char)(b + 48) : (char)(b + 55);
}

public static bool IsUserHasPermission(string input)
{
try
Expand Down

0 comments on commit 329abda

Please sign in to comment.