Skip to content

Commit cd5cfcb

Browse files
Ensure the consistency of targeting evaluation across CPU architectures. (#405)
* check system endianness * remove period * update reverse method * update comment * adjust comment
1 parent dd0c9e3 commit cd5cfcb

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Microsoft.FeatureManagement/Targeting/TargetingEvaluator.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,23 @@ private static bool IsTargeted(string contextId, double from, double to)
338338
{
339339
byte[] hash;
340340

341+
//
342+
// Cryptographic hashing algorithms ensure adequate entropy across hash
341343
using (HashAlgorithm hashAlgorithm = SHA256.Create())
342344
{
343345
hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(contextId));
344346
}
345347

348+
346349
//
347-
// Use first 4 bytes for percentage calculation
348-
// Cryptographic hashing algorithms ensure adequate entropy across hash
350+
// Endianness check ensures the consistency of targeting evaluation result across different architectures
351+
if (!BitConverter.IsLittleEndian)
352+
{
353+
Array.Reverse(hash, 0, 4);
354+
}
355+
356+
//
357+
// The first 4 bytes of the hash will be used for percentage calculation
349358
uint contextMarker = BitConverter.ToUInt32(hash, 0);
350359

351360
double contextPercentage = (contextMarker / (double)uint.MaxValue) * 100;

0 commit comments

Comments
 (0)