Skip to content

Instantly share code, notes, and snippets.

@pgaertig
Created September 27, 2018 08:31
Show Gist options
  • Save pgaertig/93a81e842b6e71bb3ddf8533d43b5399 to your computer and use it in GitHub Desktop.
Save pgaertig/93a81e842b6e71bb3ddf8533d43b5399 to your computer and use it in GitHub Desktop.
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
public class Main {
public static void main(String[] args) {
System.out.printf("JKuferek 1.0");
scanDir(new File("/home/mrp/Pictures"));
}
static void scanDir(File dir) {
System.out.println(String.format("Scanning %s", dir));
if (dir.isDirectory()) {
for(File f: dir.listFiles()) {
if(f.isDirectory()) {
scanDir(f);
} else if (f.canRead()) {
System.out.printf("Visited: %s Size: %d bytes, Sha256: %s\n",
f.getPath(), f.length(), fileSha256(f));
};
}
}
}
static String fileSha256(File input) {
try (InputStream in = new FileInputStream(input)) {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] block = new byte[4096];
int length;
while ((length = in.read(block)) > 0) {
digest.update(block, 0, length);
}
return (new HexBinaryAdapter()).marshal(digest.digest());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package main
import (
"os"
"path/filepath"
"fmt"
"crypto/sha256"
"encoding/hex"
"encoding/binary"
"io"
)
func main() {
println("Kuferek 1.0")
if len(os.Args) > 1 {
scanDir(os.Args[1])
} else {
scanDir("/home/mrp/Pictures")
}
}
func scanDir(dir string) {
fmt.Printf("Scanning: %s", dir)
visit := func(path string, f os.FileInfo, err error) error {
if f.IsDir() && path != dir {
scanDir(path)
return filepath.SkipDir
}
if f.Mode().IsRegular() {
fmt.Printf("Visited: %s Size: %d bytes, Sha256: %s\n",
path, f.Size(), fileSha256(path))
}
return nil
}
filepath.Walk(dir, visit)
}
func fileSha256(filePath string) (result string) {
file, err := os.Open(filePath)
if err != nil {
return
}
defer file.Close()
fileStat, err := file.Stat()
if err != nil {
return
}
hash := sha256.New()
if _, err = io.Copy(hash, file); err != nil {
return
}
result = hex.EncodeToString(hash.Sum(nil))
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment