diff --git a/sll/trunk/cypher.cc b/sll/trunk/cypher.cc index 79aa6f2233eaa6d362563e07b0aee11f4c44316c..01485946e13cf3d7666353a9ccfe1d7b71a10f29 100644 --- a/sll/trunk/cypher.cc +++ b/sll/trunk/cypher.cc @@ -18,6 +18,7 @@ #include "slm.hh" #include "cypher.hh" +#include "error.hh" #include #include @@ -25,23 +26,6 @@ #include #include -/** - * return sha1 of data - * - * @param data data to sha1 and base64 - * @param size size of data - * - * @return malloc'ed sha1 size - */ -unsigned char *Cypher::sha1(char *data, size_t size) { - unsigned char *md = new unsigned char[SHA_DIGEST_LENGTH]; - - // compute sha1 - SHA1((const unsigned char *) data, size, md); - - return md; -} - /** * return sha1 of data with base64 encoding * @@ -50,11 +34,11 @@ unsigned char *Cypher::sha1(char *data, size_t size) { * * @return malloc'ed sha1 size */ -char *Cypher::sha1_64(char *data, size_t size) { +string Cypher::sha1_64(const char *data, size_t size) { unsigned char md[SHA_DIGEST_LENGTH]; - char *buf; BIO *bmem, *b64; BUF_MEM *bptr; + string ret; // compute sha1 SHA1((const unsigned char *) data, size, md); @@ -67,13 +51,46 @@ char *Cypher::sha1_64(char *data, size_t size) { BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); - if (bptr->length > 0) { - buf2 = new char[bptr->length]; - memcpy(buf, bptr->data, bptr->length - 1); - buf[bptr->length - 1] = 0; - } + if (bptr->length > 0) + ret.insert(0, bptr->data, bptr->length); + else + throw Error(ERR_CYPHER, "Unable to compute sha1_64"); BIO_free_all(b64); - return buf; + return ret; +} + +/** + * return md5 hexa digest of file @param file. + * + * @return malloc'ed md5 digest in hexadecimal + */ +string Cypher::md5_16(const string &file) { + MD5_CTX ctx; + FILE *fs; + size_t len; + char buf[512]; + char md[MD5_DIGEST_LENGTH]; + char digest[MD5_DIGEST_LENGTH * 2 + 1]; + + if (!MD5_Init(&ctx)) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + if ((fs = fopen(file.c_str(), "r")) == 0) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + while ((len = fread(buf, 1, 512, fs)) > 0) + if (!MD5_Update(&ctx, buf, len)) + break; + + if (!MD5_Final((unsigned char*)md, &ctx)) + throw Error(ERR_CYPHER, (string) "Unable to compute md5_16 on file " + file); + + for(len = 0; len < MD5_DIGEST_LENGTH; ++len) + sprintf(digest + (len * 2), "%02x", (unsigned char) md[len]); + + digest[MD5_DIGEST_LENGTH * 2] = 0; + + return digest; } diff --git a/sll/trunk/cypher.hh b/sll/trunk/cypher.hh index 4bb7773bbc587f723f165d317f0d82f71bedb9ca..2018228182c04c1fbf390c9a62aae8410d400976 100644 --- a/sll/trunk/cypher.hh +++ b/sll/trunk/cypher.hh @@ -23,9 +23,11 @@ class Cypher { public: - unsigned char *sha1(char *, size_t); - unsigned char *sha1_64(char *, size_t); -protected: + // md5 functions + static string md5_16(const string &file); + + // sha1 functions + static string sha1_64(const char *, size_t); }; #endif diff --git a/sll/trunk/slm.hh b/sll/trunk/slm.hh index e04a2fd249e5a81fb4182b51a671c873c9131bf1..8c6f809a2bb470be777ecfde23c140fad8b11c10 100644 --- a/sll/trunk/slm.hh +++ b/sll/trunk/slm.hh @@ -47,6 +47,7 @@ enum { ERR_PARSE = 10, ERR_SCREENSZ = 11, ERR_SIGNAL = 12, + ERR_CYPHER = 13, ERR_NOMEM = 41, ERR_UNKNOWN = 42 };