-
-
Save mkmik/1019818 to your computer and use it in GitHub Desktop.
go md5/sha1 example
This file contains 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
$ for a in md5 sha1 ; do ./hash -$a /bin/ls; ${a}sum /bin/ls; echo; done | |
a3f812b9fe329322a3d6b00722694e84 /bin/ls | |
a3f812b9fe329322a3d6b00722694e84 /bin/ls | |
9eb997f16adb828cd0273f24b0ae94d32c5a3b3f /bin/ls | |
9eb997f16adb828cd0273f24b0ae94d32c5a3b3f /bin/ls | |
This file contains 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
/* | |
Hash - Guillermo Estrada | |
Simple utility to obtain the MD5 and/or SHA-1 | |
of a file from the command line. | |
2011 | |
*/ | |
package main | |
import ( | |
"io" | |
"os" | |
"fmt" | |
"flag" | |
"crypto/md5" | |
"crypto/sha1" | |
) | |
func main() { | |
md5f := flag.Bool("md5", false, "-md5 calculate md5 hash of file") | |
sha1f := flag.Bool("sha1", false, "-sha1 calculate sha1 hash of file") | |
flag.Parse() | |
if !*md5f && !*sha1f { | |
fmt.Println("err: No hash specified. Use -md5 or -sha1 or both.") | |
os.Exit(1) | |
} | |
infile, inerr := os.Open(flag.Arg(0)) | |
if inerr == nil { | |
if *md5f { | |
md5h := md5.New() | |
io.Copy(md5h,infile) | |
fmt.Printf("%x %s\n",md5h.Sum(), flag.Arg(0)) | |
} | |
if *sha1f { | |
sha1h := sha1.New() | |
io.Copy(sha1h,infile) | |
fmt.Printf("%x %s\n",sha1h.Sum(), flag.Arg(0)) | |
} | |
} else { | |
fmt.Println(inerr) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment