SipHash 1.0.0

dotnet add package SipHash --version 1.0.0
                    
NuGet\Install-Package SipHash -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SipHash" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SipHash" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SipHash" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SipHash --version 1.0.0
                    
#r "nuget: SipHash, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SipHash@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SipHash&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SipHash&version=1.0.0
                    
Install as a Cake Tool

SipHash (.NET 8)

License: MIT nuget

Compact, high-performance SipHash-2-4 implementation and a small DI-ready service for .NET 8.

  • Language: C# 12
  • Target: .NET 8

Overview

This library exposes:

  • SipHashCore — a pure static implementation of SipHash-2-4.
  • SipHashService — a singleton-friendly service that stores the two 64-bit keys in pinned memory and exposes ComputeHash(ReadOnlySpan<byte>).
  • SipHashOptions — simple options POCO for keys.
  • ServiceCollectionExtensions.AddSipHash — helper to register the service with the Microsoft DI container.

The implementation is optimized for speed (span-based APIs, Unsafe.ReadUnaligned, AggressiveInlining). Keys are stored using PinnedMemory<ulong> to avoid GC relocation; review PinnedMemory behavior for zeroing/locking guarantees in your security model.

Installation

Add the project to your solution or reference the assembly/NuGet package that contains the SipHash implementation.

Quick usage

  1. Configure keys in DI (recommended):
// Program.cs
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SipHash;

var host = Host.CreateDefaultBuilder()
    .ConfigureServices((ctx, services) =>
    {
        services.Configure<SipHashOptions>(opts =>
        {
            opts.K0 = 0x0123456789abcdefUL;
            opts.K1 = 0xfedcba9876543210UL;
        });

        services.AddSipHash(); // registers SipHashService as singleton
    })
    .Build();

// resolve & use
var svc = host.Services.GetRequiredService<SipHashService>();
byte[] data = System.Text.Encoding.UTF8.GetBytes("hello");
ulong tag = svc.ComputeHash(data);
  1. Use directly (no DI):
ulong k0 = 0x0123456789abcdefUL;
ulong k1 = 0xfedcba9876543210UL;
byte[] data = System.Text.Encoding.UTF8.GetBytes("hello");
ulong tag = SipHashCore.Hash(k0, k1, data);

Note: SipHashCore.Hash accepts any ReadOnlySpan<byte> (byte[] or Memory<byte>).

API Reference

  • SipHashService : IDisposable

    • Constructor: SipHashService(IOptions<SipHashOptions> options) — validates presence of K0 and K1 and pins them in memory.
    • ulong ComputeHash(ReadOnlySpan<byte> data) — computes SipHash-2-4 tag for the given data. Throws ObjectDisposedException if disposed.
    • void Dispose() — disposes pinned memory. After disposal, service methods throw.
  • SipHashCore (static)

    • static ulong Hash(ulong k0, ulong k1, ReadOnlySpan<byte> msg) — pure function implementing SipHash-2-4. Uses little-endian packing.
  • SipHashOptions

    • ulong? K0 { get; set; }
    • ulong? K1 { get; set; }
  • ServiceCollectionExtensions

    • IServiceCollection AddSipHash(this IServiceCollection services) — registers SipHashService as a singleton using configured SipHashOptions.

Thread-safety

ComputeHash uses local stack state and reads keys from pinned memory. As implemented, SipHashService is safe for concurrent callers provided:

  • PinnedMemory<ulong>.Read(int) is thread-safe for concurrent reads.
  • No mutation of keys occurs after construction.

Security considerations

  • Keys are pinned to avoid relocation, but this does not prevent process memory disclosure (core dumps, swap, memory scanners). Treat keys as sensitive.
  • Review PinnedMemory behavior for whether it zeros memory on dispose; current constructor uses zero: false so initial allocation is not zeroed. If you require explicit zeroing, ensure PinnedMemory is configured accordingly or zero keys manually before dispose.
  • Register the service as singleton only if the keys' lifetime aligns with application lifetime and DI usage.

Implementation notes

  • The code implements SipHash-2-4 (2 compression rounds, 4 finalization rounds).
  • The implementation assumes little-endian byte order when packing/unpacking 64-bit words (typical for x86/x64). If targeting big-endian platforms, validate behavior.
  • For maximum performance the implementation uses Unsafe.ReadUnaligned and MemoryMarshal.

Examples

byte[] data = System.Text.Encoding.UTF8.GetBytes("example");
ulong tag = SipHashCore.Hash(k0, k1, data);
string hex = tag.ToString("x16");

Contributing

Contributions are welcome. Open issues or PRs with focused changes: tests, API improvements, or security-hardening (zeroing keys, secure memory).

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SipHash:

Package Downloads
Guid74

Guid74 implements a reversible relationship between UUIDv7 (time-ordered identifiers) and a deterministic UUIDv4 facade. Depends on SipHash, optimized for PinnedMemory.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 179 11/1/2025