MagicBytesValidator 2.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package MagicBytesValidator --version 2.0.1                
NuGet\Install-Package MagicBytesValidator -Version 2.0.1                
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="MagicBytesValidator" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MagicBytesValidator --version 2.0.1                
#r "nuget: MagicBytesValidator, 2.0.1"                
#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.
// Install MagicBytesValidator as a Cake Addin
#addin nuget:?package=MagicBytesValidator&version=2.0.1

// Install MagicBytesValidator as a Cake Tool
#tool nuget:?package=MagicBytesValidator&version=2.0.1                

MagicBytesValidator

Recognize filetypes from Streams or IFormfiles using mime types or file extensions and validate them against the magic bytes according to the filetypes. The existing FileTypes can be expanded in various ways.

How to install?

  • Install nuget package into your project:
Install-Package MagicBytesValidator -Version 2.0.0
dotnet add package MagicBytesValidator --version 2.0.0
  • Reference in your csproj:
<PackageReference Include="MagicBytesValidator" Version="2.0.0" />

How to use it?

  • Create new instances of the validator & providers:
var validator = new MagicBytesValidator.Services.Validator();
var formFileTypeProvider = new MagicBytesValidator.Services.Http.FormFileTypeProvider();
var streamFileTypeProvider = new MagicBytesValidator.Services.Streams.StreamFileTypeProvider();
  • Find a filetype by extension or mimetype:
var pngFileType = validator.Mapping.FindByExtension("png");
var pdfFileType = validator.Mapping.FindByMimeType("application/pdf");
  • Determine & validate a filetype by uploaded IFormFile:
var fileType = await formFileTypeProvider.FindValidatedTypeAsync(formFile, null, CancellationToken.None);
  • Determine the file type of a file by its stream
var fileType = await streamFileTypeProvider.TryFindUnambiguousAsync(fileStream, CancellationToken.None);
  • Check a file with its stream and filetype:
var isValid = await validator.IsValidAsync(memoryStream, fileType, CancellationToken.None);
Expand the filetype mapping
  • Get mapping:
// use the validator:
var mapping = validator.Mapping;

// use the formFileTypeProvider:
var mapping = formFileTypeProvider.Mapping;

// or create a new instance of the mapping:
var mapping = new MagicBytesValidator.Services.Mapping(); 
  • Register a single Filetype:
mapping.Register(
    new FileByteFilter(
        "traperto/trp",  // mime type
        new[] { "trp" } // file extensions
    ) {
        // magic byte sequences
        StartsWith([
            0x78, 0x6c, 0x2f, 0x5f, 0x72, 0x65
        ])
            .EndsWith([
            0xFF, 0xFF
        ])
    }
)
  • FileTypes with specific offset checks:
mapping.Register(
    new FileByteFilter(
        "traperto/trp",  // mime type
        new[] { "trp" } // file extensions
    ) {
        // magic byte sequences
        Specific(new ByteCheck(512, [0xFD]));
    }
)

ByteCheck allows for negative offset values to look for a specific offset counting from the end of file

  • Register a list of filetypes:
mapping.Register(listOfFileTypes);

You can also create variants of IFileType and register them by passing the Assembly of the new FileTypes, e.g. mapping.Register(typeof(CustomFileType).Assembly);. This will register all FileTypes of the given Assembly that are also not abstract and have an empty constructor!

public class CustomFileType : FileTypeWithStartSequences
{
    public CustomFileType() : base(
        "traperto/trp",  // mime type
        new[] { "trp" }, // file extensions
        new[] {          // magic byte sequences
            new byte[] { 0x74, 0x72, 0x61, 0x70, 0x65, 0x72, 0x74, 0x6f }
        }
    )
    {
    }
}

var assembly = typeof(CustomFileType).Assembly;
_mapping.Register(assembly);

CLI

There's a CLI tool (MagicBytesValidator.CLI) which can be used to determine MIME types for a local file by calling the following command.

dotnet run --project MagicBytesValidator.CLI -- [PATH]

This can be useful when debugging or validating newly added FileTypes.

List of Filetypes

Mimetype Extension Magicbytes (decimal)
audio/x-pn-realaudio-plugin rpm 237 171 238 219
application/octet-stream bin<br />file<br />com<br />class<br />ini <ul><li>83 80 48 49</li><li>201</li><li>202 254 186 190</li></ul>
video/3gpp 3gp 102 116 121 112 51 103
image/x-icon ico 0 0 1 0
image/gif gif <ul><li>71 73 70 56 55 97</li><li>71 73 70 56 57 97</li></ul>
image/tiff tif<br />tiff <ul><li>73 73 42 0</li><li>77 77 0 42</li></ul>
image/jpeg jpg<br />jpeg<br />jpe <ul><li>255 216 255 219</li><li>255 216 255 224 0 16 74</li><li>70 73 70 0 1</li><li>255 216 255 238</li><li>105 102 0 0</li></ul>
image/png png 137 80 78 71 13 10 26 10
video/ogg ogg<br />ogv 79 103 103 83
audio/basic snd<br />au <ul><li>56 83 86 88</li><li>65 73 70 70</li></ul>
application/dsptype tsp 77 90
text/plain txt <ul><li>239 187 191</li><li>255 254</li><li>254 255</li><li>255 254 0 0</li></ul>
application/zip zip 80 75 3 4
application docx<br />xlsx 80 75 7 8
application/vnd.oasis.opendocument.presentation odp 80 75 7 8
application/vnd.oasis.opendocument.spreadsheet ods 80 75 7 8
application/vnd.oasis.opendocument.text odt 80 75 7 8
audio/mpeg mp3 73 68 51
image/bmp bmp 66 77
audio/x-midi midi<br />mid 77 84 104 100
application/msword doc<br />dot 208 207 17 224 161 177 26 255
application/msexcel xlx<br />xla 208 207 17 224 161 177 26 255
application/mspowerpoint ppt<br />ppz<br />pps<br />pt 208 207 17 224 161 177 26 225
application/gzip gz 31 139
video/webm webm 26 69 223 163
application/rtf rtf 123 92 114 116 102 49
text/tab-separated-values tsv 71
video/mpeg mpg<br />mpeg<br />mpe <ul><li>71</li><li>0 0 1 186</li><li>0 0 1 179</li></ul>
video/mp4 mp4 <ul><li>102 116 121 112 105 115 111 109</li><li>102 116 121 112 109 112 52 50</li><li>102 116 121 112 77 83 62 86</li></ul>
image/x-portable-bitmap pbm 80 49 10
image/x-portable-graymap pgm 80 50 10
image/x-portable-pixmap ppm 80 51 10
application/pdf pdf 25 50 44 46

What is the licence?

MIT License

▓▓  ▓▓▓▓▓▓▓▓▓▓
▓▓          ▓▓
▓▓▓▓▓▓▓▓▓   ▓▓
▓▓          ▓▓            traperto GmbH
▓▓  ▓▓▓▓▓▓▓▓▓▓
▓▓
▓▓▓▓▓▓▓▓▓   ▓▓
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.2 1,076 8/21/2024
2.0.1 148 8/20/2024
2.0.0 330 8/7/2024
1.0.16 5,987 10/18/2023
1.0.15 3,262 9/11/2023
1.0.14 346 6/30/2023
1.0.13 4,976 3/3/2023
1.0.12 260 2/28/2023
1.0.11 365 2/14/2023
1.0.10 9,392 8/3/2022
1.0.9 489 7/29/2022
1.0.8 461 7/28/2022
1.0.7 456 7/26/2022
1.0.6 492 7/14/2022
1.0.5 400 7/14/2022
1.0.4 408 7/12/2022
1.0.3 449 7/12/2022
1.0.2 2,566 10/5/2021
1.0.1 443 10/3/2021