#!/usr/local/bin/python3.0

#   skeinsum.py
#   Copyright 2008, 2009 Hagen Fürstenau <hfuerstenau@gmx.net>
#
#   Demonstrates Skein hashing with PySkein.
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import skein
from io import DEFAULT_BUFFER_SIZE

HASH = skein.skein512
DIGEST_BITS = 256


def printsum(f, name):
    h = HASH(digest_bits=DIGEST_BITS)
    buf = True
    while buf:
        try:
            buf = f.read(DEFAULT_BUFFER_SIZE)
        except KeyboardInterrupt:
            print()
            sys.exit(130)
        h.update(buf)
    try:
        print("{0}  {1}".format(h.hexdigest(), name))
    except IOError as e:
        if e.errno != 32:
            raise


if len(sys.argv) < 2:
    printsum(sys.stdin.buffer, "-")
else:
    for filename in sys.argv[1:]:
        if os.path.isdir(filename):
            print("skeinsum: {0}: is a directory".format(filename),
                  file=sys.stderr)
            continue
        with open(filename, "rb") as f:
            printsum(f, filename)

