FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > ArchLinux > ArchLinux Pacman Development

 
 
LinkBack Thread Tools
 
Old 08-11-2011, 08:47 PM
Dan McGee
 
Default Add ALPM sha256sum routines

These mirror ones we already have for md5sums.

Signed-off-by: Dan McGee <dan@archlinux.org>
---
lib/libalpm/alpm.h | 1 +
lib/libalpm/util.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ ++
2 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 32db687..44f3d41 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -1052,6 +1052,7 @@ char *alpm_dep_compute_string(const alpm_depend_t *dep);

/* checksums */
char *alpm_compute_md5sum(const char *name);
+char *alpm_compute_sha256sum(const char *filename);

/** @addtogroup alpm_api_errors Error Codes
* @{
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index adb1d19..b1f37eb 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -46,8 +46,10 @@

#ifdef HAVE_LIBSSL
#include <openssl/md5.h>
+#include <openssl/sha.h>
#else
#include "md5.h"
+#include "sha2.h"
#endif

/* libalpm */
@@ -747,6 +749,53 @@ static int md5_file(const char *path, unsigned char output[16])
fclose(f);
return 0;
}
+
+/* third param is so we match the PolarSSL definition */
+static int sha2_file(const char *path, unsigned char output[32], int is224)
+{
+ FILE *f;
+ size_t n;
+ SHA256_CTX ctx;
+ unsigned char *buf;
+
+ CALLOC(buf, 8192, sizeof(unsigned char), return 1);
+
+ if((f = fopen(path, "rb")) == NULL) {
+ free(buf);
+ return 1;
+ }
+
+ if(is224) {
+ SHA224_Init(&ctx);
+ } else {
+ SHA256_Init(&ctx);
+ }
+
+ while((n = fread(buf, 1, sizeof(buf), f)) > 0) {
+ if(is224) {
+ SHA224_Update(&ctx, buf, n);
+ } else {
+ SHA256_Update(&ctx, buf, n);
+ }
+ }
+
+ if(is224) {
+ SHA224_Final(output, &ctx);
+ } else {
+ SHA256_Final(output, &ctx);
+ }
+
+ memset(&ctx, 0, sizeof(SHA256_CTX));
+ free(buf);
+
+ if(ferror(f) != 0) {
+ fclose(f);
+ return 2;
+ }
+
+ fclose(f);
+ return 0;
+}
#endif

/** Get the md5 sum of file.
@@ -799,6 +848,37 @@ int _alpm_test_md5sum(const char *filepath, const char *md5sum)
return ret;
}

+/** Get the sha256 sum of file.
+ * @param filename name of the file
+ * @return the checksum on success, NULL on error
+ * @addtogroup alpm_misc
+ */
+char SYMEXPORT *alpm_compute_sha256sum(const char *filename)
+{
+ unsigned char output[16];
+ char *sha256sum;
+ int ret, i;
+
+ ASSERT(filename != NULL, return NULL);
+
+ /* allocate 32 chars plus 1 for null */
+ CALLOC(sha256sum, 65, sizeof(char), return NULL);
+ /* defined above for OpenSSL, otherwise defined in sha2.h */
+ ret = sha2_file(filename, output, 0);
+
+ if(ret > 0) {
+ return NULL;
+ }
+
+ /* Convert the result to something readable */
+ for (i = 0; i < 16; i++) {
+ /* sprintf is acceptable here because we know our output */
+ sprintf(sha256sum +(i * 2), "%02x", output[i]);
+ }
+
+ return sha256sum;
+}
+
/* Note: does NOT handle sparse files on purpose for speed. */
int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b)
{
--
1.7.6
 

Thread Tools




All times are GMT. The time now is 08:01 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org