Add bufHex()to Buffer object.

A general-purpose function for converting buffers to hex strings.
This commit is contained in:
David Steele
2018-09-26 22:33:48 +01:00
parent 51484a008f
commit bcca625062
11 changed files with 53 additions and 48 deletions
+1 -1
View File
@@ -91,7 +91,7 @@
</release-item>
<release-item>
<p>Add <code>bufNewZ()</code> to <code>Buffer</code> object.</p>
<p>Add <code>bufNewZ()</code> and <code>bufHex()</code>to <code>Buffer</code> object.</p>
</release-item>
<release-item>
+2 -2
View File
@@ -51,7 +51,7 @@ CODE:
MEM_CONTEXT_XS_TEMP_BEGIN()
{
String *hash = cryptoHashHex(self->pxPayload);
String *hash = bufHex(cryptoHash(self->pxPayload));
RETVAL = newSV(strSize(hash));
SvPOK_only(RETVAL);
@@ -84,7 +84,7 @@ CODE:
STRLEN messageSize;
const unsigned char *messagePtr = (const unsigned char *)SvPV(message, messageSize);
String *hash = cryptoHashOneC(strNew(type), messagePtr, messageSize);
String *hash = bufHex(cryptoHashOneC(strNew(type), messagePtr, messageSize));
RETVAL = newSV(strSize(hash));
SvPOK_only(RETVAL);
+20
View File
@@ -207,6 +207,26 @@ bufEq(const Buffer *this, const Buffer *compare)
FUNCTION_TEST_RESULT(BOOL, result);
}
/***********************************************************************************************************************************
Convert the buffer to a hex string
***********************************************************************************************************************************/
String *
bufHex(const Buffer *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(BUFFER, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
String *result = strNew("");
for (unsigned int bufferIdx = 0; bufferIdx < this->size; bufferIdx++)
strCatFmt(result, "%02x", this->buffer[bufferIdx]);
FUNCTION_TEST_RESULT(STRING, result);
}
/***********************************************************************************************************************************
Move buffer to a new mem context
***********************************************************************************************************************************/
+1
View File
@@ -24,6 +24,7 @@ Buffer *bufCat(Buffer *this, const Buffer *cat);
Buffer *bufCatC(Buffer *this, const unsigned char *cat, size_t catOffset, size_t catSize);
Buffer *bufCatSub(Buffer *this, const Buffer *cat, size_t catOffset, size_t catSize);
bool bufEq(const Buffer *this, const Buffer *compare);
String *bufHex(const Buffer *this);
Buffer *bufMove(Buffer *this, MemContext *parentNew);
Buffer *bufResize(Buffer *this, size_t size);
+9 -30
View File
@@ -177,27 +177,6 @@ cryptoHash(CryptoHash *this)
FUNCTION_DEBUG_RESULT(BUFFER, this->hash);
}
/***********************************************************************************************************************************
Get string representation of the hash
***********************************************************************************************************************************/
String *
cryptoHashHex(CryptoHash *this)
{
FUNCTION_DEBUG_BEGIN(logLevelTrace);
FUNCTION_DEBUG_PARAM(CRYPTO_HASH, this);
FUNCTION_DEBUG_ASSERT(this != NULL);
FUNCTION_DEBUG_END();
const Buffer *hash = cryptoHash(this);
String *hashStr = strNew("");
for (unsigned int hashIdx = 0; hashIdx < bufSize(hash); hashIdx++)
strCatFmt(hashStr, "%02x", bufPtr(hash)[hashIdx]);
FUNCTION_DEBUG_RESULT(STRING, hashStr);
}
/***********************************************************************************************************************************
Get filter interface
***********************************************************************************************************************************/
@@ -229,7 +208,7 @@ cryptoHashResult(CryptoHash *this)
MEM_CONTEXT_BEGIN(this->memContext)
{
result = varNewStr(cryptoHashHex(this));
result = varNewStr(bufHex(cryptoHash(this)));
}
MEM_CONTEXT_END();
@@ -263,7 +242,7 @@ cryptoHashFree(CryptoHash *this)
/***********************************************************************************************************************************
Get hash for one C buffer
***********************************************************************************************************************************/
String *
Buffer *
cryptoHashOneC(const String *type, const unsigned char *message, size_t messageSize)
{
FUNCTION_DEBUG_BEGIN(logLevelTrace);
@@ -274,7 +253,7 @@ cryptoHashOneC(const String *type, const unsigned char *message, size_t messageS
FUNCTION_DEBUG_ASSERT(message != NULL);
FUNCTION_DEBUG_END();
String *result = NULL;
Buffer *result = NULL;
MEM_CONTEXT_TEMP_BEGIN()
{
@@ -282,18 +261,18 @@ cryptoHashOneC(const String *type, const unsigned char *message, size_t messageS
cryptoHashProcessC(hash, message, messageSize);
memContextSwitch(MEM_CONTEXT_OLD());
result = cryptoHashHex(hash);
result = bufNewC(bufSize(cryptoHash(hash)), bufPtr(cryptoHash(hash)));
memContextSwitch(MEM_CONTEXT_TEMP());
}
MEM_CONTEXT_TEMP_END();
FUNCTION_DEBUG_RESULT(STRING, result);
FUNCTION_DEBUG_RESULT(BUFFER, result);
}
/***********************************************************************************************************************************
Get hash for one Buffer
***********************************************************************************************************************************/
String *
Buffer *
cryptoHashOne(const String *type, Buffer *message)
{
FUNCTION_TEST_BEGIN();
@@ -304,13 +283,13 @@ cryptoHashOne(const String *type, Buffer *message)
FUNCTION_TEST_ASSERT(message != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(STRING, cryptoHashOneC(type, bufPtr(message), bufSize(message)));
FUNCTION_TEST_RESULT(BUFFER, cryptoHashOneC(type, bufPtr(message), bufSize(message)));
}
/***********************************************************************************************************************************
Get hash for one String
***********************************************************************************************************************************/
String *
Buffer *
cryptoHashOneStr(const String *type, String *message)
{
FUNCTION_TEST_BEGIN();
@@ -321,5 +300,5 @@ cryptoHashOneStr(const String *type, String *message)
FUNCTION_TEST_ASSERT(message != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(STRING, cryptoHashOneC(type, (const unsigned char *)strPtr(message), strSize(message)));
FUNCTION_TEST_RESULT(BUFFER, cryptoHashOneC(type, (const unsigned char *)strPtr(message), strSize(message)));
}
+3 -4
View File
@@ -37,7 +37,6 @@ void cryptoHashProcessStr(CryptoHash *this, const String *message);
Getters
***********************************************************************************************************************************/
const Buffer *cryptoHash(CryptoHash *this);
String *cryptoHashHex(CryptoHash *this);
IoFilter *cryptoHashFilter(CryptoHash *this);
const Variant *cryptoHashResult(CryptoHash *this);
@@ -49,9 +48,9 @@ void cryptoHashFree(CryptoHash *this);
/***********************************************************************************************************************************
Helper functions
***********************************************************************************************************************************/
String *cryptoHashOne(const String *type, Buffer *message);
String *cryptoHashOneC(const String *type, const unsigned char *message, size_t messageSize);
String *cryptoHashOneStr(const String *type, String *message);
Buffer *cryptoHashOne(const String *type, Buffer *message);
Buffer *cryptoHashOneC(const String *type, const unsigned char *message, size_t messageSize);
Buffer *cryptoHashOneStr(const String *type, String *message);
/***********************************************************************************************************************************
Macros for function logging
+2 -2
View File
@@ -126,11 +126,11 @@ infoValidInternal(
CryptoHash *hash = infoHash(this->ini);
// ??? Temporary hack until get json parser: add quotes around hash before comparing
if (!strEq(infoChecksum, strQuoteZ(cryptoHashHex(hash), "\"")))
if (!strEq(infoChecksum, strQuoteZ(bufHex(cryptoHash(hash)), "\"")))
{
// ??? Temporary hack until get json parser: remove quotes around hash before displaying in messsage & check < 3
String *chksumMsg = strNewFmt("invalid checksum in '%s', expected '%s' but found '%s'",
strPtr(this->fileName), strPtr(cryptoHashHex(hash)), (strSize(infoChecksum) < 3) ?
strPtr(this->fileName), strPtr(bufHex(cryptoHash(hash))), (strSize(infoChecksum) < 3) ?
"[undef]" : strPtr(strSubN(infoChecksum, 1, strSize(infoChecksum) - 2)));
if (!ignoreError)
+2 -2
View File
@@ -513,7 +513,7 @@ XS_EUPXS(XS_pgBackRest__LibC__Crypto__Hash_result)
MEM_CONTEXT_XS_TEMP_BEGIN()
{
String *hash = cryptoHashHex(self->pxPayload);
String *hash = bufHex(cryptoHash(self->pxPayload));
RETVAL = newSV(strSize(hash));
SvPOK_only(RETVAL);
@@ -571,7 +571,7 @@ XS_EUPXS(XS_pgBackRest__LibC_cryptoHashOne)
STRLEN messageSize;
const unsigned char *messagePtr = (const unsigned char *)SvPV(message, messageSize);
String *hash = cryptoHashOneC(strNew(type), messagePtr, messageSize);
String *hash = bufHex(cryptoHashOneC(strNew(type), messagePtr, messageSize));
RETVAL = newSV(strSize(hash));
SvPOK_only(RETVAL);
+1 -1
View File
@@ -166,7 +166,7 @@ unit:
# ----------------------------------------------------------------------------------------------------------------------------
- name: type-buffer
total: 5
total: 6
coverage:
common/type/buffer: full
+6
View File
@@ -112,6 +112,12 @@ testRun(void)
TEST_RESULT_BOOL(bufEq(bufNewZ("123"), bufNewZ("123")), true, "buffers equal");
}
// *****************************************************************************************************************************
if (testBegin("bufHex()"))
{
TEST_RESULT_STR(strPtr(bufHex(bufNewZ("ABC-CBA"))), "4142432d434241", "buffer to hex");
}
// *****************************************************************************************************************************
if (testBegin("bufCat*()"))
{
+6 -6
View File
@@ -25,8 +25,8 @@ testRun(void)
// -------------------------------------------------------------------------------------------------------------------------
TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_SHA1)), "create sha1 hash");
TEST_RESULT_STR(strPtr(cryptoHashHex(hash)), "da39a3ee5e6b4b0d3255bfef95601890afd80709", " check empty hash");
TEST_RESULT_STR(strPtr(cryptoHashHex(hash)), "da39a3ee5e6b4b0d3255bfef95601890afd80709", " check empty hash again");
TEST_RESULT_STR(strPtr(bufHex(cryptoHash(hash))), "da39a3ee5e6b4b0d3255bfef95601890afd80709", " check empty hash");
TEST_RESULT_STR(strPtr(bufHex(cryptoHash(hash))), "da39a3ee5e6b4b0d3255bfef95601890afd80709", " check empty hash again");
TEST_RESULT_VOID(cryptoHashFree(hash), " free hash");
// -------------------------------------------------------------------------------------------------------------------------
@@ -44,12 +44,12 @@ testRun(void)
// -------------------------------------------------------------------------------------------------------------------------
TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_MD5)), "create md5 hash");
TEST_RESULT_STR(strPtr(cryptoHashHex(hash)), "d41d8cd98f00b204e9800998ecf8427e", " check empty hash");
TEST_RESULT_STR(strPtr(bufHex(cryptoHash(hash))), "d41d8cd98f00b204e9800998ecf8427e", " check empty hash");
// -------------------------------------------------------------------------------------------------------------------------
TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_SHA256)), "create sha256 hash");
TEST_RESULT_STR(
strPtr(cryptoHashHex(hash)), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
strPtr(bufHex(cryptoHash(hash))), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
" check empty hash");
}
@@ -57,10 +57,10 @@ testRun(void)
if (testBegin("cryptoHashOne*()"))
{
TEST_RESULT_STR(
strPtr(cryptoHashOne(strNew(HASH_TYPE_SHA1), bufNewZ("12345"))), "8cb2237d0679ca88db6464eac60da96345513964",
strPtr(bufHex(cryptoHashOne(strNew(HASH_TYPE_SHA1), bufNewZ("12345")))), "8cb2237d0679ca88db6464eac60da96345513964",
" check small hash");
TEST_RESULT_STR(
strPtr(cryptoHashOneStr(strNew(HASH_TYPE_SHA1), strNew("12345"))), "8cb2237d0679ca88db6464eac60da96345513964",
strPtr(bufHex(cryptoHashOneStr(strNew(HASH_TYPE_SHA1), strNew("12345")))), "8cb2237d0679ca88db6464eac60da96345513964",
" check small hash");
}