Update main.go
This commit is contained in:
257
main.go
257
main.go
@@ -1,4 +1,5 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/hex"
|
||||
@@ -13,152 +14,154 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var bits = flag.Int("b", 224, "Bits: 224, 256, 384 and 512.")
|
||||
var check = flag.String("c", "", "Check hashsum file.")
|
||||
var recursive = flag.Bool("r", false, "Process directories recursively.")
|
||||
var target = flag.String("t", "", "Target file/wildcard to generate hashsum list.")
|
||||
var verbose = flag.Bool("v", false, "Verbose mode. (The exit code is always 0 in this mode)")
|
||||
var (
|
||||
bits = flag.Int("b", 224, "Bits: 224, 256, 384 and 512.")
|
||||
check = flag.String("c", "", "Check hashsum file.")
|
||||
recursive = flag.Bool("r", false, "Process directories recursively.")
|
||||
target = flag.String("t", "", "Target file/wildcard to generate hashsum list.")
|
||||
verbose = flag.Bool("v", false, "Verbose mode. (The exit code is always 0 in this mode)")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
flag.Parse()
|
||||
|
||||
if (len(os.Args) < 2) || (*bits != 224 && *bits != 256 && *bits != 384 && *bits != 512) {
|
||||
fmt.Println("SHA3 Hashsum Tool - ALBANESE Lab (c) 2020-2021\n")
|
||||
fmt.Println("Usage of",os.Args[0]+":")
|
||||
fmt.Printf("%s [-v] [-b N] [-c <hash.ext>] [-r] -t <file.ext>\n\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
os.Exit(1)
|
||||
}
|
||||
if (len(os.Args) < 2) || (*bits != 224 && *bits != 256 && *bits != 384 && *bits != 512) {
|
||||
fmt.Println("SHA3 Hashsum Tool - ALBANESE Lab (c) 2020-2021\n")
|
||||
fmt.Println("Usage of", os.Args[0]+":")
|
||||
fmt.Printf("%s [-v] [-b N] [-c <hash.ext>] [-r] -t <file.ext>\n\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if *target != "" && *recursive == false {
|
||||
files, err := filepath.Glob(*target)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for _, match := range files {
|
||||
var h hash.Hash
|
||||
if *bits == 224 {
|
||||
h = sha3.New224()
|
||||
} else if *bits == 256 {
|
||||
h = sha3.New256()
|
||||
} else if *bits == 384 {
|
||||
h = sha3.New384()
|
||||
} else if *bits == 512 {
|
||||
h = sha3.New512()
|
||||
}
|
||||
f, err := os.Open(match)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
file, err := os.Stat(match)
|
||||
if file.IsDir() {
|
||||
} else {
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(hex.EncodeToString(h.Sum(nil)), "*" + f.Name())
|
||||
}
|
||||
}
|
||||
if *target != "" && *recursive == false {
|
||||
files, err := filepath.Glob(*target)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for _, match := range files {
|
||||
var h hash.Hash
|
||||
if *bits == 224 {
|
||||
h = sha3.New224()
|
||||
} else if *bits == 256 {
|
||||
h = sha3.New256()
|
||||
} else if *bits == 384 {
|
||||
h = sha3.New384()
|
||||
} else if *bits == 512 {
|
||||
h = sha3.New512()
|
||||
}
|
||||
f, err := os.Open(match)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
file, err := os.Stat(match)
|
||||
if file.IsDir() {
|
||||
} else {
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(hex.EncodeToString(h.Sum(nil)), "*"+f.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if *target != "" && *recursive == true && *bits == 224 {
|
||||
err := filepath.Walk(filepath.Dir(*target),
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.Stat(path)
|
||||
if file.IsDir() {
|
||||
} else {
|
||||
filename := filepath.Base(path)
|
||||
pattern := filepath.Base(*target)
|
||||
matched, err := filepath.Match(pattern, filename)
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
if matched {
|
||||
var h hash.Hash
|
||||
if *bits == 224 {
|
||||
h = sha3.New224()
|
||||
} else if *bits == 256 {
|
||||
h = sha3.New256()
|
||||
} else if *bits == 384 {
|
||||
h = sha3.New384()
|
||||
} else if *bits == 512 {
|
||||
h = sha3.New512()
|
||||
file, err := os.Stat(path)
|
||||
if file.IsDir() {
|
||||
} else {
|
||||
filename := filepath.Base(path)
|
||||
pattern := filepath.Base(*target)
|
||||
matched, err := filepath.Match(pattern, filename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
if matched {
|
||||
var h hash.Hash
|
||||
if *bits == 224 {
|
||||
h = sha3.New224()
|
||||
} else if *bits == 256 {
|
||||
h = sha3.New256()
|
||||
} else if *bits == 384 {
|
||||
h = sha3.New384()
|
||||
} else if *bits == 512 {
|
||||
h = sha3.New512()
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(hex.EncodeToString(h.Sum(nil)), "*"+f.Name())
|
||||
}
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(hex.EncodeToString(h.Sum(nil)), "*" + f.Name())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
if *check != "" {
|
||||
file, err := os.Open(*check)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening file: %s", err)
|
||||
}
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
var txtlines []string
|
||||
|
||||
for scanner.Scan() {
|
||||
txtlines = append(txtlines, scanner.Text())
|
||||
}
|
||||
file.Close()
|
||||
for _, eachline := range txtlines {
|
||||
lines := strings.Split(string(eachline), " *")
|
||||
if strings.Contains(string(eachline), " *") {
|
||||
var h hash.Hash
|
||||
if *bits == 224 {
|
||||
h = sha3.New224()
|
||||
} else if *bits == 256 {
|
||||
h = sha3.New256()
|
||||
} else if *bits == 384 {
|
||||
h = sha3.New384()
|
||||
} else if *bits == 512 {
|
||||
h = sha3.New512()
|
||||
}
|
||||
_, err := os.Stat(lines[1])
|
||||
if err == nil {
|
||||
f, err := os.Open(lines[1])
|
||||
if *check != "" {
|
||||
file, err := os.Open(*check)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatalf("failed opening file: %s", err)
|
||||
}
|
||||
io.Copy(h, f)
|
||||
|
||||
if *verbose {
|
||||
if hex.EncodeToString(h.Sum(nil)) == lines[0] {
|
||||
fmt.Println(lines[1] + "\t", "OK")
|
||||
} else {
|
||||
fmt.Println(lines[1] + "\t", "FAILED")
|
||||
}
|
||||
} else {
|
||||
if hex.EncodeToString(h.Sum(nil)) == lines[0] {
|
||||
} else {
|
||||
os.Exit(1)
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
var txtlines []string
|
||||
|
||||
for scanner.Scan() {
|
||||
txtlines = append(txtlines, scanner.Text())
|
||||
}
|
||||
file.Close()
|
||||
for _, eachline := range txtlines {
|
||||
lines := strings.Split(string(eachline), " *")
|
||||
if strings.Contains(string(eachline), " *") {
|
||||
var h hash.Hash
|
||||
if *bits == 224 {
|
||||
h = sha3.New224()
|
||||
} else if *bits == 256 {
|
||||
h = sha3.New256()
|
||||
} else if *bits == 384 {
|
||||
h = sha3.New384()
|
||||
} else if *bits == 512 {
|
||||
h = sha3.New512()
|
||||
}
|
||||
_, err := os.Stat(lines[1])
|
||||
if err == nil {
|
||||
f, err := os.Open(lines[1])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
io.Copy(h, f)
|
||||
|
||||
if *verbose {
|
||||
if hex.EncodeToString(h.Sum(nil)) == lines[0] {
|
||||
fmt.Println(lines[1]+"\t", "OK")
|
||||
} else {
|
||||
fmt.Println(lines[1]+"\t", "FAILED")
|
||||
}
|
||||
} else {
|
||||
if hex.EncodeToString(h.Sum(nil)) == lines[0] {
|
||||
} else {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if *verbose {
|
||||
fmt.Println(lines[1]+"\t", "Not found!")
|
||||
} else {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if *verbose {
|
||||
fmt.Println(lines[1] + "\t", "Not found!")
|
||||
} else {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user