]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added guid_128_host_hash_get().
authorTimo Sirainen <tss@iki.fi>
Mon, 18 Feb 2013 06:45:54 +0000 (08:45 +0200)
committerTimo Sirainen <tss@iki.fi>
Mon, 18 Feb 2013 06:45:54 +0000 (08:45 +0200)
src/lib/guid.c
src/lib/guid.h

index 4f848b6a6e88f6bd9d5e095c0d71444e1f34594f..8db6cee88a3502704f2304b30881dcfbf3181c25 100644 (file)
@@ -34,12 +34,23 @@ const char *guid_generate(void)
                               pid, my_hostname);
 }
 
+void guid_128_host_hash_get(const char *host,
+                           unsigned char hash_r[GUID_128_HOST_HASH_SIZE])
+{
+       unsigned char full_hash[SHA1_RESULTLEN];
+
+       sha1_get_digest(host, strlen(host), full_hash);
+       memcpy(hash_r, full_hash + sizeof(full_hash)-GUID_128_HOST_HASH_SIZE,
+              GUID_128_HOST_HASH_SIZE);
+}
+
 void guid_128_generate(guid_128_t guid_r)
 {
+#if GUID_128_HOST_HASH_SIZE != 4
+#  error GUID_128_HOST_HASH_SIZE must be 4
+#endif
        static struct timespec ts = { 0, 0 };
        static uint8_t guid_static[8];
-       unsigned char hostdomain_hash[SHA1_RESULTLEN];
-       const char *hostdomain;
        uint32_t pid;
 
        /* we'll use the current time in nanoseconds as the initial 64bit
@@ -48,16 +59,12 @@ void guid_128_generate(guid_128_t guid_r)
                if (clock_gettime(CLOCK_REALTIME, &ts) < 0)
                        i_fatal("clock_gettime() failed: %m");
                pid = getpid();
-               hostdomain = my_hostdomain();
-               sha1_get_digest(hostdomain, strlen(hostdomain),
-                               hostdomain_hash);
 
                guid_static[0] = (pid & 0x000000ff);
                guid_static[1] = (pid & 0x0000ff00) >> 8;
                guid_static[2] = (pid & 0x00ff0000) >> 16;
                guid_static[3] = (pid & 0xff000000) >> 24;
-               memcpy(guid_static+4,
-                      hostdomain_hash+sizeof(hostdomain_hash)-4, 4);
+               guid_128_host_hash_get(my_hostdomain(), guid_static+4);
        } else if ((uint32_t)ts.tv_nsec < (uint32_t)-1) {
                ts.tv_nsec++;
        } else {
index 3fb1532584ec18b13123cc81ba8ac2509d9ecb63..ca4cf8a282fbee212d38631c450472c351f93799 100644 (file)
@@ -4,6 +4,8 @@
 #define GUID_128_SIZE 16
 typedef uint8_t guid_128_t[GUID_128_SIZE];
 
+#define GUID_128_HOST_HASH_SIZE 4
+
 /* Generate a GUID (contains host name) */
 const char *guid_generate(void);
 /* Generate 128 bit GUID */
@@ -20,4 +22,8 @@ int guid_128_from_string(const char *str, guid_128_t guid_r);
 unsigned int guid_128_hash(const uint8_t *guid);
 int guid_128_cmp(const uint8_t *guid1, const uint8_t *guid2);
 
+/* Return the hash of host used by guid_128_generate(). */
+void guid_128_host_hash_get(const char *host,
+                           unsigned char hash_r[GUID_128_HOST_HASH_SIZE]);
+
 #endif