FileSigCheck 2.0.1
dotnet add package FileSigCheck --version 2.0.1
NuGet\Install-Package FileSigCheck -Version 2.0.1
<PackageReference Include="FileSigCheck" Version="2.0.1" />
<PackageVersion Include="FileSigCheck" Version="2.0.1" />
<PackageReference Include="FileSigCheck" />
paket add FileSigCheck --version 2.0.1
#r "nuget: FileSigCheck, 2.0.1"
#:package FileSigCheck@2.0.1
#addin nuget:?package=FileSigCheck&version=2.0.1
#tool nuget:?package=FileSigCheck&version=2.0.1
File Signature Checker
This .NET Standard 2.0 library contains code for checking file signatures.
File signatures are tell-tale signs of a file format, usually a part of the file's header in the file contents, which can be used to determine the format or metadata of the file.
The file signatures were taken from this helpful resource: https://filesignatures.net/ (Thanks to them!) and also from Wikipedia.
Supported Extensions
It currently contains file signatures for the following extensions:
- .png
- .jpg
- .jpeg
- .tif
- .tiff
- .webp
- .gif
- .doc
- .docx
- .ppt
- .pptx
- .xls
- .xlsx
- .rtf
- .mp3
- .wav
- .avi
- .ogg
- .mpg
- .m4a
- .xml
- .rar
- .exe
- .blend
I will add more signatures if they are requested, or if I desire them. I will also consider pull requests which add more file signatures. If you do add more signatures, please provide a reference to the source of the signature definition within the pull request.
Base 64 Images
There is a method for checking base 64 strings Data URIs to see whether they are valid images or audio clips.
Use as bool result = FileSignatureUtil.IsDataUriSignatureValid(myDataUri);
It will use the mime-type at the beginning of the Data URI to determine which signature to check. So image.png
will compare to a .png
file.
Currently, it only works for image/jpeg, image/png, image/gif, image/tiff, audio/ogg an audio/mpeg. Help is wanted to expand this list.
Usage
Once you have a file stream accessible, if you want to check against the file name's extension, use:
bool result = FileSignatureUtil.IsFileSignatureValid("myImage.png", filestream);
Otherwise you can specify a set of extensions and that the data should match any of them:
static readonly string[] fileExtensions = { ".pdf", ".jpg", ".jpeg", ".png" };
bool result = FileSignatureUtil.IsFileSignatureValid(filestream, fileExtensions);
A typical scenario in which this might be used is with a file upload in .NET Core API:
static readonly string[] fileExtensions = { ".pdf", ".jpg", ".jpeg", ".png" };
public async Task<IActionResult> UploadImage([FromForm]IFormFile file)
{
var ext = Path.GetExtension(file.FileName);
/* Check the file extension in permissible file types */
if(!fileExtensions.Contains(ext))
{
return StatusCode(StatusCodes.Status415UnsupportedMediaType);
}
/* Get the file data */
using (var rs = file.OpenReadStream())
{
/* perform check */
if (!FileSignatureUtil.IsFileSignatureValid(rs, fileExtensions))
{
ModelState.AddModelError("File", $"Is not recognised as a {ext} file.");
return BadRequest(ModelState);
}
/* Then do what you would normally do with that file... */
}
return Ok();
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.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.