Skip to content

Instantly share code, notes, and snippets.

@nakamkaz
Last active June 17, 2020 02:29
Show Gist options
  • Save nakamkaz/63a1942144123321cc9656976c49c833 to your computer and use it in GitHub Desktop.
Save nakamkaz/63a1942144123321cc9656976c49c833 to your computer and use it in GitHub Desktop.
  • How to Build
go build checksumagogo.go
  • How to Run
checksumagogo  zerofile.txt
zerofile.txt -- [md5] d41d8cd98f00b204e9800998ecf8427e
checksumagogo -t md5  zerofile.txt
zerofile.txt -- [md5] d41d8cd98f00b204e9800998ecf8427e
checksumagogo -t sha1  zerofile.txt
zerofile.txt -- [sha1] da39a3ee5e6b4b0d3255bfef95601890afd80709
checksumagogo -t sha256  zerofile.txt
zerofile.txt -- [sha256] e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
checksumagogo -t sha512  zerofile.txt
zerofile.txt -- [sha512] cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"flag"
"fmt"
"hash"
"io"
"log"
"os"
)
var (
p = fmt.Printf
// isForAll = flag.Bool("A", false, "calculates md5,sha1,sha256,sha512 for one")
topt = flag.String("t", "md5", "default is md5. You can use md5,sha1,sha256,sha512.")
// when '-t sha1', *topt is set to "sha1"
)
func getCheckSum(hashname string, file *os.File) (string, string, string) {
var h hash.Hash
switch hashname {
case "sha1":
h = sha1.New()
case "sha256":
h = sha256.New()
case "sha512":
h = sha512.New()
default:
h = md5.New()
hashname = "md5"
}
if _, err := io.Copy(h, file); err != nil {
log.Fatal(err)
}
cksum := fmt.Sprintf("%x", h.Sum(nil))
return cksum, hashname, file.Name()
}
func printForOne(t string, file *os.File) {
h, ty, fn := getCheckSum(t, file)
p("%s -- [%s] %s\n", fn, ty, h)
}
func printForAll(file *os.File) {
p("%s -- \n", file.Name())
for _, t := range []string{"md5", "sha1", "sha256", "sha512"} {
printForOne(t, file)
}
}
func main() {
flag.Parse()
for _, af := range flag.Args() {
file, err := os.Open(af)
if err != nil {
log.Fatal(err)
}
defer file.Close()
/*
if *isForAll == true {
printForAll(file)
} else {
*/
printForOne(*topt, file)
/*
}
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment