diff --git a/src/Microsoft.FeatureManagement/Targeting/TargetingEvaluator.cs b/src/Microsoft.FeatureManagement/Targeting/TargetingEvaluator.cs index cc809e27..5c8ed8b1 100644 --- a/src/Microsoft.FeatureManagement/Targeting/TargetingEvaluator.cs +++ b/src/Microsoft.FeatureManagement/Targeting/TargetingEvaluator.cs @@ -343,14 +343,23 @@ private static bool IsTargeted(string contextId, double from, double to) { byte[] hash; + // + // Cryptographic hashing algorithms ensure adequate entropy across hash using (HashAlgorithm hashAlgorithm = SHA256.Create()) { hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(contextId)); } + // - // Use first 4 bytes for percentage calculation - // Cryptographic hashing algorithms ensure adequate entropy across hash + // Endianness check ensures the consistency of targeting evaluation result across different architectures + if (!BitConverter.IsLittleEndian) + { + Array.Reverse(hash, 0, 4); + } + + // + // The first 4 bytes of the hash will be used for percentage calculation uint contextMarker = BitConverter.ToUInt32(hash, 0); double contextPercentage = (contextMarker / (double)uint.MaxValue) * 100;