Last active
September 17, 2020 00:18
-
-
Save lotusirous/a392c47c477cdf45345ec3dbd97b8d28 to your computer and use it in GitHub Desktop.
An example of finding apk file by its hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/md5" | |
"flag" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func getHash(location string) (string, error) { | |
f, err := os.Open(location) | |
if err != nil { | |
return "", fmt.Errorf("open %s failed: %w", location, err) | |
} | |
defer f.Close() | |
h := md5.New() | |
if _, err := io.Copy(h, f); err != nil { | |
return "", fmt.Errorf("copy hashing failed: %w", err) | |
} | |
return fmt.Sprintf("%x", h.Sum(nil)), nil | |
} | |
func main() { | |
var hash string | |
var dir string | |
flag.StringVar(&hash, "hash", "cd3bbabf0b1987526e97ed63c27af5c1", "enter your hash") | |
flag.StringVar(&dir, "dir", "/", "enter lookup path") | |
flag.Parse() | |
fmt.Println("Search file by hash", hash) | |
err := filepath.Walk("/", | |
func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err | |
} | |
if info.IsDir() { // skip directory | |
return nil | |
} | |
if !strings.HasSuffix(path, ".apk") { | |
return nil // skip non-apk file | |
} | |
h, err := getHash(path) | |
if err != nil { | |
return err | |
} | |
// break the loop | |
if h == hash { | |
return fmt.Errorf("%s has hash: %s", path, h) | |
} | |
return nil | |
}) | |
if err != nil { | |
log.Println(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment