Return

File Type Verifier

Detects file type by reading magic number (hex signature) and validates against file extension

No files selected
Verification Results
Select files to verify their type and extension match status

Early File Identification (1970s-1980s)

Before standardized magic numbers, operating systems relied exclusively on file extensions or filename patterns to determine file types. This approach proved unreliable as users could rename files arbitrarily, leading to misinterpretation and execution errors. Early versions of Unix began exploring the concept of inspecting file content to identify type, laying groundwork for what would become the "magic number" system.

The term "magic number" originated in the Unix operating system, where the file command was developed to identify file types by looking for specific byte patterns. In 1973, the first version of the file command appeared in Unix Research Version 4, using a simple text database of magic number patterns. This allowed the system to distinguish between binary executables, text files, shell scripts, and other formats regardless of their extension. [Wikipedia, file command]

The Magic Number Database (1986)

In 1986, Ian Darwin wrote a more sophisticated version of the file command that used a configuration file (/etc/magic) to define byte offsets and expected values. This database approach allowed the system to be updated with new file formats without recompiling the tool. The magic database grew to include hundreds of file signatures, from common formats like JPEG and PDF to obscure proprietary formats. [Wikipedia, Magic Number]

The concept of magic numbers was later adopted by many operating systems and programming languages. The libmagic library, derived from the file command, became the standard for MIME type detection in web servers, file managers, and development tools. Today, magic number databases contain thousands of file signatures maintained by the open source community.

Modern Magic Number Detection (2000s-Present)

With the growth of the web and need for accurate MIME type detection, libraries such as file-type (JavaScript), python-magic (Python), and Apache Tika (Java) emerged. These libraries implement the same magic number principles but are optimized for modern applications, providing fast, memory efficient detection. The importance of magic numbers remains critical for security: web browsers use MIME sniffing to prevent malicious files from masquerading as safe content types. [Wikipedia, Content Sniffing]

The Basic Principle: Bytes at Specific Offsets

A magic number is a sequence of bytes at a fixed position (usually the beginning) of a file that uniquely identifies the file format. Different file formats have distinct "signatures" stored in their headers. When a file verifier reads the first few bytes of a file and compares them against a database of known signatures, it can determine the actual file type regardless of the file extension or user assigned name. [Wikipedia, List of file signatures]

Hex Representation and Byte Order

Magic numbers are often represented in hexadecimal (base-16) notation because it provides a compact way to view raw byte values. Each byte corresponds to two hex characters (0-9, A-F). For example, the magic number for PDF files is "%PDF" which in hex is "25 50 44 46". Some formats use little endian or big endian byte ordering, meaning the same value might appear reversed depending on architecture. Detection libraries handle both variants.

Many signatures include "wildcard" bytes (placeholders) where the value may vary. For example, the JPEG format uses "FF D8" as a start marker but the exact following bytes vary by JPEG variant. Advanced detection uses bitmasks to ignore variable portions while matching the fixed signature pattern.

How the Detection Algorithm Works

Modern file type detection follows these steps:

  • Read Byte Buffer: The tool reads the first N bytes of the file (typically 4, 12, 16, or more depending on need).
  • Sequential Matching: The buffer is compared against a prioritized list of known magic numbers. Complex formats may require checking multiple offsets, not just the start.
  • Extension Validation: After detecting the MIME type and standard extension, the tool compares it to the original filename extension to flag mismatches, which may indicate renaming, corruption, or security risks.

The file-type library used in this tool implements the same algorithm used by thousands of web applications and is maintained by the open source community, supporting over 100 unique file formats including images, documents, archives, and audio/video containers. [GitHub, file-type]

Common Magic Number Signatures

Below is a reference list of widely used file signatures. The hex column shows the byte sequence typically found at the beginning of the file.

Images

  • JPEG/JFIF: FF D8 FF (hex) - Starts with JPEG SOI marker
  • PNG: 89 50 4E 47 0D 0A 1A 0A (hex) - "‰PNG" plus carriage return/line feed
  • GIF87a / GIF89a: 47 49 46 38 37 61 / 47 49 46 38 39 61 (hex) - "GIF87a" or "GIF89a"
  • BMP: 42 4D (hex) - "BM" (Windows bitmap)
  • WebP: 52 49 46 46 . . . 57 45 42 50 (hex) - "RIFF" followed by "WEBP"
  • TIFF: 49 49 2A 00 (little endian) or 4D 4D 00 2A (big endian)

Documents & Archives

  • PDF: 25 50 44 46 (hex) - "%PDF" (version follows)
  • ZIP (PKZIP): 50 4B 03 04 or 50 4B 05 06 (hex) - "PK" (Phil Katz ZIP)
  • Microsoft Office (DOCX/XLSX/PPTX): 50 4B 03 04 (ZIP based, then specific internal structures)
  • RAR (5.0+): 52 61 72 21 1A 07 01 00 (hex) - "Rar!" followed by version marker (RAR 1.5 to 4.0 uses 52 61 72 21 1A 07 00)
  • 7-Zip (7z): 37 7A BC AF 27 1C (hex) - "7z" plus compressed header
  • Gzip: 1F 8B (hex) - gzip compressed data

Executables & System

  • ELF (Unix Executable): 7F 45 4C 46 (hex) - DEL character followed by "ELF"
  • PE/EXE (Windows): 4D 5A (hex) - "MZ" (Mark Zbikowski)
  • Mach-O (macOS): FE ED FA CE (32-bit) / FE ED FA CF (64-bit) (hex) - single architecture Mach-O; CA FE BA BE is the fat/universal binary variant (shared with Java class files)
  • Java Class: CA FE BA BE (hex) - "CAFEBABE" Java bytecode

Audio & Video

  • MP3 (ID3): 49 44 33 (hex) - "ID3" tag, or FF FB for raw MPEG audio
  • MP4 / QuickTime: 00 00 00 . . . 66 74 79 70 (hex) - "ftyp" box at offset 4
  • AVI: 52 49 46 46 . . . 41 56 49 20 (hex) - "RIFF" then "AVI"
  • WAV: 52 49 46 46 . . . 57 41 56 45 (hex) - "RIFF" then "WAVE"
  • FLAC: 66 4C 61 43 (hex) - "fLaC" marker

Text & Data Formats

  • XML / HTML / SVG: Typically starts with "<?" or "<!" or plain text, but UTF BOM may appear: EF BB BF (UTF-8)
  • JSON: 7B 0A or 7B 20 (hex) - "{" followed by space or newline
  • SQLite Database: 53 51 4C 69 74 65 20 66 6F 72 6D 61 74 20 33 00 (hex) - "SQLite format 3"
  • PostScript: 25 21 50 53 (hex) - "%!PS"

Note: This is not an exhaustive list. The file-type library used in this tool recognizes over 100 distinct formats. Many newer formats (AVIF, HEIC, etc.) also have well-defined magic numbers but may require deeper inspection.