https://github.com/liamg/magic
:tophat::rabbit2: Toolkit for detecting and verifying file type using magic bytes in pure Golang
https://github.com/liamg/magic
file-signature magic-bytes magic-numbers
Last synced: 16 days ago
JSON representation
:tophat::rabbit2: Toolkit for detecting and verifying file type using magic bytes in pure Golang
- Host: GitHub
- URL: https://github.com/liamg/magic
- Owner: liamg
- License: unlicense
- Created: 2019-01-23T07:19:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-28T07:55:09.000Z (almost 2 years ago)
- Last Synced: 2025-05-05T04:04:09.179Z (16 days ago)
- Topics: file-signature, magic-bytes, magic-numbers
- Language: Go
- Homepage:
- Size: 125 KB
- Stars: 26
- Watchers: 3
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# magic
Toolkit for detecting and verifying file type using magic bytes in pure Go
Support for all file signatures listed [here](https://en.wikipedia.org/wiki/List_of_file_signatures).
You only need to provide the first few hundred bytes of a given file to detect the file type, unless you want to detect `.iso` images, which require examination of the first 32774 bytes.
A description and a suggested file extension are provided where relevant, and MIME types will be added in future.
## Example Usage
```go
package mainimport "github.com/liamg/magic"
func main() {
data := []byte{0xa1, 0xb2, 0xc3, 0xd4, 0x00, 0x00, 0x00, 0x00}
fileType, err := magic.Lookup(data)
if err != nil {
if err == magic.ErrUnknown {
fmt.Println("File type is unknown")
os.Exit(1)
}else{
panic(err)
}
}fmt.Printf("File extension: %s\n", fileType.Extension)
fmt.Printf("File type description: %s\n", fileType.Description)
}
```