add digestVector() and digestReset() functions, fix bug in cleanup() function
authorMichael Schroeder <mls@suse.de>
Fri, 18 Jun 2010 10:19:20 +0000 (12:19 +0200)
committerMichael Schroeder <mls@suse.de>
Fri, 18 Jun 2010 10:19:20 +0000 (12:19 +0200)
zypp/Digest.cc
zypp/Digest.h

index 091555f..fcbe997 100644 (file)
@@ -121,6 +121,7 @@ namespace zypp {
       {
        EVP_MD_CTX_cleanup(&mdctx);
        initialized = false;
+       finalized = false;
       }
     }
 
@@ -150,6 +151,21 @@ namespace zypp {
       return _dp->name;
     }
 
+    bool Digest::reset()
+    {
+      if (!_dp->initialized)
+       return false;
+      if(!_dp->finalized)
+       {
+         (void)EVP_DigestFinal_ex(&_dp->mdctx, _dp->md_value, &_dp->md_len);
+          _dp->finalized = true;
+       }
+      if(!EVP_DigestInit_ex(&_dp->mdctx, _dp->md, NULL))
+       return false;
+      _dp->finalized = false;
+      return true;
+    }
+
     std::string Digest::digest()
     {
       if(!_dp->maybeInit())
@@ -174,6 +190,24 @@ namespace zypp {
       return std::string(mdtxt);
     }
 
+    std::vector<unsigned char> Digest::digestVector()
+    {
+      std::vector<unsigned char> r;
+      if(!_dp->maybeInit())
+        return r;
+
+      if(!_dp->finalized)
+      {   
+        if(!EVP_DigestFinal_ex(&_dp->mdctx, _dp->md_value, &_dp->md_len))
+            return r;
+        _dp->finalized = true;
+      }   
+      r.reserve(_dp->md_len);
+      for(unsigned i = 0; i < _dp->md_len; ++i)
+       r.push_back(_dp->md_value[i]);
+      return r;
+    }
+
     bool Digest::update(const char* bytes, size_t len)
     {
       if(!bytes)
index 4b1937f..69556bf 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <string>
 #include <iosfwd>
+#include <vector>
 
 #include <zypp/Callback.h>
 #include <zypp/Pathname.h>
@@ -101,6 +102,22 @@ namespace zypp {
         * */
        std::string digest();
 
+       /** \brief get vector of unsigned char representation of the digest
+        *
+        * this function will finalize the digest computation. calls to update
+        * after this function will start from scratch
+        *
+        * @return vector representation of the digest
+        * */
+       std::vector<unsigned char> digestVector();
+
+       /** \brief reset internal digest state
+        *
+        * this function is equivalent to calling create() with an unchanged name,
+        * but it may be implemented in a more efficient way.
+        */
+       bool reset();
+
        /** \brief compute digest of a stream. convenience function
         *
         * calls create, update and digest in one function. The data for the