Created
May 6, 2016 15:55
-
-
Save dkolbly/1f4315411d0c322c35db94648c77017d to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"archive/tar" | |
"crypto/sha1" | |
"encoding/hex" | |
"fmt" | |
"io" | |
"os" | |
) | |
func main() { | |
var src io.Reader | |
if os.Args[1] == "-" { | |
src = os.Stdin | |
} else { | |
s, err := os.Open(os.Args[1]) | |
if err != nil { | |
panic(err) | |
} | |
src = s | |
} | |
r := tar.NewReader(src) | |
for { | |
hdr, err := r.Next() | |
if err == io.EOF { | |
fmt.Printf("done\n") | |
break | |
} | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("%12d '%c' %s\n", hdr.Size, hdr.Typeflag, hdr.Name) | |
hash := sha1.New() | |
n, err := io.Copy(hash, r) | |
if err != nil { | |
panic(err) | |
} | |
digest := hex.EncodeToString(hash.Sum(nil)) | |
fmt.Printf(" ... copied %d bytes: %s\n", n, digest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment