filesig

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2023 License: MIT Imports: 3 Imported by: 3

README

Go File Signature Validator

small library to validate Files by reading each magic number from a file

Go Reference filesig Go CI

Install

$ go get github.com/telkomdev/go-filesig

Usage

Simple

package main

import (
	"fmt"
	"github.com/telkomdev/go-filesig"
	"os"
)

func main() {
	args := os.Args

	if len(args) < 2 {
		fmt.Println("required input file")
		os.Exit(1)
	}

	inputFileArg := args[1]
	inFile, err := os.Open(inputFileArg)

	if err != nil {
		fmt.Println("error open input file ", err)
		os.Exit(1)
	}

	defer func() { inFile.Close() }()

	valid := filesig.Is3gp(inFile)
	fmt.Println(valid)

One Of

package main

import (
	"fmt"
	"github.com/telkomdev/go-filesig"
	"os"
)

func main() {
	args := os.Args

	if len(args) < 2 {
		fmt.Println("required input file")
		os.Exit(1)
	}

	inputFileArg := args[1]
	inFile, err := os.Open(inputFileArg)

	if err != nil {
		fmt.Println("error open input file ", err)
		os.Exit(1)
	}

	defer func() { inFile.Close() }()

	valid := filesig.IsOneOf(inFile, filesig.Is3gp, filesig.IsPng, filesig.IsJpeg)
	fmt.Println(valid)

}

HTTP Form File

package main

import (
	"fmt"
	"github.com/telkomdev/go-filesig"
	"net/http"
)

func uploadFile(w http.ResponseWriter, r *http.Request) {
	fmt.Println("File Upload Endpoint Hit")

	// Parse our multipart form, 10 << 20 specifies a maximum
	// upload of 10 MB files.
	r.ParseMultipartForm(10 << 20)
	// FormFile returns the first file for the given key `myFile`
	// it also returns the FileHeader so we can get the Filename,
	// the Header and the size of the file
	file, handler, err := r.FormFile("myFile")
	if err != nil {
		fmt.Println("Error Retrieving the File")
		fmt.Println(err)
		return
	}
	defer file.Close()
	fmt.Printf("Uploaded File: %+v\n", handler.Filename)
	fmt.Printf("File Size: %+v\n", handler.Size)
	fmt.Printf("MIME Header: %+v\n", handler.Header)

	// validate file
	validPdf := filesig.IsPdf(file)
	if !validPdf {
		fmt.Print("file is not valid PDF file \n")
	}

	// return that we have successfully uploaded our file!
	fmt.Fprintf(w, "Successfully Uploaded File\n")
}

func setupRoutes() {
	http.HandleFunc("/upload", uploadFile)
	http.ListenAndServe(":8000", nil)
}

func main() {
	fmt.Println("Hello World")
	setupRoutes()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AVIF       = []byte{0x00, 0x00, 0x00}
	BMP        = []byte{0x42, 0x4D}
	DIB_0      = []byte{0x42, 0x4D}
	DIB_1      = []byte{0x28, 0x00}
	GIF        = []byte{0x47, 0x49, 0x46, 0x38}
	TIFF       = []byte{0x49, 0x49, 0x2A, 0x00}
	MP3        = []byte{0x49, 0x44, 0x33}
	MPG_0      = []byte{0x00, 0x00, 0x01, 0xB3}
	MPG_1      = []byte{0x00, 0x00, 0x01, 0xBA}
	FLV        = []byte{0x46, 0x4C, 0x56}
	APK        = []byte{0x50, 0x4B, 0x03, 0x04}
	MS_OFFICE  = []byte{0x50, 0x4B, 0x03, 0x04}
	JAR        = []byte{0x50, 0x4B, 0x03, 0x04}
	SWF_0      = []byte{0x43, 0x57, 0x53}
	SWF_1      = []byte{0x46, 0x57, 0x53}
	PNG        = []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
	JPEG       = []byte{0xFF, 0xD8}
	MP4_0      = []byte{0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6F, 0x6D}
	MP4_1      = []byte{0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70}
	MP4_2      = []byte{0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70}
	THREE_GP_0 = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
	THREE_GP_1 = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
	THREE_GP_2 = []byte{0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70}
	MKV        = []byte{0x1A, 0x45, 0xDF, 0xA3}
	PDF        = []byte{0x25, 0x50, 0x44, 0x46}
	RAR        = []byte{0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00}
	GZIP       = []byte{0x1F, 0x8B, 0x08}
	ZIP_0      = []byte{0x50, 0x4B, 0x03, 0x04}
	ZIP_1      = []byte{0x50, 0x4B, 0x05, 0x06}
	ZIP_2      = []byte{0x50, 0x4B, 0x07, 0x08}
	WEBP       = []byte{0x52, 0x49, 0x46, 0x46}

	HtmlCommentRegex = regexp.MustCompile(`(?i)<!--([\s\S]*?)-->`)
	SvgRegex         = regexp.MustCompile(`(?i)^\s*(?:<\?xml[^>]*>\s*)?(?:<!doctype svg[^>]*>\s*)?<svg[^>]*>[^*]*<\/svg>\s*$`)
)

Functions

func Is3gp

func Is3gp(r io.ReadSeeker) bool

Is3gp function will return true if File is a valid 3gp

func IsApk

func IsApk(r io.ReadSeeker) bool

IsApk function will return true if File is a valid APK

func IsAvif

func IsAvif(r io.ReadSeeker) bool

IsAvif function will return true if File is a valid AVIF

func IsBmp

func IsBmp(r io.ReadSeeker) bool

IsBmp function will return true if File is a valid BMP

func IsDib

func IsDib(r io.ReadSeeker) bool

IsDib function will return true if File is a valid DIB

func IsFlv

func IsFlv(r io.ReadSeeker) bool

IsFlv function will return true if File is a valid FLV

func IsGif

func IsGif(r io.ReadSeeker) bool

IsGif function will return true if File is a valid GIF

func IsGzip

func IsGzip(r io.ReadSeeker) bool

IsGzip function will return true if File is a valid GZIP

func IsJar

func IsJar(r io.ReadSeeker) bool

IsJar function will return true if File is a valid JAR (Java Archive)

func IsJpeg

func IsJpeg(r io.ReadSeeker) bool

IsJpeg function will return true if File is a valid JPEG

func IsMkv

func IsMkv(r io.ReadSeeker) bool

IsMkv function will return true if File is a valid MKV

func IsMp3

func IsMp3(r io.ReadSeeker) bool

IsMp3 function will return true if File is a valid MP3

func IsMp4

func IsMp4(r io.ReadSeeker) bool

func IsMpg

func IsMpg(r io.ReadSeeker) bool

IsMpg function will return true if File is a valid MPG

func IsMsOffice

func IsMsOffice(r io.ReadSeeker) bool

IsMsOffice function will return true if File is a valid MS OFFICE Document (DOCX|PPTX|XLSX)

func IsOneOf

func IsOneOf(r io.ReadSeeker, functions ...function) bool

IsOneOf function will validate File with multiple function

func IsPdf

func IsPdf(r io.ReadSeeker) bool

IsPdf function will return true if File is a valid PDF

func IsPng

func IsPng(r io.ReadSeeker) bool

IsPng function will return true if File is a valid PNG

func IsRar

func IsRar(r io.ReadSeeker) bool

IsRar function will return true if File is a valid RAR

func IsSvg added in v1.0.2

func IsSvg(r io.ReadSeeker) bool

IsSvg function will return true if File is a valid SVG

func IsSwf

func IsSwf(r io.ReadSeeker) bool

IsSwf function will return true if File is a valid SWF

func IsTiff

func IsTiff(r io.ReadSeeker) bool

IsTiff function will return true if File is a valid TIFF

func IsWebp

func IsWebp(r io.ReadSeeker) bool

IsWebp function will return true if File is a valid Webp

func IsZip

func IsZip(r io.ReadSeeker) bool

IsZip function will return true if File is a valid ZIP

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL