2008-12-19 03:18:49 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2008 Juan Romero Pardines.
|
|
|
|
* All rights reserved.
|
2008-10-20 14:34:27 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2008-12-19 03:18:49 +00:00
|
|
|
* 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.
|
2008-10-20 14:34:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-12-21 22:57:02 +00:00
|
|
|
#include <xbps_api.h>
|
2008-10-20 14:34:27 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2008-10-29 17:42:23 +00:00
|
|
|
fprintf(stderr, "usage: xbps-digest <file> <file1+N> ...\n");
|
2008-12-29 14:56:40 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2008-10-20 14:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
SHA256_CTX ctx;
|
|
|
|
uint8_t buffer[BUFSIZ * 20], *digest;
|
|
|
|
ssize_t bytes;
|
2008-10-29 17:42:23 +00:00
|
|
|
int i, fd;
|
|
|
|
|
|
|
|
if (argc < 2)
|
2008-10-20 14:34:27 +00:00
|
|
|
usage();
|
|
|
|
|
2008-10-29 17:42:23 +00:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if ((fd = open(argv[i], O_RDONLY)) == -1) {
|
|
|
|
printf("xbps-digest: cannot open %s (%s)\n", argv[i],
|
2009-02-05 13:46:09 +00:00
|
|
|
strerror(errno));
|
2008-12-29 14:56:40 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2008-10-29 17:42:23 +00:00
|
|
|
}
|
2008-10-20 14:34:27 +00:00
|
|
|
|
2009-07-29 21:13:04 +00:00
|
|
|
digest = malloc(SHA256_DIGEST_STRING_LENGTH);
|
2008-10-29 17:42:23 +00:00
|
|
|
if (digest == NULL) {
|
2008-12-19 03:18:49 +00:00
|
|
|
printf("xbps-digest: malloc failed (%s)\n",
|
|
|
|
strerror(errno));
|
2008-12-29 14:56:40 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2008-10-29 17:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SHA256_Init(&ctx);
|
|
|
|
while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
|
|
|
|
SHA256_Update(&ctx, buffer, (size_t)bytes);
|
2008-10-20 14:34:27 +00:00
|
|
|
|
2008-10-29 17:42:23 +00:00
|
|
|
printf("%s\n", SHA256_End(&ctx, digest));
|
|
|
|
free(digest);
|
|
|
|
close(fd);
|
|
|
|
}
|
2008-10-20 14:34:27 +00:00
|
|
|
|
2008-12-29 14:56:40 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2008-10-20 14:34:27 +00:00
|
|
|
}
|