Skip to content

Commit 187f0e9

Browse files
committed
New build options: MvsSlnFeatureGuidSha1 and MvsSlnFeatureHuid
Part of Issue #51 MvsSlnFeatureHuid via Huid 1.0.0 https://github.com/3F/Huid/releases/tag/1.0
1 parent 63ad0f2 commit 187f0e9

File tree

6 files changed

+133
-29
lines changed

6 files changed

+133
-29
lines changed

.vssbe

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@
5555
"$type": "net.r_eg.vsSBE.Events.ModeScript, vsSolutionBuildEvent",
5656
"Type": "Script",
5757
"Command__": [
58-
"#[\" Packages \"]",
58+
"~ Packages",
5959
"",
6060
"#[( !(#[IO exists.file(\"packages/__checked\")]) )",
6161
"{",
62-
" #[File call(\".tools\\hmsbuild.bat\", \"~c $(Configuration) -t:restore /v:q /nologo\", 300)]",
63-
" ",
64-
" ",
6562
" #[IO copy.directory(\"\", \"packages/\", true)]",
6663
" #[File write(\"packages/__checked\"): ]",
67-
"}]"
64+
"}]",
65+
"",
66+
"Call restore target to update nodes due to different options like MvsSlnFeatureHuid=true etc.",
67+
"#[File scall(\".tools\\hmsbuild.bat\", \"~c $(Configuration) -t:restore /v:q /nologo\", 200)]"
6868
]
6969
}
7070
},

MvsSln/Core/Guids.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* See accompanying License.txt file or visit https://github.com/3F/MvsSln
66
*/
77

8+
using System;
89
using System.Collections.Generic;
910
using System.Linq;
1011
using net.r_eg.MvsSln.Extensions;
@@ -87,6 +88,18 @@ public static class Guids
8788
/// </summary>
8889
public const string PROJECT_CS_SDK = "{9A19103F-16F7-4668-BE54-9A1E7A4F7556}";
8990

91+
/// <summary>
92+
/// Reserved region for MvsSln related purposes.
93+
/// </summary>
94+
public static readonly Guid domainMvsSln = new
95+
(
96+
0xE0B7C8AE,
97+
0x3333,
98+
0x4623,
99+
0xAE, 0xB8,
100+
0x7D, 0xEC, 0xF9, 0x9D, 0xD4, 0x00
101+
);
102+
90103
private readonly static Dictionary<ProjectType, string> projectTypeGuids = new Dictionary<ProjectType, string>()
91104
{
92105
{ ProjectType.Cs, PROJECT_CS },

MvsSln/Extensions/StringExtension.cs

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.IO;
11-
using System.Security.Cryptography;
1211
using System.Text;
12+
using net.r_eg.MvsSln.Core;
13+
14+
#if FEATURE_HUID
15+
using net.r_eg.hashing;
16+
#else
17+
using System.Security.Cryptography;
18+
#endif
1319

1420
namespace net.r_eg.MvsSln.Extensions
1521
{
@@ -18,23 +24,75 @@ namespace net.r_eg.MvsSln.Extensions
1824
public static class StringExtension
1925
{
2026
/// <summary>
21-
/// Gets Guid from hash by any string.
27+
/// Get <see cref="System.Guid"/> for input string using specified hashing algorithm*
2228
/// </summary>
23-
/// <param name="str">String for calculating.</param>
24-
/// <returns></returns>
29+
/// <remarks>
30+
/// *Huid (Fnv-1a-128 (via LX4Cnh)), SHA-1, or MD5; depending on compilation options.
31+
///
32+
/// <br/><br/>
33+
/// Note: Huid and SHA-1 hashing works in <see cref="Guids.domainMvsSln"/> (the base),
34+
/// while implementation on MD5 uses initial vector.
35+
///
36+
/// <br/><br/>
37+
/// https://github.com/3F/MvsSln/issues/51
38+
/// </remarks>
39+
/// <param name="str">Any string data to generate <see cref="System.Guid"/></param>
40+
/// <returns>Either parsed GUID from string or new generated using specified hashing algorithm*</returns>
2541
public static Guid Guid(this string str)
2642
{
27-
if(System.Guid.TryParse(str, out Guid res)) {
28-
return res;
29-
}
43+
if(System.Guid.TryParse(str, out Guid res)) return res;
3044

31-
if(str == null) {
32-
str = String.Empty;
33-
}
45+
str ??= string.Empty;
3446

35-
using(MD5 md5 = MD5.Create()) {
36-
return new Guid(md5.ComputeHash(Encoding.UTF8.GetBytes(str)));
37-
}
47+
// Note about FIPS https://github.com/3F/DllExport/issues/171#issuecomment-752043556
48+
49+
#if FEATURE_HUID
50+
51+
return Huid.NewGuid(Guids.domainMvsSln, str);
52+
53+
#elif FEATURE_GUID_SHA1
54+
55+
const int _FMT = 16; // The UUID format is 16 octets
56+
57+
byte[] bytes = Encoding.UTF8.GetBytes(str);
58+
59+
using HashAlgorithm alg = SHA1.Create();
60+
61+
alg.TransformBlock(Guids.domainMvsSln.ToByteArray(), 0, _FMT, null, 0);
62+
alg.TransformFinalBlock(bytes, 0, bytes.Length);
63+
64+
byte[] ret = new byte[_FMT];
65+
Array.Copy(alg.Hash, 0, ret, 0, _FMT);
66+
67+
// 6-7 octets, the high field of the timestamp multiplexed with the version number;
68+
// *local byte order
69+
ret[7] &= 0x0F;
70+
ret[7] |= 5 << 4;
71+
/* rfc4122, UUID version ------v
72+
0 1 0 1 5 The name-based version specified in this document
73+
that uses SHA-1 hashing.
74+
*/
75+
76+
// 8 octet, the high field of the clock sequence multiplexed with the variant;
77+
// *reserved
78+
ret[8] &= 0x3F;
79+
ret[8] |= 0x80;
80+
81+
return new Guid(ret);
82+
83+
#else
84+
byte[] bytes = Encoding.UTF8.GetBytes(str);
85+
86+
//Note: legacy version does not use Guids.domainMvsSln
87+
88+
#if NET5_0_OR_GREATER
89+
return new Guid(MD5.HashData(bytes));
90+
#else
91+
using MD5 alg = MD5.Create();
92+
return new Guid(alg.ComputeHash(bytes));
93+
#endif
94+
95+
#endif
3896
}
3997

4098
/// <summary>

MvsSlnTest/Extensions/StringExtensionTest.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,25 @@ public void GuidTest1()
1414
{
1515
string data = " MvsSln_-_v1 ";
1616

17+
#if FEATURE_HUID
18+
19+
Assert.Equal(new Guid("{cbdd6048-feda-8dac-9dc4-c6c857c4abb7}"), data.Guid());
20+
Assert.Equal(new Guid("{e0b7c8ae-3333-8623-aeb8-7decf99dd400}"), "".Guid());
21+
Assert.Equal(new Guid("{7635fb3f-6ae4-8d6e-becf-2165e03ae3e9}"), " ".Guid());
22+
Assert.Equal(new Guid("{e0b7c8ae-3333-8623-aeb8-7decf99dd400}"), ((string)null).Guid());
23+
24+
#elif FEATURE_GUID_SHA1
25+
26+
Assert.Equal(new Guid("{8f78ea2d-50d2-5371-b55d-c09993af4ba8}"), data.Guid());
27+
Assert.Equal(new Guid("{67edabcd-520c-5836-b363-f5b468c6f198}"), "".Guid());
28+
Assert.Equal(new Guid("{e1fcb595-ffed-5081-92f1-64fee31bbaa5}"), " ".Guid());
29+
Assert.Equal(new Guid("{67edabcd-520c-5836-b363-f5b468c6f198}"), ((string)null).Guid());
30+
#else
1731
Assert.Equal(new Guid("{ee265a58-1e72-6c44-60aa-134eaf5c6f9c}"), data.Guid());
1832
Assert.Equal(new Guid("{d98c1dd4-008f-04b2-e980-0998ecf8427e}"), "".Guid());
1933
Assert.Equal(new Guid("{ef8db523-b411-2757-d335-1702515f86af}"), " ".Guid());
2034
Assert.Equal(new Guid("{d98c1dd4-008f-04b2-e980-0998ecf8427e}"), ((string)null).Guid());
35+
#endif
2136
}
2237

2338
[Fact]
@@ -33,10 +48,24 @@ public void GuidSlnFormatTest1()
3348
[Fact]
3449
public void ReformatSlnGuidTest1()
3550
{
51+
#if FEATURE_HUID
52+
53+
Assert.Equal("{E0B7C8AE-3333-8623-AEB8-7DECF99DD400}", "".ReformatSlnGuid());
54+
Assert.Equal("{E0B7C8AE-3333-8623-AEB8-7DECF99DD400}", " ".ReformatSlnGuid());
55+
Assert.Equal("{9939088C-7F2F-8FEE-B5F3-7DEF927816C5}", "invalid".ReformatSlnGuid());
56+
57+
#elif FEATURE_GUID_SHA1
58+
59+
Assert.Equal("{67EDABCD-520C-5836-B363-F5B468C6F198}", "".ReformatSlnGuid());
60+
Assert.Equal("{67EDABCD-520C-5836-B363-F5B468C6F198}", " ".ReformatSlnGuid());
61+
Assert.Equal("{EB71634D-4275-5E2E-9AA2-15419F819ACC}", "invalid".ReformatSlnGuid());
62+
#else
3663
Assert.Equal("{D98C1DD4-008F-04B2-E980-0998ECF8427E}", "".ReformatSlnGuid());
3764
Assert.Equal("{D98C1DD4-008F-04B2-E980-0998ECF8427E}", " ".ReformatSlnGuid());
38-
Assert.Null(((string)null).ReformatSlnGuid());
3965
Assert.Equal("{842DDBFE-FECA-8620-2CB4-399751A8A7E3}", "invalid".ReformatSlnGuid());
66+
#endif
67+
Assert.Null(((string)null).ReformatSlnGuid());
68+
4069
Assert.Equal("{DCE5BB88-7640-4CFB-861D-6CBAA1F6EF0E}", "dce5bb88-7640-4cfb-861d-6cbaa1f6ef0e".ReformatSlnGuid());
4170
Assert.Equal("{D98C1DD4-008F-04B2-E980-0998ECF8427E}", "{d98c1dd4-008f-04b2-e980-0998ecf8427e}".ReformatSlnGuid());
4271
}

build.bat

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
@echo off
22

3-
call .tools\hMSBuild ~x -GetNuTool & (
4-
if [%~1]==[#] exit /B 0
5-
)
3+
call .tools\hMSBuild ~x -GetNuTool & if [%~1]==[#] exit /B 0
64

75
set "reltype=%~1" & if not defined reltype set reltype=Release
8-
call packages\vsSolutionBuildEvent\cim.cmd /v:m /m:7 /p:Configuration=%reltype% || goto err
9-
exit /B
10-
11-
:err
12-
echo Failed build>&2
13-
exit /B 1
6+
packages\vsSolutionBuildEvent\cim.cmd ~x ~c %reltype%

common.props

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@
5858
<DefineConstants Condition="'$(IsNetCoreFamilyTarget)' == 'true'">NETCORE;$(DefineConstants)</DefineConstants>
5959
<DefineConstants Condition="'$(TargetFramework)' == 'netstandard2.0'">NETSTD20;NETSTD;$(DefineConstants)</DefineConstants>
6060
<DefineConstants Condition="'$(TargetFramework)' == 'net40'">NET40;$(DefineConstants)</DefineConstants>
61-
</PropertyGroup>
61+
</PropertyGroup>
62+
63+
<PropertyGroup>
64+
<DefineConstants Condition="'$(MvsSlnFeatureGuidSha1)' == 'true'">FEATURE_GUID_SHA1;$(DefineConstants)</DefineConstants>
65+
<DefineConstants Condition="'$(MvsSlnFeatureHuid)' == 'true'">FEATURE_HUID;$(DefineConstants)</DefineConstants>
66+
<DefineConstants Condition="'$(MvsSlnFeatureExactRopOrderCmp)' == 'true'">FEATURE_EXACT_ROP_ORDER_CMP;$(DefineConstants)</DefineConstants>
67+
<DefineConstants Condition="'$(MvsSlnFeatureCohExt)' == 'true'">FEATURE_COH_EXT;$(DefineConstants)</DefineConstants>
68+
</PropertyGroup>
6269

6370
<ItemGroup Condition="'$(IsNetCoreFamilyTarget)' != 'true'">
6471
<Reference Include="Microsoft.CSharp" />
@@ -69,6 +76,10 @@
6976
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
7077
</ItemGroup>
7178

79+
<ItemGroup Condition="'$(MvsSlnFeatureHuid)' == 'true'">
80+
<PackageReference Include="Huid" Version="1.0.0" />
81+
</ItemGroup>
82+
7283
<PropertyGroup>
7384
<OverridedTFM Condition="$(MvsSlnTFM.Contains('$(TargetFramework)'))=='false'">true</OverridedTFM>
7485
<MicrosoftBuildVersion Condition="'$(MicrosoftBuildVersion)'==''">17.3.2</MicrosoftBuildVersion>

0 commit comments

Comments
 (0)