2015-01-01 07:32:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/magical/keccak"
|
|
|
|
)
|
|
|
|
|
2024-10-06 04:18:39 +00:00
|
|
|
const usage = "usage: sha3sum [-h] files..."
|
|
|
|
|
|
|
|
var (
|
|
|
|
stdout = os.Stdout
|
|
|
|
stderr = os.Stderr
|
|
|
|
)
|
|
|
|
|
2015-01-01 07:32:14 +00:00
|
|
|
func main() {
|
2024-10-06 04:18:39 +00:00
|
|
|
// Parse flags
|
|
|
|
if len(os.Args) <= 1 {
|
|
|
|
stderr.WriteString(usage + "\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
args := os.Args[1:]
|
|
|
|
for _, a := range args {
|
|
|
|
switch a {
|
|
|
|
case "-h", "--help":
|
|
|
|
stdout.WriteString(usage + "\n")
|
|
|
|
return
|
|
|
|
case "", "-":
|
|
|
|
default:
|
|
|
|
if a[0] == '-' {
|
|
|
|
stderr.WriteString("sha3sum: unrecognized flag " + a + "\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// compute file hashes
|
2015-01-01 07:32:14 +00:00
|
|
|
var sum [keccak.Size]byte
|
|
|
|
h := keccak.New256()
|
2024-10-06 04:18:39 +00:00
|
|
|
files := args
|
2015-01-01 07:32:14 +00:00
|
|
|
for _, filename := range files {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2024-10-06 04:18:39 +00:00
|
|
|
stderr.WriteString("sha3sum: " + err.Error() + "\n")
|
2015-01-01 07:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
h.Reset()
|
|
|
|
_, err = io.Copy(h, f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
2024-10-06 04:18:39 +00:00
|
|
|
stderr.WriteString("sha3sum: " + err.Error() + "\n")
|
2015-01-01 07:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
h.Sum(sum[:0])
|
2024-10-06 04:18:39 +00:00
|
|
|
stdout.WriteString(toHex(sum[:]) + " " + filename + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toHex(data []byte) string {
|
|
|
|
const digits = "0123456789abcdef"
|
|
|
|
out := make([]byte, len(data)*2)
|
|
|
|
for i, v := range data {
|
|
|
|
out[i*2] = digits[v>>4]
|
|
|
|
out[i*2+1] = digits[v&15]
|
2015-01-01 07:32:14 +00:00
|
|
|
}
|
2024-10-06 04:18:39 +00:00
|
|
|
return string(out)
|
2015-01-01 07:32:14 +00:00
|
|
|
}
|