aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-09-14 09:42:37 +0200
committerMattias Andrée <maandree@operamail.com>2015-09-14 09:42:37 +0200
commitab1775193129c6fb32a39d229b959b5b75d31d1b (patch)
tree4ac32ef6c254d1dc419b9b34a4f2b44b4144a4af
parentimplement sha2 (diff)
downloadlibsha2-ab1775193129c6fb32a39d229b959b5b75d31d1b.tar.gz
libsha2-ab1775193129c6fb32a39d229b959b5b75d31d1b.tar.bz2
libsha2-ab1775193129c6fb32a39d229b959b5b75d31d1b.tar.xz
add libsha2_sum_fd
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--src/libsha2.h1
-rw-r--r--src/libsha2/files.c65
-rw-r--r--src/libsha2/files.h40
3 files changed, 106 insertions, 0 deletions
diff --git a/src/libsha2.h b/src/libsha2.h
index 7fdee6b..d6ebd2b 100644
--- a/src/libsha2.h
+++ b/src/libsha2.h
@@ -23,6 +23,7 @@
#include "libsha2/digest.h"
#include "libsha2/state.h"
#include "libsha2/hex.h"
+#include "libsha2/files.h"
#endif
diff --git a/src/libsha2/files.c b/src/libsha2/files.c
new file mode 100644
index 0000000..2f4bb72
--- /dev/null
+++ b/src/libsha2/files.c
@@ -0,0 +1,65 @@
+/**
+ * libsha2 – SHA-2-family hashing library
+ *
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "files.h"
+#include "digest.h"
+#include <stddef.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <alloca.h>
+
+
+
+/**
+ * Calculate the checksum for a file,
+ * the content of the file is assumed non-sensitive
+ *
+ * @param fd The file descriptor of the file
+ * @param algorithm The hashing algorithm
+ * @param hashsum Output buffer for the hash
+ * @return Zero on success, -1 on error
+ */
+int libsha2_sum_fd(int fd, libsha2_algorithm_t algorithm, char* restrict hashsum)
+{
+ libsha2_state_t state;
+ ssize_t got;
+ struct stat attr;
+ size_t blksize = 4096;
+ char* restrict chunk;
+
+ if (libsha2_state_initialise(&state, algorithm) < 0)
+ return -1;
+
+ if (fstat(fd, &attr) == 0)
+ if (attr.st_blksize > 0)
+ blksize = (size_t)(attr.st_blksize);
+
+ chunk = alloca(blksize);
+
+ for (;;)
+ {
+ got = read(fd, chunk, blksize);
+ if (got < 0) return -1;
+ if (got == 0) break;
+ libsha2_update(&state, chunk, (size_t)got);
+ }
+
+ libsha2_digest(&state, NULL, 0, hashsum);
+ return 0;
+}
+
diff --git a/src/libsha2/files.h b/src/libsha2/files.h
new file mode 100644
index 0000000..2854cd7
--- /dev/null
+++ b/src/libsha2/files.h
@@ -0,0 +1,40 @@
+/**
+ * libsha2 – SHA-2-family hashing library
+ *
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef LIBSHA2_FILES_H
+#define LIBSHA2_FILES_H 1
+
+
+#include "state.h"
+
+
+/**
+ * Calculate the checksum for a file,
+ * the content of the file is assumed non-sensitive
+ *
+ * @param fd The file descriptor of the file
+ * @param algorithm The hashing algorithm
+ * @param hashsum Output buffer for the hash
+ * @return Zero on success, -1 on error
+ */
+__attribute__((nonnull, leaf))
+int libsha2_sum_fd(int fd, libsha2_algorithm_t algorithm, char* restrict hashsum);
+
+
+#endif
+