From: cantor Date: Mon, 2 Nov 2009 20:29:51 +0000 (+0000) Subject: Make hex encoding optional to allow replacement of deprecated version. X-Git-Tag: 1.4.1~163 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fxmltooling.git;a=commitdiff_plain;h=42bf696ea7ece15a19dbbc099566733370bb9055 Make hex encoding optional to allow replacement of deprecated version. git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/branches/REL_1@671 de75baf8-a10c-0410-a50a-987c0e22f00f --- diff --git a/xmltooling/security/SecurityHelper.h b/xmltooling/security/SecurityHelper.h index 54faf54..4f2bc7e 100644 --- a/xmltooling/security/SecurityHelper.h +++ b/xmltooling/security/SecurityHelper.h @@ -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. diff --git a/xmltooling/security/impl/SecurityHelper.cpp b/xmltooling/security/impl/SecurityHelper.cpp index 91bc3b4..c61d233 100644 --- a/xmltooling/security/impl/SecurityHelper.cpp +++ b/xmltooling/security/impl/SecurityHelper.cpp @@ -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; }