amiga-sha/sha.c

123 lines
3.4 KiB
C

/*-
* Copyright (c) 2014-2015 Carsten Larsen
* Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com>
* 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 ALLAN SADDI AND HIS CONTRIBUTORS ``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 ALLAN SADDI OR HIS CONTRIBUTORS 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.
*
*/
#include "platform.h"
#include "sha1.h"
#include "sha256.h"
#include "sha384.h"
#include "sha512.h"
int shaFile (char *name, FILE *f, int which)
{
union {
SHA1Context sha1;
SHA256Context sha256;
SHA384Context sha384;
SHA512Context sha512;
} s;
size_t len;
uint8_t hash[SHA512_HASH_SIZE];
int hashLen, i;
int success = 1;
switch (which) {
case 1:
SHA1Init (&s.sha1);
break;
case 2:
SHA256Init (&s.sha256);
break;
case 3:
SHA384Init (&s.sha384);
break;
case 5:
SHA512Init (&s.sha512);
break;
default:
abort ();
}
while ((len = fread (buffer, 1, SHA_BUFFER_SIZE, f)) > 0) {
switch (which) {
case 1:
SHA1Update (&s.sha1, buffer, len);
break;
case 2:
SHA256Update (&s.sha256, buffer, len);
break;
case 3:
SHA384Update (&s.sha384, buffer, len);
break;
case 5:
SHA512Update (&s.sha512, buffer, len);
break;
default:
abort ();
}
}
if (ferror (f)) {
fprintf (stderr, "%s: %s\n", name, "Read error");
success = 0;
}
else {
switch (which) {
case 1:
SHA1Final (&s.sha1, hash);
hashLen = SHA1_HASH_SIZE;
break;
case 2:
SHA256Final (&s.sha256, hash);
hashLen = SHA256_HASH_SIZE;
break;
case 3:
SHA384Final (&s.sha384, hash);
hashLen = SHA384_HASH_SIZE;
break;
case 5:
SHA512Final (&s.sha512, hash);
hashLen = SHA512_HASH_SIZE;
break;
default:
abort ();
}
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;
}