Skip to content
Closed
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
Expand Up @@ -90,7 +90,7 @@ public static string HashDataToString(ReadOnlySpan<object?> values, JsonSerializ

// For cases where the hash may be used as a cache key, we rely on collision resistance for security purposes.
// If a collision occurs, we'd serve a cached LLM response for a potentially unrelated prompt, leading to information
// disclosure. Use of SHA256 is an implementation detail and can be easily swapped in the future if needed, albeit
// disclosure. Use of SHA384 is an implementation detail and can be easily swapped in the future if needed, albeit
// invalidating any existing cache entries.
#if NET
IncrementalHashStream? stream = IncrementalHashStream.ThreadStaticInstance;
Expand All @@ -107,7 +107,7 @@ public static string HashDataToString(ReadOnlySpan<object?> values, JsonSerializ
stream = new();
}

Span<byte> hashData = stackalloc byte[SHA256.HashSizeInBytes];
Span<byte> hashData = stackalloc byte[SHA384.HashSizeInBytes];
try
{
foreach (object? value in values)
Expand All @@ -133,8 +133,8 @@ public static string HashDataToString(ReadOnlySpan<object?> values, JsonSerializ
JsonSerializer.Serialize(stream, value, jti);
}

using var sha256 = SHA256.Create();
var hashData = sha256.ComputeHash(stream.GetBuffer(), 0, (int)stream.Length);
using var hashAlgorithm = SHA384.Create();
var hashData = hashAlgorithm.ComputeHash(stream.GetBuffer(), 0, (int)stream.Length);

return ConvertToHexString(hashData);

Expand Down Expand Up @@ -185,7 +185,7 @@ private sealed class IncrementalHashStream : Stream
public static IncrementalHashStream? ThreadStaticInstance;

/// <summary>The <see cref="IncrementalHash"/> used by this instance.</summary>
private readonly IncrementalHash _hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
private readonly IncrementalHash _hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA384);

/// <summary>Gets the current hash and resets.</summary>
public void GetHashAndReset(Span<byte> bytes) => _hash.GetHashAndReset(bytes);
Expand Down
Loading