Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Security.Cryptography;
using System.Text;
using LaunchDarkly.Sdk.Server.Internal.Model;

Expand All @@ -9,11 +8,9 @@ namespace LaunchDarkly.Sdk.Server.Internal.BigSegments
{
internal static class BigSegmentsInternalTypes
{
private static readonly SHA256 _hasher = SHA256.Create();

internal static string BigSegmentContextKeyHash(string userKey) =>
Convert.ToBase64String(
_hasher.ComputeHash(Encoding.UTF8.GetBytes(userKey))
LdSha256.HashData(Encoding.UTF8.GetBytes(userKey))
);

internal static string MakeBigSegmentRef(Segment s) =>
Expand Down
38 changes: 38 additions & 0 deletions pkgs/sdk/server/src/Internal/LDSha256.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Security.Cryptography;

namespace LaunchDarkly.Sdk.Server.Internal
{
// Hasher which uses HashData on .NET 5.0+ platforms and ComputeHash on older platforms.
// HashData is static and thread-safe offering better performance when available.
#if NET5_0_OR_GREATER
/// <summary>
/// Thread-safe hasher using SHA256.HashData for .NET 5.0+ platforms.
/// </summary>
internal static class LdSha256
{
public static byte[] HashData(byte[] data) {
return SHA256.HashData(data);
}
}
#else
/// <summary>
/// Thread-safe hasher using SHA256.ComputeHash for .NET Framework and .netstandard targets.
/// </summary>
/// <remarks>
/// This hasher creates a SHA256 instance per-call. This is likely to perform better under high parallelism
/// than locking. With low parallelism, locking would have lower overhead and exert lower pressure on the GC.
/// The pre-existing evaluation algorithm was already using per-call SHA1 instances, so this should have
/// reasonable performance characteristics.
/// </remarks>
internal static class LdSha256
{
public static byte[] HashData(byte[] data)
{
using (var hasher = SHA256.Create())
{
return hasher.ComputeHash(data);
}
}
}
#endif
}
Loading