An implementation of file signature validation as documented in ASP.NET Core file upload document. This library can validate signatures of 518 different file types, all taken from https://filesignatures.net/, as the document suggests.
There are two main ways to use this library:
-
SignHere.FindSignature()
methodvar stream = new MemoryStream(new byte[] { 0xFF, 0xD8, 0xFF, 0xE2, 0xB7, }); // "CANNON EOS JPEG FILE" var signature = SignHere.FindSignature(stream); // Comes out as: // signature.Extension = Extension.JPEG, // signature.MagicBytes = new byte[] { 0xFF, 0xD8, 0xFF, 0xE2 }, // signature.Description = "CANNON EOS JPEG FILE"
-
.Is()
extension methodstream.Is()
extension: Works with anyStream
object. Expects the stream content to be abyte[]
. There are three different overloads to go with, all of them also haveIEnumerable
variants too:stream.Is(Extension)
: Checks byExtension
enum.var stream = new MemoryStream(new byte[] { 0xFF, 0xD8, 0xFF, 0xE2, 0xB7, }) var isJPEG = stream.Is(Extension.JPEG); // true
stream.Is(Category)
: Checks byCategory
. ACategory
is a predefined constant string which is checked againstSignature.Description
.var stream = new MemoryStream(new byte[] { 0x9C, 0xCB, 0xCB, 0x8D, 0x13, 0x75, 0xD2, 0x11 }) var whitelistedCategoryies = new List<Category>() { Category.Video, Category.Audio }; var isVideoOrAudio = stream.Is(whitelistedCategoryies); // false
stream.Is(string)
: Checks bystring
. Matches ifSignature.Description
contains it.var stream = new MemoryStream(new byte[] { 0xE3, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 }) var isAmigaFile = stream.Is("Amiga"); // true
There are also
Is()
overloads that works withIFormFile
and takeExtension
(s),Category
(s) orstring
(s) as parameters.
- Files with no extension are represented as
Extension.None
. - Extensions starting with a number are escaped with an underscore _ in
Extension
enum, since an enum member cannot start with a number. (eg. Extension 3GP is represented asExtension._3GP
) Category
relies on definition field in filesignatures.net (example, JPEG extension). Until all extensions are processed and categorised manually, validating with this type might be deceiving.Category.Video
will only filter the extensions with the word "video" in their descriptions.
- Performance improvements
- Include other file signature databases
- Support for
File
type if requested - Manual assigning of
Category
s - More tests
- Better icon