Portable Network Graphics
image/png
Magic Bytes
Offset: 0
89 50 4E 47 0D 0A 1A 0A
Portable Network Graphics (PNG) is a raster graphics file format developed by the PNG Development Group and maintained as an international standard by the W3C and ISO/IEC. It is widely utilized in web design and digital publishing for logos, icons, and illustrations requiring high fidelity or transparency. While considered a secure format, historical vulnerabilities in specific library implementations, such as buffer overflows in older versions of libpng, underscore the importance of using updated viewing software.
Validation Code
How to validate .png files in Python
Python
def is_png(file_path: str) -> bool:
"""Check if file is a valid PNG by magic bytes."""
signature = bytes([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])
with open(file_path, "rb") as f:
return f.read(8) == signature
How to validate .png files in Node.js
Node.js
function isPNG(buffer: Buffer): boolean {
const signature = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
return buffer.subarray(0, 8).equals(signature);
}
Go
func IsPNG(data []byte) bool {
signature := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
if len(data) < 8 {
return false
}
return bytes.Equal(data[:8], signature)
}
API Endpoint
GET
/api/v1/png
curl https://filesignature.org/api/v1/png