[pacman-dev] [PATCH] Use the full buffer when computing md5/sha256 sums

Dan McGee dan at archlinux.org
Wed Sep 28 05:17:59 EDT 2011


No wonder these were slower than expected. We were only reading 4
(32-bit) or 8 (64-bit) bytes at a time and feeding it to the hash
functions. Define a buffer size constant and use it correctly so we feed
8K at a time into the hashing algorithm.

This cut one larger `-Sw --noconfirm` operation, with nothing to
actually download so only timing integrity, from 3.3s to 1.7s.

This has been broken since the original commit eba521913d6 introducing
OpenSSL usage for crypto hash functions. Boy do I feel stupid.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 lib/libalpm/util.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index a15eb96..18d1c36 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -741,6 +741,8 @@ int _alpm_lstat(const char *path, struct stat *buf)
 }
 
 #ifdef HAVE_LIBSSL
+#define BUFFER_SIZE 8192
+
 static int md5_file(const char *path, unsigned char output[16])
 {
 	FILE *f;
@@ -748,7 +750,7 @@ static int md5_file(const char *path, unsigned char output[16])
 	MD5_CTX ctx;
 	unsigned char *buf;
 
-	CALLOC(buf, 8192, sizeof(unsigned char), return 1);
+	CALLOC(buf, BUFFER_SIZE, sizeof(unsigned char), return 1);
 
 	if((f = fopen(path, "rb")) == NULL) {
 		free(buf);
@@ -757,7 +759,7 @@ static int md5_file(const char *path, unsigned char output[16])
 
 	MD5_Init(&ctx);
 
-	while((n = fread(buf, 1, sizeof(buf), f)) > 0) {
+	while((n = fread(buf, 1, BUFFER_SIZE, f)) > 0) {
 		MD5_Update(&ctx, buf, n);
 	}
 
@@ -783,7 +785,7 @@ static int sha2_file(const char *path, unsigned char output[32], int is224)
 	SHA256_CTX ctx;
 	unsigned char *buf;
 
-	CALLOC(buf, 8192, sizeof(unsigned char), return 1);
+	CALLOC(buf, BUFFER_SIZE, sizeof(unsigned char), return 1);
 
 	if((f = fopen(path, "rb")) == NULL) {
 		free(buf);
@@ -796,7 +798,7 @@ static int sha2_file(const char *path, unsigned char output[32], int is224)
 		SHA256_Init(&ctx);
 	}
 
-	while((n = fread(buf, 1, sizeof(buf), f)) > 0) {
+	while((n = fread(buf, 1, BUFFER_SIZE, f)) > 0) {
 		if(is224) {
 			SHA224_Update(&ctx, buf, n);
 		} else {
-- 
1.7.6.4



More information about the pacman-dev mailing list