Back to index

nordugrid-arc-nox  1.1.0~rc6
FileCacheHash.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <cstdio>
00008 #include <cstring>
00009 
00010 #include <openssl/evp.h>
00011 
00012 #include "FileCacheHash.h"
00013 
00014 int FileCacheHash::MAX_MD5_LENGTH = 32;
00015 int FileCacheHash::MAX_SHA1_LENGTH = 40;
00016 
00017 std::string FileCacheHash::getHash(std::string url) {
00018 
00019   /*
00020    * example borrowed from http://www.openssl.org/docs/crypto/EVP_DigestInit.html
00021    */
00022   EVP_MD_CTX mdctx;
00023   const EVP_MD *md = EVP_sha1(); // change to EVP_md5() for md5 hashes
00024   char *mess1 = (char*)url.c_str();
00025   unsigned char md_value[EVP_MAX_MD_SIZE];
00026   unsigned int md_len, i;
00027 
00028   OpenSSL_add_all_digests();
00029 
00030   EVP_MD_CTX_init(&mdctx);
00031   EVP_DigestInit_ex(&mdctx, md, NULL);
00032   EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
00033   EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
00034   EVP_MD_CTX_cleanup(&mdctx);
00035 
00036   char result[3];
00037   std::string res("");
00038   for (i = 0; i < md_len; i++) {
00039     snprintf(result, 3, "%02x", md_value[i]);
00040     res.append(result);
00041   }
00042   return res;
00043 }