Recursive AI

Create a reproducible TAR archive in C#

OneArchive is a free, open-source archive and compression library for .NET from Recursive AI: zip, tar, 7z, RAR and more in RecursiveAI.OneArchive, with compression codecs in RecursiveAI.OneCompress. MIT or Apache-2.0, no native dependencies.

Set Normalize = true to make repeat builds byte-identical.

Terminal
dotnet add package RecursiveAI.OneArchive

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneArchive;
 
File.WriteAllText("a.txt", "Alpha");
var opts = new ArchiveCreateOptions
    { Normalize = true };
Archive.Create("bundle.tar", opts)
    .Add("a.txt").Save();
Console.WriteLine("Wrote bundle.tar");
Text
Wrote bundle.tar

Unzip the example, then dotnet run.

The full program below builds two TARs and checks that they have the same SHA-256 hash.

The full program

This version writes first.tar and second.tar and prints whether their SHA-256 hashes match.

C#
using System.Security.Cryptography;
using RecursiveAI.OneArchive;
 
File.WriteAllText("a.txt", "Alpha");
File.WriteAllText("b.txt", "Beta");
var opts = new ArchiveCreateOptions
{
    Normalize = true
};
Archive.Create("first.tar", opts)
    .Add("b.txt").Add("a.txt").Save();
File.SetLastWriteTimeUtc("a.txt",
    DateTime.UnixEpoch.AddDays(1));
Archive.Create("second.tar", opts)
    .Add("a.txt").Add("b.txt").Save();
Console.WriteLine(Hash("first.tar") ==
                  Hash("second.tar"));
 
static string Hash(string path) =>
    Convert.ToHexString(SHA256.HashData(
        File.ReadAllBytes(path)));

dotnet run prints:

Text
True

Both archives contain a.txt (Alpha) and b.txt (Beta). The second build reverses the Add calls and changes the modification time of a.txt, not its contents. Keep the output TARs outside any input folder you package, so you don’t accidentally include an earlier build.

Variations

Set a fixed release date. Replace the options in the full program with these. The comparison still prints True because both builds use the same timestamp.

C#
var opts = new ArchiveCreateOptions
{
    Normalize = true,
    NormalizedTime = DateTimeOffset.UnixEpoch
        .AddDays(100)
};

The short answer

Set ArchiveCreateOptions.Normalize to true and pass the options to Archive.Create for each TAR. OneArchive uses a stable ordinal sort on entry names and writes normalized metadata instead of the source timestamps. Rebuild from the same file contents and entry names, then compare the SHA-256 hashes of the two archives to check that the result didn’t change.

Why do TAR hashes change when the files haven’t changed?

TAR stores a header for each entry as well as its contents. A new modification time changes header bytes even when the file itself is unchanged. Putting the same entries in a different order also changes the archive bytes. Without normalization, OneArchive records separate Add calls in call order and uses timestamps from the file system. With normalization, the modification time defaults to January 1, 1970 UTC, owners are set to numeric zero with no names, and access and change times are dropped. Set NormalizedTime if your builds need a different common date.

What still has to match for a reproducible TAR?

Keep the entry names, file contents, link targets, and writer options the same. Normalization doesn’t make executable and non-executable files equivalent: the builder keeps the distinction by writing a mode of 0755 for regular files with any execute bit and 0644 for other regular files. Directories get 0755 and symbolic links 0777. If two entries have the same name, the stable sort keeps their relative order; give entries unique paths if your build can discover files in a different order on each run.

Compare like with like: the example hashes two plain .tar files using the same options, not a TAR and a compressed copy. If the hashes differ, compare the archive entry names and metadata before assuming the file contents changed.

All onearchive recipes