Select Git revision
sha512sum.c
-
Anders Blomdell authoredAnders Blomdell authored
sha512sum.c 3.24 KiB
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <openssl/sha.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include "libhash.h"
#define BLOCKSIZE 4096
#define XATTR_SHA512HASH "trusted.sha512.control.lth.se"
struct __attribute__((__packed__)) hash_attr {
time_t hash_time;
time_t mtime;
uint32_t mtime_nsec;
uint8_t hash[SHA512_DIGEST_LENGTH];
};
int hash_sha512_buf(char *buf,
size_t length,
unsigned char *hash)
{
SHA512_CTX ctx;
SHA512_Init(&ctx);
SHA512_Update(&ctx, buf, length);
SHA512_Final(hash, &ctx);
return 0;
}
int hash_sha512_file(char *filename,
time_t mtime,
int64_t mtime_nsec,
int flags,
time_t maxage,
unsigned char *hash)
{
int fd;
int result = 0, recalc = 1;
struct hash_attr hash_attr;
if (flags & FLAGS_CLEAR_XATTR) {
if (removexattr(filename, XATTR_SHA512HASH) != 0) {
if (errno != ENODATA) {
result = -1;
fprintf(stderr, "Failed to remove xattr '%s'\n", filename);
goto out;
} else if ((flags & FLAGS_VERBOSE_MASK) > FLAGS_VERBOSE2) {
fprintf(stderr, "No xattr for '%s'\n", filename);
}
} else {
if ((flags & FLAGS_VERBOSE_MASK) > FLAGS_VERBOSE2) {
fprintf(stderr, "Removed xattr for '%s'\n", filename);
}
}
}
if (flags & FLAGS_NO_CALC_HASH) {
// We should not calculate hash
goto out;
}
if (flags & FLAGS_READ_XATTR) {