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 https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
25 /* Create a hexadecimal string version of the SHA-1 digest of 'str'
26 * (including its null terminating character).
28 * This function returns a newly allocated string which the caller
29 * should free() when finished.
32 _notmuch_sha1_of_string (const char *str)
37 sha1 = g_checksum_new (G_CHECKSUM_SHA1);
38 g_checksum_update (sha1, (const guchar *) str, strlen (str) + 1);
39 digest = xstrdup (g_checksum_get_string (sha1));
40 g_checksum_free (sha1);
45 /* Create a hexadecimal string version of the SHA-1 digest of the
46 * contents of the named file.
48 * This function returns a newly allocated string which the caller
49 * should free() when finished.
51 * If any error occurs while reading the file, (permission denied,
52 * file not found, etc.), this function returns NULL.
55 _notmuch_sha1_of_file (const char *filename)
58 #define BLOCK_SIZE 4096
59 unsigned char block[BLOCK_SIZE];
64 file = fopen (filename, "r");
68 sha1 = g_checksum_new (G_CHECKSUM_SHA1);
73 bytes_read = fread (block, 1, 4096, file);
74 if (bytes_read == 0) {
77 else if (ferror (file))
80 g_checksum_update (sha1, block, bytes_read);
84 digest = xstrdup (g_checksum_get_string (sha1));
88 g_checksum_free (sha1);