Skip to content

Commit

Permalink
Merge pull request #14 from glaslos/api_changes
Browse files Browse the repository at this point in the history
API changes and hash distance calculation.
  • Loading branch information
glaslos committed Jul 18, 2017
2 parents b807dd6 + da36316 commit 61c0b37
Show file tree
Hide file tree
Showing 6 changed files with 4,919 additions and 45 deletions.
45 changes: 35 additions & 10 deletions app/tlsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,54 @@ var (
BUILDDATE = ""
)

func main() {
var file = flag.String("f", "", "path to the `file` to be hashed")
var raw = flag.Bool("r", false, "set to get only the hash")
var version = flag.Bool("version", false, "print version")
var file string
var compare string
var raw bool
var version bool

func init() {
flag.StringVar(&file, "f", "", "path to the `file` to be hashed")
flag.StringVar(&compare, "c", "", "specifies a `filename` od `digest` whose TLSH value will be compared to a filename specified (-f)")
flag.BoolVar(&raw, "r", false, "set to get only the hash")
flag.BoolVar(&version, "version", false, "print version")
flag.Parse()
if *version {
}

// Main contains the main code
func Main() {
if version {
fmt.Printf("%s %s\n", VERSION, BUILDDATE)
return
}
if *file == "" {
if file == "" {
fmt.Fprintf(os.Stderr, "Usage of %s [-f <file>]\n\n", os.Args[0])
flag.PrintDefaults()
fmt.Println()
return
}
hash, err := tlsh.Hash(*file)
hash, err := tlsh.HashFilename(file)
if err != nil {
fmt.Println(err)
return
}
if *raw {
fmt.Println(hash)
if compare != "" {
hashCompare, err := tlsh.HashFilename(compare)
if err != nil {
fmt.Println(err)
return
}
distance := hash.Diff(hashCompare)

fmt.Printf("%d %s %s - %s %s\n", distance, hash, file, hashCompare, compare)
} else {
fmt.Printf("%s %s\n", hash, *file)
if raw {
fmt.Println(hash)
} else {
fmt.Printf("%s %s\n", hash, file)
}
}
}

func main() {
Main()
}
43 changes: 43 additions & 0 deletions app/tlsh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import "testing"

func TestMainVersion(t *testing.T) {
version = true
Main()
}

func TestMainRaw(t *testing.T) {
version = false
file = "../tests/test_file_1"
raw = true
Main()
}

func TestMainHash(t *testing.T) {
version = false
file = "../tests/test_file_1"
raw = false
Main()
}

func TestMainHashError(t *testing.T) {
version = false
file = "../tests/test_file_666"
raw = false
Main()
}

func TestMainCompare(t *testing.T) {
version = false
file = "../tests/test_file_1"
compare = "../tests/test_file_2"
raw = false
Main()
}

func TestMainHelp(t *testing.T) {
version = false
file = ""
Main()
}

0 comments on commit 61c0b37

Please sign in to comment.