[cpp-sp] branch main updated: Move hex encoding routine into header.
Scott Cantor
cantor.2 at osu.edu
Wed Jun 4 22:49:44 UTC 2025
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch main
in repository cpp-sp.
View the commit online:
http://git.shibboleth.net/view/?p=cpp-sp.git;a=commit;h=01ec27f506917602b9e46a18182b694d0c3b22a3
The following commit(s) were added to refs/heads/main by this push:
new 01ec27f5 Move hex encoding routine into header.
01ec27f5 is described below
commit 01ec27f506917602b9e46a18182b694d0c3b22a3
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Jun 4 18:49:38 2025 -0400
Move hex encoding routine into header.
---
shibsp/io/impl/CookieManager.cpp | 11 +++--------
shibsp/session/impl/MemorySessionCache.cpp | 18 ++----------------
shibsp/util/Misc.h | 29 +++++++++++++++++++++++++++--
3 files changed, 32 insertions(+), 26 deletions(-)
diff --git a/shibsp/io/impl/CookieManager.cpp b/shibsp/io/impl/CookieManager.cpp
index 4b4b1cbc..165f7ec2 100644
--- a/shibsp/io/impl/CookieManager.cpp
+++ b/shibsp/io/impl/CookieManager.cpp
@@ -24,6 +24,7 @@
#include "SPRequest.h"
#include "RequestMapper.h"
#include "io/CookieManager.h"
+#include "util/Misc.h"
#include "util/PropertySet.h"
#include <boost/lexical_cast.hpp>
@@ -124,17 +125,11 @@ string CookieManager::computeCookieName(const SPRequest& request) const
string cookieName(request.getRequestSettings().first->getString(m_overrideProperty.c_str(), m_defaultName.c_str()));
// This is just a hex-encode to avoid a dependency on a hashing API.
- static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
string encode(request.getAgent().getID());
encode += request.getRequestSettings().first->getString(
RequestMapper::APPLICATION_ID_PROP_NAME, RequestMapper::APPLICATION_ID_PROP_DEFAULT);
- cookieName += '_';
- for (const char* ch = encode.c_str(); *ch; ++ch) {
- cookieName += (DIGITS[((unsigned char)(0xF0 & *ch)) >> 4 ]);
- cookieName += (DIGITS[0x0F & *ch]);
- }
-
- return cookieName;
+
+ return cookieName + '_' + hex_encode(encode);
}
void CookieManager::outputHeader(SPRequest& request, const char* value, int maxAge) const
diff --git a/shibsp/session/impl/MemorySessionCache.cpp b/shibsp/session/impl/MemorySessionCache.cpp
index bdf79dec..1098fea7 100644
--- a/shibsp/session/impl/MemorySessionCache.cpp
+++ b/shibsp/session/impl/MemorySessionCache.cpp
@@ -27,6 +27,7 @@
#include "csprng/csprng.hpp"
#include "session/AbstractSessionCache.h"
#include "logging/Category.h"
+#include "util/Misc.h"
#include <boost/property_tree/ptree.hpp>
@@ -35,7 +36,6 @@ using namespace boost::property_tree;
using namespace std;
namespace {
-
class MemorySessionCache : public virtual AbstractSessionCache {
public:
MemorySessionCache(const ptree& pt);
@@ -55,20 +55,6 @@ namespace {
private:
duthomhas::csprng m_rng;
};
-
- static inline string hexify(string& s) {
- static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
-
- string ret;
-
- for (string::value_type ch : s) {
- ret += (DIGITS[((unsigned char)(0xF0 & ch)) >> 4 ]);
- ret += (DIGITS[0x0F & ch]);
- }
-
- return ret;
- }
-
};
namespace shibsp {
@@ -87,7 +73,7 @@ MemorySessionCache::~MemorySessionCache()
string MemorySessionCache::cache_create(DDF& sessionData)
{
- return hexify(m_rng(string(16,0)));
+ return hex_encode(m_rng(string(16,0)));
}
DDF MemorySessionCache::cache_read(
diff --git a/shibsp/util/Misc.h b/shibsp/util/Misc.h
index 16a69c25..df62612b 100644
--- a/shibsp/util/Misc.h
+++ b/shibsp/util/Misc.h
@@ -29,14 +29,39 @@ namespace shibsp {
/**
* Internal utility used for decoding %XX escapes in various places.
+ *
+ * @param what input escape sequence
+ *
+ * @return decoded character point
*/
- static char x2c(const char* what) {
+ static inline char x2c(const char* what) {
char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
- return(digit);
+ return digit;
+ }
+
+ /**
+ * Translate each byte of a character string into a pair of hexidecimal characters and return
+ * the resulting string.
+ *
+ * @param s input string
+ *
+ * @return encoded string
+ */
+ static inline std::string hex_encode(std::string& s) {
+ static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
+ std::string ret;
+
+ for (std::string::value_type ch : s) {
+ ret += (DIGITS[((unsigned char)(0xF0 & ch)) >> 4 ]);
+ ret += (DIGITS[0x0F & ch]);
+ }
+
+ return ret;
}
/**
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list