Skip to content

Commit 9818c9d

Browse files
initial commit
0 parents  commit 9818c9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2895
-0
lines changed

.gitignore

Lines changed: 400 additions & 0 deletions
Large diffs are not rendered by default.

CPU Password Generator.csproj

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net9.0-windows10.0.26100.0</TargetFramework>
6+
<RootNamespace>CPU_Password_Generator</RootNamespace>
7+
<ImplicitUsings>disable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<PublishAot>true</PublishAot>
10+
<InvariantGlobalization>true</InvariantGlobalization>
11+
<_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>
12+
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
13+
<ApplicationIcon>Resources\shell32_48.ico</ApplicationIcon>
14+
<PlatformTarget>x64</PlatformTarget>
15+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
16+
<UseWindowsForms>True</UseWindowsForms>
17+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
18+
<FileVersion>1.0.0.0</FileVersion>
19+
<Version>1.0.0.0</Version>
20+
</PropertyGroup>
21+
22+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
23+
<DebugType>none</DebugType>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<Compile Remove="lib\**" />
28+
<EmbeddedResource Remove="lib\**" />
29+
<None Remove="lib\**" />
30+
</ItemGroup>
31+
32+
<ItemGroup>
33+
<PackageReference Include="WinFormsComInterop" Version="0.5.0" />
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<Reference Include="HWRand.Net">
38+
<HintPath>lib\HWRand.Net.dll</HintPath>
39+
</Reference>
40+
</ItemGroup>
41+
42+
</Project>

CPU Password Generator.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.35906.104 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPU Password Generator", "CPU Password Generator.csproj", "{88C982CB-E387-4015-BD2D-FAC724C9446D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{88C982CB-E387-4015-BD2D-FAC724C9446D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{88C982CB-E387-4015-BD2D-FAC724C9446D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{88C982CB-E387-4015-BD2D-FAC724C9446D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{88C982CB-E387-4015-BD2D-FAC724C9446D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {38E67D72-7A09-4E9E-8F99-95336E53EE77}
24+
EndGlobalSection
25+
EndGlobal

DWM.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
internal static partial class DwmInterop
5+
{
6+
internal static Boolean Initialized { get => _initialized; }
7+
private static Boolean _initialized = false;
8+
9+
internal static Boolean Initialize()
10+
{
11+
if (_initialized) return false;
12+
13+
Object regOutput = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "CurrentBuildNumber", null)!;
14+
15+
if (regOutput == null) return false;
16+
if (regOutput is not String) return false;
17+
if (!Int32.TryParse((String)regOutput, out _windowsBuildNumber)) return false;
18+
if (_windowsBuildNumber < 0) return false;
19+
20+
if (_windowsBuildNumber >= 18985) // windows 10 '20H1' or newer
21+
{
22+
_dwmDarkModeWindowAttribute = DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE;
23+
_darkModeCompatibilityLevel = DWM_Dark_Mode_Compatibility_Level.IMMERSIVE_DARK_MODE;
24+
}
25+
else if (_windowsBuildNumber >= 17763)
26+
{
27+
_dwmDarkModeWindowAttribute = DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_18985_EQUAL_OR_AFTER_17763;
28+
_darkModeCompatibilityLevel = DWM_Dark_Mode_Compatibility_Level.IMMERSIVE_DARK_MODE_BEFORE_18985_EQUAL_OR_AFTER_17763;
29+
}
30+
else
31+
{
32+
_dwmDarkModeWindowAttribute = 0;
33+
_darkModeCompatibilityLevel = DWM_Dark_Mode_Compatibility_Level.NONE;
34+
}
35+
36+
_initialized = true;
37+
return true;
38+
}
39+
40+
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
41+
42+
[LibraryImport("dwmapi.dll", SetLastError = true)]
43+
[return: MarshalAs(UnmanagedType.U4)]
44+
private static unsafe partial UInt32 DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, UInt32* pvAttribute, UInt32 cbAttribute);
45+
46+
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
47+
48+
/// <summary>
49+
/// Requires OS version 17763 or later
50+
/// </summary>
51+
/// <param name="hwnd"></param>
52+
/// <param name="darkMode"></param>
53+
/// <returns>HRESULT</returns>
54+
/// <exception cref="InvalidOperationException"></exception>
55+
internal static unsafe UInt32 SetTheme(IntPtr hwnd, Boolean darkMode)
56+
{
57+
if (!_initialized) throw new Exception("DwmInterop not initialized.");
58+
59+
UInt32 expandedBoolean = *(UInt32*)&darkMode;
60+
61+
return DwmSetWindowAttribute(hwnd, _dwmDarkModeWindowAttribute, &expandedBoolean, sizeof(UInt32));
62+
}
63+
64+
/// <summary>
65+
/// Requires OS version 22000 or later
66+
/// </summary>
67+
/// <param name="hwnd"></param>
68+
/// <param name="COLORREF"></param>
69+
/// <returns>HRESULT</returns>
70+
/// <exception cref="Exception"></exception>
71+
internal static unsafe UInt32 SetCaptionColor(IntPtr hwnd, UInt32 COLORREF)
72+
{
73+
if (!_initialized) throw new Exception("DwmInterop not initialized.");
74+
75+
return DwmSetWindowAttribute(hwnd, DWMWINDOWATTRIBUTE.DWMWA_CAPTION_COLOR, &COLORREF, sizeof(UInt32));
76+
}
77+
78+
/// <summary>
79+
/// Requires OS version 22000 or later
80+
/// </summary>
81+
/// <param name="hwnd"></param>
82+
/// <param name="COLORREF"></param>
83+
/// <returns>HRESULT</returns>
84+
/// <exception cref="Exception"></exception>
85+
internal static unsafe UInt32 SetBorderColor(IntPtr hwnd, UInt32 COLORREF)
86+
{
87+
if (!_initialized) throw new Exception("DwmInterop not initialized.");
88+
89+
return DwmSetWindowAttribute(hwnd, DWMWINDOWATTRIBUTE.DWMWA_BORDER_COLOR, &COLORREF, sizeof(UInt32));
90+
}
91+
92+
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
93+
94+
internal static DWM_Dark_Mode_Compatibility_Level DarkModeCompatibilityLevel { get => _darkModeCompatibilityLevel; }
95+
private static DWM_Dark_Mode_Compatibility_Level _darkModeCompatibilityLevel;
96+
97+
internal enum DWM_Dark_Mode_Compatibility_Level : Int32
98+
{
99+
NONE = 0,
100+
IMMERSIVE_DARK_MODE_BEFORE_18985_EQUAL_OR_AFTER_17763 = 1,
101+
IMMERSIVE_DARK_MODE = 2,
102+
}
103+
104+
private static DWMWINDOWATTRIBUTE _dwmDarkModeWindowAttribute = 0;
105+
private enum DWMWINDOWATTRIBUTE : UInt32
106+
{
107+
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_18985_EQUAL_OR_AFTER_17763 = 19,
108+
DWMWA_USE_IMMERSIVE_DARK_MODE = 20,
109+
DWMWA_WINDOW_CORNER_PREFERENCE = 33,
110+
DWMWA_BORDER_COLOR = 34,
111+
DWMWA_CAPTION_COLOR = 35,
112+
DWMWA_TEXT_COLOR = 36
113+
}
114+
115+
private static Int32 _windowsBuildNumber = -1;
116+
}

Generator/BufferToBase64String.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
3+
internal static partial class Generator
4+
{
5+
private static readonly Int32[] PaddingLengthMap = [0, 2, 1];
6+
7+
private unsafe static String BufferToBase64String(ref readonly Span<Byte> buffer, Int32 length, Boolean exactLength, Boolean trim)
8+
{
9+
String base64 = Convert.ToBase64String(buffer, Base64FormattingOptions.None);
10+
11+
if (exactLength)
12+
{
13+
String output = new('\0', length);
14+
15+
fixed (Char* ptr = &output.GetPinnableReference())
16+
{
17+
for (Int32 i = 0; i < length; ++i)
18+
{
19+
ptr[i] = (Char)buffer[i];
20+
}
21+
}
22+
23+
return output;
24+
}
25+
26+
if (trim)
27+
{
28+
Int32 base64Length = base64.Length - PaddingLengthMap[length % 3];
29+
30+
String output = new('\0', base64Length);
31+
32+
fixed (Char* ptr = &output.GetPinnableReference())
33+
{
34+
for (Int32 i = 0; i < base64Length; ++i)
35+
{
36+
ptr[i] = base64[i];
37+
}
38+
}
39+
40+
return output;
41+
}
42+
43+
return Convert.ToBase64String(buffer, Base64FormattingOptions.None);
44+
}
45+
}

Generator/BufferToString.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
internal static partial class Generator
4+
{
5+
private unsafe static String BufferToString(ref readonly Span<Byte> buffer, UInt16 length)
6+
{
7+
String output = new('\0', length);
8+
9+
fixed (Char* ptr = &output.GetPinnableReference())
10+
{
11+
for (Int32 i = 0; i < length; ++i)
12+
{
13+
ptr[i] = (Char)buffer[i];
14+
}
15+
}
16+
17+
return output;
18+
}
19+
}

Generator/FillBuffer.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using BSS.Random;
3+
using System.Runtime.InteropServices;
4+
5+
internal static partial class Generator
6+
{
7+
[StructLayout(LayoutKind.Explicit)]
8+
private ref struct UIntByteView
9+
{
10+
[FieldOffset(0)]
11+
internal UInt64 UInt64;
12+
13+
//
14+
15+
[FieldOffset(0)]
16+
internal Byte Byte1;
17+
18+
[FieldOffset(1)]
19+
internal Byte Byte2;
20+
21+
[FieldOffset(2)]
22+
internal Byte Byte3;
23+
24+
[FieldOffset(3)]
25+
internal Byte Byte4;
26+
27+
[FieldOffset(4)]
28+
internal Byte Byte5;
29+
30+
[FieldOffset(5)]
31+
internal Byte Byte6;
32+
33+
[FieldOffset(6)]
34+
internal Byte Byte7;
35+
36+
[FieldOffset(7)]
37+
internal Byte Byte8;
38+
}
39+
40+
private static Boolean FillBuffer(ref readonly Span<Byte> buffer, UInt16 length)
41+
{
42+
UIntByteView byteSplitter = default;
43+
Int32 i = 0;
44+
45+
TOP:
46+
if (!HWRandom.ReadSeed64(in byteSplitter.UInt64)) return false;
47+
48+
if (Settings.CharMap[byteSplitter.Byte1])
49+
{
50+
buffer[i++] = byteSplitter.Byte1;
51+
if (i == length) return true;
52+
}
53+
54+
if (Settings.CharMap[byteSplitter.Byte2])
55+
{
56+
buffer[i++] = byteSplitter.Byte2;
57+
if (i == length) return true;
58+
}
59+
60+
if (Settings.CharMap[byteSplitter.Byte3])
61+
{
62+
buffer[i++] = byteSplitter.Byte3;
63+
if (i == length) return true;
64+
}
65+
66+
if (Settings.CharMap[byteSplitter.Byte4])
67+
{
68+
buffer[i++] = byteSplitter.Byte4;
69+
if (i == length) return true;
70+
}
71+
72+
if (Settings.CharMap[byteSplitter.Byte5])
73+
{
74+
buffer[i++] = byteSplitter.Byte5;
75+
if (i == length) return true;
76+
}
77+
78+
if (Settings.CharMap[byteSplitter.Byte6])
79+
{
80+
buffer[i++] = byteSplitter.Byte6;
81+
if (i == length) return true;
82+
}
83+
84+
if (Settings.CharMap[byteSplitter.Byte7])
85+
{
86+
buffer[i++] = byteSplitter.Byte7;
87+
if (i == length) return true;
88+
}
89+
90+
if (Settings.CharMap[byteSplitter.Byte8])
91+
{
92+
buffer[i++] = byteSplitter.Byte8;
93+
if (i == length) return true;
94+
}
95+
96+
goto TOP;
97+
}
98+
}

Generator/_Generate.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Windows.Forms;
3+
4+
internal static partial class Generator
5+
{
6+
internal unsafe static String Generate(UInt16 length)
7+
{
8+
Span<Byte> buffer = stackalloc Byte[length];
9+
10+
if (!FillBuffer(ref buffer, length))
11+
{
12+
MessageBox.Show("Failed to fill buffer with random bytes,\nthis usually only happens in very (very) rare cases.\n\nSource overloaded?", "RNG Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
13+
return null!;
14+
}
15+
16+
if (Settings.EncodeBase64) return BufferToBase64String(ref buffer, length, Settings.ExactLength, Settings.TrimEncodeBase64);
17+
else return BufferToString(ref buffer, length);
18+
}
19+
}

0 commit comments

Comments
 (0)