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