amiga-sha/sha512_amiga.c

105 lines
2.8 KiB
C

/*
* Copyright (c) 2015 Carsten Larsen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#define ARGSFORMAT "FILES/M/A"
#define VERSION_PROG "sha512"
#include "platform.h"
#include "sha512.h"
const char *vers = VERSION_STRING_AMIGA;
struct shaargs {
char **files;
};
int main(int argc, char **argv)
{
int i;
FILE *f;
int failure = 0;
struct shaargs args = { NULL };
atexit(clean_exit);
alloc_buffer();
rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL);
if (!rdargs)
{
args_error(argv[0]);
exit(5);
}
for (i = 0; args.files[i]; i++) {
if ((f = fopen(args.files[i], "rb"))) {
if (!shaFile(args.files[i], f, 0))
failure = 1;
fclose(f);
}
else {
fprintf(stderr, "%s: %s\n",args.files[i], "No such file or directory");
failure = 1;
}
}
exit(failure);
}
int shaFile (char *name, FILE *f, int which)
{
SHA512Context s;
size_t len;
uint8_t hash[SHA512_HASH_SIZE];
int hashLen, i;
int success = 1;
SHA512Init (&s);
while ((len = fread (buffer, 1, SHA_BUFFER_SIZE, f)) > 0) {
SHA512Update (&s, buffer, len);
}
if (ferror (f)) {
fprintf (stderr, "%s: %s\n", name, "Read error");
success = 0;
}
else {
SHA512Final (&s, hash);
hashLen = SHA512_HASH_SIZE;
for (i = 0; i < hashLen; i++)
printf ("%02x", hash[i]);
if (name)
printf (" %s\n", name);
else
printf ("\n");
}
memset(&s, 0, sizeof(s));
return success;
}