CFLib.org – Common Function Library Project

nullPad(plainText, blockSize[, encoding])

Last updated May 11, 2016

author

Leigh

Version: 1 | Requires: CF10 | Library: StrLib

Description:
Pads a string with null bytes, to a multiple of the given block size. Useful for encryption compatibility.

Return Values:
The supplied string padded with null bytes.

Example:

<cfscript>
    // Note: Verify using string length (null bytes are not visible when displayed with writeOutput)
    originalText = "Four";
    writeOutput("<br>Original Text  Length = "& len(originalText));
    paddedText = nullPad(originalText, 8);
    writeOutput("<br>Padded Text Length = "& len(paddedText));
</cfscript>

Parameters:

Name Description Required
plainText Input string. Yes
blockSize Size of desired output. Yes
encoding Encoding of string, defaults to UTF-8. No

Full UDF Source:

string function nullPad( string plainText, numeric blockSize, string encoding="UTF-8")
{
    if (arguments.blockSize <= 0) {
        throw("Block size must be greater than 0");
    }
    local.newText = arguments.plainText;
    local.bytes = charsetDecode(arguments.plainText, arguments.encoding);
    local.remain = arrayLen( local.bytes ) % arguments.blockSize;

    if (local.remain != 0) 
    {
        local.padSize = arguments.blockSize - local.remain;
        local.newText &= repeatString( urlDecode("%00"), local.padSize );
    }

    return local.newText;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson