Skip to content

Commit 6806a32

Browse files
- updated HWRandom.NET to 2.0.0.0
- fixed a bug (infinite loop) when attempting to generate a password when no characters were enabled in the settings
1 parent 6682de1 commit 6806a32

21 files changed

+124
-97
lines changed

CPU Password Generator.csproj

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,18 @@
1414
<PlatformTarget>x64</PlatformTarget>
1515
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
1616
<UseWindowsForms>True</UseWindowsForms>
17-
<AssemblyVersion>1.0.1.0</AssemblyVersion>
18-
<FileVersion>1.0.1.0</FileVersion>
19-
<Version>1.0.1.0</Version>
20-
<AssemblyTitle>Generate Passwords with the CPU RNG</AssemblyTitle>
21-
<Platforms>AnyCPU;x64</Platforms>
17+
<Platforms>x64</Platforms>
2218
</PropertyGroup>
2319

2420
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
2521
<DebugType>none</DebugType>
2622
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
2723
</PropertyGroup>
2824

29-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
30-
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
31-
</PropertyGroup>
32-
3325
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
3426
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
3527
</PropertyGroup>
3628

37-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
38-
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
39-
</PropertyGroup>
40-
4129
<ItemGroup>
4230
<Compile Remove="libs\**" />
4331
<EmbeddedResource Remove="libs\**" />
@@ -52,13 +40,13 @@
5240
<Reference Include="HWRandom.NET">
5341
<HintPath>libs\HWRandom.NET.dll</HintPath>
5442
</Reference>
55-
<DirectPInvoke Include="native" />
56-
<NativeLibrary Include="libs\native.lib" />
43+
<DirectPInvoke Include="HWRandomCore" />
44+
<NativeLibrary Include="libs\HWRandomCore.lib" />
5745
<LinkerArg Include="/LTCG /FIXED:NO /NXCOMPAT /DYNAMICBASE /LARGEADDRESSAWARE /CETCOMPAT" />
5846
</ItemGroup>
5947

6048
<ItemGroup>
61-
<None Update="native.dll">
49+
<None Update="HWRandomCore.dll">
6250
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6351
</None>
6452
</ItemGroup>

DWM.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ internal static Boolean Initialize()
3737
return true;
3838
}
3939

40-
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
40+
/********************************************************************/
4141

4242
[LibraryImport("dwmapi.dll", SetLastError = true)]
4343
[return: MarshalAs(UnmanagedType.U4)]
4444
private static unsafe partial UInt32 DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, UInt32* pvAttribute, UInt32 cbAttribute);
4545

46-
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
46+
/********************************************************************/
4747

4848
/// <summary>
4949
/// Requires OS version 17763 or later
@@ -89,7 +89,7 @@ internal static unsafe UInt32 SetBorderColor(IntPtr hwnd, UInt32 COLORREF)
8989
return DwmSetWindowAttribute(hwnd, DWMWINDOWATTRIBUTE.DWMWA_BORDER_COLOR, &COLORREF, sizeof(UInt32));
9090
}
9191

92-
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
92+
/********************************************************************/
9393

9494
internal static DWM_Dark_Mode_Compatibility_Level DarkModeCompatibilityLevel { get => _darkModeCompatibilityLevel; }
9595
private static DWM_Dark_Mode_Compatibility_Level _darkModeCompatibilityLevel;

Generator/CharMapIsNotAllZero.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Runtime.Intrinsics;
3+
using System.Runtime.Intrinsics.X86;
4+
5+
internal static partial class Generator
6+
{
7+
private unsafe static Boolean CharMapIsNotAllZero(Byte* charMapPtr)
8+
{
9+
if (Avx2.X64.IsSupported) return IsNotAllZeroAVX2(charMapPtr);
10+
else return IsNotAllZero(charMapPtr);
11+
}
12+
13+
/********************************************************************/
14+
15+
private static readonly Vector256<Byte> MASK = Vector256.Create((Byte)0);
16+
17+
private unsafe static Boolean IsNotAllZeroAVX2(Byte* charMapPtr)
18+
{
19+
//if (length < 1 || buffer == null) throw new Exception("buffer was null or length was less than 1");
20+
21+
UInt16 counter = 0;
22+
23+
TOP:
24+
Vector256<Byte> vector = Avx.LoadVector256(charMapPtr + counter);
25+
Vector256<Byte> result = Avx2.CompareEqual(MASK, vector);
26+
27+
if (Avx2.MoveMask(result) != -1) return true;
28+
29+
counter += 32;
30+
if (counter < 256) goto TOP;
31+
32+
return false;
33+
}
34+
35+
/********************************************************************/
36+
37+
private static unsafe Boolean IsNotAllZero(Byte* charMapPtr)
38+
{
39+
Int32 counter = -1;
40+
41+
TOP:
42+
if (charMapPtr[++counter] != 0) return true;
43+
44+
if (counter < 256) goto TOP;
45+
else return false;
46+
}
47+
}

Generator/FillBuffer.cs

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,86 +10,65 @@ private ref struct UIntByteView
1010
[FieldOffset(0)]
1111
internal UInt64 UInt64;
1212

13-
//
14-
1513
[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;
14+
internal unsafe fixed Byte Bytes[8];
3815
}
3916

40-
private static Boolean FillBuffer(ref readonly Span<Byte> buffer, UInt16 length)
17+
private unsafe static Boolean FillBuffer(ref readonly Span<Byte> buffer, UInt16 length, Byte* charMapPtr)
4118
{
42-
UIntByteView byteSplitter = default;
19+
UIntByteView byteSplitter;
20+
byteSplitter.UInt64 = 0;
21+
4322
Int32 i = 0;
4423

4524
TOP:
4625
if (!HWRandom.ReadSeed64(in byteSplitter.UInt64)) return false;
4726

48-
if (Settings.CharMap[byteSplitter.Byte1])
27+
if (*(Boolean*)&charMapPtr[byteSplitter.Bytes[0]])
4928
{
50-
buffer[i++] = byteSplitter.Byte1;
29+
buffer[i++] = byteSplitter.Bytes[0];
5130
if (i == length) return true;
5231
}
5332

54-
if (Settings.CharMap[byteSplitter.Byte2])
33+
if (*(Boolean*)&charMapPtr[byteSplitter.Bytes[1]])
5534
{
56-
buffer[i++] = byteSplitter.Byte2;
35+
buffer[i++] = byteSplitter.Bytes[1];
5736
if (i == length) return true;
5837
}
5938

60-
if (Settings.CharMap[byteSplitter.Byte3])
39+
if (*(Boolean*)&charMapPtr[byteSplitter.Bytes[2]])
6140
{
62-
buffer[i++] = byteSplitter.Byte3;
41+
buffer[i++] = byteSplitter.Bytes[2];
6342
if (i == length) return true;
6443
}
6544

66-
if (Settings.CharMap[byteSplitter.Byte4])
45+
if (*(Boolean*)&charMapPtr[byteSplitter.Bytes[3]])
6746
{
68-
buffer[i++] = byteSplitter.Byte4;
47+
buffer[i++] = byteSplitter.Bytes[3];
6948
if (i == length) return true;
7049
}
7150

72-
if (Settings.CharMap[byteSplitter.Byte5])
51+
if (*(Boolean*)&charMapPtr[byteSplitter.Bytes[4]])
7352
{
74-
buffer[i++] = byteSplitter.Byte5;
53+
buffer[i++] = byteSplitter.Bytes[4];
7554
if (i == length) return true;
7655
}
7756

78-
if (Settings.CharMap[byteSplitter.Byte6])
57+
if (*(Boolean*)&charMapPtr[byteSplitter.Bytes[5]])
7958
{
80-
buffer[i++] = byteSplitter.Byte6;
59+
buffer[i++] = byteSplitter.Bytes[5];
8160
if (i == length) return true;
8261
}
8362

84-
if (Settings.CharMap[byteSplitter.Byte7])
63+
if (*(Boolean*)&charMapPtr[byteSplitter.Bytes[6]])
8564
{
86-
buffer[i++] = byteSplitter.Byte7;
65+
buffer[i++] = byteSplitter.Bytes[6];
8766
if (i == length) return true;
8867
}
8968

90-
if (Settings.CharMap[byteSplitter.Byte8])
69+
if (*(Boolean*)&charMapPtr[byteSplitter.Bytes[7]])
9170
{
92-
buffer[i++] = byteSplitter.Byte8;
71+
buffer[i++] = byteSplitter.Bytes[7];
9372
if (i == length) return true;
9473
}
9574

Generator/_Generate.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@ internal unsafe static String Generate(UInt16 length)
77
{
88
Span<Byte> buffer = stackalloc Byte[length];
99

10-
if (!FillBuffer(ref buffer, length))
10+
fixed (Byte* charMapPtr = Settings.CharMap)
1111
{
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!;
12+
if (!CharMapIsNotAllZero(charMapPtr))
13+
{
14+
MessageBox.Show("Failed to generate password, no characters selected!\n" +
15+
"Select at least one character in the settings.", "Generator", MessageBoxButtons.OK, MessageBoxIcon.Information);
16+
return null!;
17+
}
18+
19+
if (!FillBuffer(ref buffer, length, charMapPtr))
20+
{
21+
MessageBox.Show("Failed to fill buffer with random bytes,\n" +
22+
"this usually only happens in very (very) rare cases.\n\n" +
23+
"Source overloaded?", "RNG Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
24+
return null!;
25+
}
1426
}
1527

1628
if (Settings.EncodeBase64) return BufferToBase64String(ref buffer, length, Settings.ExactLength, Settings.TrimEncodeBase64);
111 KB
Binary file not shown.

MainWindow/AddControls.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal static partial class MainWindow
5757
Width = 26,
5858
};
5959

60-
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
60+
/********************************************************************/
6161

6262
private static void AddControls(Form mainForm)
6363
{

MainWindow/ButtonHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal static partial class MainWindow
77
{
88
private static String? _generatorOutput;
99

10-
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
10+
/********************************************************************/
1111

1212
private static void Generate(Object? sender, EventArgs e)
1313
{

MainWindow/_Run.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ internal static partial class MainWindow
99
[return: MarshalAs(UnmanagedType.Bool)]
1010
private static partial Boolean SetProcessDPIAware();
1111

12-
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
12+
/********************************************************************/
1313

1414
internal static FontFamily? FontFamily;
1515

16-
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
16+
/********************************************************************/
1717

1818
internal unsafe static void Run()
1919
{

Properties/AssemblyInfo.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
using System.Reflection;
22
using System.Runtime.Versioning;
3-
using System.Runtime.InteropServices;
43

5-
// General Information about an assembly is controlled through the following
6-
// set of attributes. Change these attribute values to modify the information
7-
// associated with an assembly.
8-
[assembly: AssemblyTitle("Generate Passwords with the CPU RNG")]
4+
// description
5+
[assembly: AssemblyTitle("CPU Password Generator - Generate Passwords with the CPU RNG")]
96
[assembly: AssemblyDescription("")]
107
[assembly: AssemblyConfiguration("")]
118
[assembly: AssemblyCompany("")]
9+
// product name
1210
[assembly: AssemblyProduct("CPU Password Generator")]
1311
[assembly: AssemblyCopyright("")]
1412
[assembly: AssemblyTrademark("")]
1513
[assembly: TargetPlatform("Windows10.0.26100.0")]
1614
[assembly: SupportedOSPlatform("Windows10.0.19041.0")]
1715
[assembly: AssemblyCulture("")]
1816

19-
// Setting ComVisible to false makes the types in this assembly not visible
20-
// to COM components. If you need to access a type in this assembly from
21-
// COM, set the ComVisible attribute to true on that type.
22-
[assembly: ComVisible(false)]
23-
24-
// The following GUID is for the ID of the typelib if this project is exposed to COM
25-
[assembly: Guid("47c5c09b-16bf-479f-8e30-9b9270e606e0")]
26-
27-
[assembly: AssemblyVersion("1.0.1.0")]
28-
[assembly: AssemblyFileVersion("1.0.1.0")]
29-
[assembly: AssemblyInformationalVersion("1.0.1.0")]
17+
[assembly: AssemblyVersion("1.1.0.0")]
18+
[assembly: AssemblyFileVersion("1.1.0.0")]
19+
[assembly: AssemblyInformationalVersion("1.1.0.0")]

0 commit comments

Comments
 (0)