Source

sha3 / sha3sum.go

Full commit
// Copyright 2012 Eric Roshan-Eisner. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import (
	"../sha3"
	"flag"
	"fmt"
	"hash"
	"io"
	"os"
)

var size = flag.Int("size", 256, "bit size of output [224, 256, 384, 512]")

func main() {
	flag.Parse()
	var h hash.Hash
	switch *size {
	case 224:
		h = sha3.New224()
	case 256:
		h = sha3.New256()
	case 384:
		h = sha3.New384()
	case 512:
		h = sha3.New512()
	default:
		fmt.Fprintf(os.Stderr,
			"Invalid hash size %d. Must be one of [224, 256, 384, 512]\n", *size)
		os.Exit(1)
	}
	sum := make([]byte, h.Size())

	if len(flag.Args()) == 0 {
		_, err := io.Copy(h, os.Stdin)
		if err != nil {
			fmt.Fprintf(os.Stderr, "sha3sum: stdin: %s\n", err)
			return
		}
		fmt.Printf("%x  %s\n", h.Sum(sum[:0]), "[stdin]")
		return
	}

	for _, filename := range flag.Args() {
		h.Reset()
		file, err := os.Open(filename)
		if err != nil {
			fmt.Fprintf(os.Stderr, "sha3sum: %s\n", err)
			continue
		}

		_, err = io.Copy(h, file)
		if err != nil {
			fmt.Fprintf(os.Stderr, "sha3sum: %s\n", err)
			file.Close()
			continue
		}

		fmt.Printf("%x  %s\n", h.Sum(sum[:0]), filename)
		file.Close()
	}
}