1 /* sha1.c - Interfaces to SHA-1 hash for the notmuch mail system
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
25 /* Just some simple interfaces on top of libsha1 so that we can leave
26 * libsha1 as untouched as possible. */
29 notmuch_sha1_of_file (const char *filename)
32 #define BLOCK_SIZE 4096
33 unsigned char block[BLOCK_SIZE];
36 unsigned char digest[SHA1_DIGEST_SIZE];
40 file = fopen (filename, "r");
47 bytes_read = fread (block, 1, 4096, file);
48 if (bytes_read == 0) {
51 } else if (ferror (file)) {
56 sha1_hash (block, bytes_read, &sha1);
60 sha1_end (digest, &sha1);
62 result = calloc (SHA1_DIGEST_SIZE * 2 + 1, 1);
66 for (r = result, i = 0;
70 sprintf (r, "%02x", digest[i]);