Do SHA1 hash _before_ compression.
[gitweb.git] / sha1_file.c
index bb44a01746cb08863fde8a320bee40bcb3806fcf..40c00b77d0e52b31dda1696f10026fe6f92bc082 100644 (file)
@@ -80,12 +80,14 @@ char *sha1_file_name(const unsigned char *sha1)
        return base;
 }
 
-int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
+int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
 {
+       char header[100];
        unsigned char real_sha1[20];
        SHA_CTX c;
 
        SHA1_Init(&c);
+       SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
        SHA1_Update(&c, map, size);
        SHA1_Final(real_sha1, &c);
        return memcmp(sha1, real_sha1, 20) ? -1 : 0;
@@ -172,6 +174,11 @@ int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
        unsigned char sha1[20];
        SHA_CTX c;
 
+       /* Sha1.. */
+       SHA1_Init(&c);
+       SHA1_Update(&c, buf, len);
+       SHA1_Final(sha1, &c);
+
        /* Set it up */
        memset(&stream, 0, sizeof(stream));
        deflateInit(&stream, Z_BEST_COMPRESSION);
@@ -188,11 +195,6 @@ int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
        deflateEnd(&stream);
        size = stream.total_out;
 
-       /* Sha1.. */
-       SHA1_Init(&c);
-       SHA1_Update(&c, compressed, size);
-       SHA1_Final(sha1, &c);
-
        if (write_sha1_buffer(sha1, compressed, size) < 0)
                return -1;
        if (returnsha1)