Repository: dotnet/aspnetcore
File: src/Components/Endpoints/src/Builder/ResourceCollectionUrlEndpoint.cs (C#)

Code before:
        foreach (var resource in resources)
        {
            var url = resource.Url;
            AppendToHash(incrementalHash, buffer, ref rented, url);

            // For non-fingerprinted assets (those without a 'label' property), we need to include
            // the integrity hash in the fingerprint calculation. This ensures that if the content
            // changes between builds, the resource-collection fingerprint also changes.
            if (resource.Properties != null)
            {
                var hasLabel = false;
                string? integrity = null;
                foreach (var property in resource.Properties)
                {
                    if (property.Name.Equals("label", StringComparison.OrdinalIgnoreCase))
                    {
                        hasLabel = true;
                        // No need to continue if we found a label - we won't use integrity
                        break;
                    }
                    else if (property.Name.Equals("integrity", StringComparison.OrdinalIgnoreCase))
                    {
                        integrity = property.Value;
                        // Continue searching in case there's also a label property
                    }
                }

                // If there's no label (non-fingerprinted) but there is an integrity value, include it
                if (!hasLabel && integrity != null)
                {
                    AppendToHash(incrementalHash, buffer, ref rented, integrity);
                }
            }
        }
        incrementalHash.GetCurrentHash(result);
        // Base64 encoding at most increases size by (4 * byteSize / 3 + 2),
        // add an extra byte for the initial dot.
        Span<char> fingerprintSpan = stackalloc char[(incrementalHash.HashLengthInBytes * 4 / 3) + 3];
        var length = WebUtilities.WebEncoders.Base64UrlEncode(result, fingerprintSpan[1..]);
        fingerprintSpan[0] = '.';
        return fingerprintSpan[..(length + 1)].ToString();
    }

    private static void AppendToHash(IncrementalHash incrementalHash, Span<byte> buffer, ref byte[]? rented, string value)
    {
        if (Encoding.UTF8.TryGetBytes(value, buffer, out var written))
        {
            incrementalHash.AppendData(buffer[..written]);
        }
        else
        {

Review comment (refactor):
Move this into a helper method that returns a string and then have the check be against the returned string. If there is more data we need to hash we will return a non-null string. Otherwise, we return null and skip.

Code after:
        foreach (var resource in resources)
        {
            var url = resource.Url;
            AppendToHash(incrementalHash, buffer, ref rented, url);

            var additionalData = GetAdditionalDataForFingerprint(resource);
            if (additionalData is not null)
            {
                AppendToHash(incrementalHash, buffer, ref rented, additionalData);
            }
        }
        incrementalHash.GetCurrentHash(result);
        // Base64 encoding at most increases size by (4 * byteSize / 3 + 2),
        // add an extra byte for the initial dot.
        Span<char> fingerprintSpan = stackalloc char[(incrementalHash.HashLengthInBytes * 4 / 3) + 3];
        var length = WebUtilities.WebEncoders.Base64UrlEncode(result, fingerprintSpan[1..]);
        fingerprintSpan[0] = '.';
        return fingerprintSpan[..(length + 1)].ToString();
    }

    private static string? GetAdditionalDataForFingerprint(ResourceAsset resource)
    {
        // For non-fingerprinted assets (those without a 'label' property), we need to include
        // the integrity hash in the fingerprint calculation. This ensures that if the content
        // changes between builds, the resource-collection fingerprint also changes.
        if (resource.Properties is null)
        {
            return null;
        }

        var hasLabel = false;
        string? integrity = null;
        foreach (var property in resource.Properties)
        {
            if (property.Name.Equals("label", StringComparison.OrdinalIgnoreCase))
            {
                hasLabel = true;
                // No need to continue if we found a label - we won't use integrity
                break;
            }
            else if (property.Name.Equals("integrity", StringComparison.OrdinalIgnoreCase))
            {
                integrity = property.Value;
                // Continue searching in case there's also a label property
            }
        }

        // If there's no label (non-fingerprinted) but there is an integrity value, return it
        return !hasLabel ? integrity : null;
    }