Skip to content

Commit

Permalink
feat(string): plain hex dump style
Browse files Browse the repository at this point in the history
feat(aspnet): ASP.NET Identity hash parser helper to migrate from hash v3 to new one
feat(string): base64 support
  • Loading branch information
brunobritodev committed Aug 30, 2021
1 parent 7274910 commit 6f933a5
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/NetDevPack/NetDevPack.csproj
Expand Up @@ -22,6 +22,10 @@
<PackageReference Include="mediatr" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\assets\IconNuget.png">
<Pack>True</Pack>
Expand Down
71 changes: 71 additions & 0 deletions src/NetDevPack/Utilities/AspNetIdentityHashInfo.cs
@@ -0,0 +1,71 @@
using System;
using System.Globalization;

namespace NetDevPack.Utilities
{
public class AspNetIdentityHashInfo
{
public AspNetIdentityHashInfo(string base64Hash)
{
HexHash = base64Hash.FromBase64().ToPlainHexDumpStyle();
Hash = base64Hash;
var hashVersion = HexHash.Substring(0, 2);
switch (hashVersion)
{
case "01":
HashVersion = AspNetIdentityHashVersion.PBKDF2_HMAC_SHA256;
GetV3Info();
break;
case "00":
HashVersion = AspNetIdentityHashVersion.PBKDF2_HMAC_SHA1;
break;
default:
throw new Exception("Invalid hash version");
}


}


private void GetV3Info()
{
HexPrf = HexHash.Substring(2, 8);
HexIterCount = HexHash.Substring(10, 8);
HexSaltLength = HexHash.Substring(18, 8);
HexSalt = HexHash.Substring(26, 32);
HexSubKey = HexHash.Substring(58, 64);
IterCount = int.Parse(HexIterCount, NumberStyles.HexNumber);
SaltLength = int.Parse(HexSaltLength, NumberStyles.HexNumber) * 8;
Salt = HexSalt.FromPlainHexDumpStyleToByteArray().ToBase64();
SubKey = HexSubKey.FromPlainHexDumpStyleToByteArray().ToBase64();
HashcatFormat = $"sha256:{IterCount}:{Salt}:{SubKey}";
ShaType = "sha256";
}

public string ShaType { get; set; }

public string HashcatFormat { get; set; }

public string SubKey { get; set; }

public string Salt { get; set; }

public int SaltLength { get; set; }

public int IterCount { get; set; }

public string HexSubKey { get; set; }

public string HexSalt { get; set; }

public string HexSaltLength { get; set; }

public string HexIterCount { get; set; }

public string HexPrf { get; set; }

public AspNetIdentityHashVersion HashVersion { get; set; }
public string HexHash { get; set; }
public string Hash { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/NetDevPack/Utilities/AspNetIdentityHashVersion.cs
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Text;

namespace NetDevPack.Utilities
{
public enum AspNetIdentityHashVersion
{
PBKDF2_HMAC_SHA1 = 0,
PBKDF2_HMAC_SHA256 = 1,
}
}
55 changes: 55 additions & 0 deletions src/NetDevPack/Utilities/StringExtensions.cs
Expand Up @@ -293,5 +293,60 @@ public static string OnlyNumbers(this string str)
}


public static string FromBase64ToString(this string str, Encoding enc = null)
{
return (enc ?? Encoding.Default).GetString(FromBase64(str));
}

public static byte[] FromBase64(this string str)
{
return Convert.FromBase64String(str);
}
public static string ToBase64(this string str, Encoding enc = null)
{
return ToBase64((enc ?? Encoding.Default).GetBytes(str));
}

public static string ToBase64(this byte[] data, Encoding enc = null)
{
return Convert.ToBase64String(data);
}
public static byte[] FromPlainHexDumpStyleToByteArray(this string hex)
{
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");

byte[] arr = new byte[hex.Length >> 1];

for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}

return arr;
}

private static int GetHexVal(char hex)
{
int val = (int)hex;
//For uppercase A-F letters:
//return val - (val < 58 ? 48 : 55);
//For lowercase a-f letters:
//return val - (val < 58 ? 48 : 87);
//Or the two combined, but a bit slower:
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}

/// <summary>
/// Equivalent to xxd -p
/// see: https://linux.die.net/man/1/xxd
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string ToPlainHexDumpStyle(this byte[] data)
{
return BitConverter.ToString(data).Replace("-", "").ToLower();
}

}
}

0 comments on commit 6f933a5

Please sign in to comment.