Make hex encoding optional to allow replacement of deprecated version.
authorScott Cantor <cantor.2@osu.edu>
Mon, 2 Nov 2009 20:29:51 +0000 (20:29 +0000)
committerScott Cantor <cantor.2@osu.edu>
Mon, 2 Nov 2009 20:29:51 +0000 (20:29 +0000)
xmltooling/security/SecurityHelper.h
xmltooling/security/impl/SecurityHelper.cpp

index 54faf54..4f2bc7e 100644 (file)
@@ -133,14 +133,15 @@ namespace xmltooling {
         static bool matches(const XSECCryptoKey& key1, const XSECCryptoKey& key2);
 
         /**
-         * Performs a hash operation over the supplied data and returns a hex-encoded string.
+         * Performs a hash operation over the supplied data.
          *
          * @param hashAlg   name of hash algorithm, syntax specific to crypto provider
          * @param buf       input data to hash
          * @param buflen    length of input data
-         * @return  hex-encoded result of hash operation, or an empty string
+         * @param toHex     if true, hex-encodes the resulting raw bytes
+         * @return  result of hash operation, or an empty string
          */
-        static std::string doHash(const char* hashAlg, const char* buf, unsigned long buflen);
+        static std::string doHash(const char* hashAlg, const char* buf, unsigned long buflen, bool toHex=true);
 
         /**
          * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
index 91bc3b4..c61d233 100644 (file)
@@ -484,7 +484,7 @@ bool SecurityHelper::matches(const XSECCryptoKey& key1, const XSECCryptoKey& key
     return false;
 }
 
-string SecurityHelper::doHash(const char* hashAlg, const char* buf, unsigned long buflen)
+string SecurityHelper::doHash(const char* hashAlg, const char* buf, unsigned long buflen, bool toHex)
 {
     static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
     string ret;
@@ -511,9 +511,16 @@ string SecurityHelper::doHash(const char* hashAlg, const char* buf, unsigned lon
             );
         return ret;
     }
-    for (unsigned int i=0; i < len; ++i) {
-        ret+=(DIGITS[((unsigned char)(0xF0 & digest[i])) >> 4 ]);
-        ret+=(DIGITS[0x0F & digest[i]]);
+    if (toHex) {
+        for (int i=0; i < len; ++i) {
+            ret += (DIGITS[((unsigned char)(0xF0 & digest[i])) >> 4 ]);
+            ret += (DIGITS[0x0F & digest[i]]);
+        }
+    }
+    else {
+        for (int i=0; i < len; ++i) {
+            ret += digest[i];
+        }
     }
     return ret;
 }