Wt  4.11.1
Enumerations | Functions
Wt::Utils Namespace Reference

Namespace with utility functions. More...

Enumerations

enum class  HtmlEncodingFlag { EncodeNewLines = 0x1 }
 An enumeration for HTML encoding flags. More...
 

Functions

std::string md5 (const std::string &data)
 Computes an MD5 hash. More...
 
std::string sha1 (const std::string &data)
 Computes a SHA-1 hash. More...
 
std::string base32Encode (const std::string &data, bool crlf=true)
 Performs Base32-encoding of data. More...
 
std::string base32Decode (const std::string &data)
 Performs Base32-decoding of data. More...
 
std::string base64Encode (const std::string &data, bool crlf=true)
 Performs Base64-encoding of data. More...
 
std::string base64Decode (const std::string &data)
 Performs Base64-decoding of data. More...
 
std::string hexEncode (const std::string &data)
 Performs Hex-encoding of data. More...
 
std::string hexDecode (const std::string &data)
 Performs Hex-decoding of data. More...
 
std::string htmlEncode (const std::string &text, WFlags< HtmlEncodingFlag > flags=None)
 Performs HTML encoding of text. More...
 
WString htmlEncode (const WString &text, WFlags< HtmlEncodingFlag > flags=None)
 Performs HTML encoding of text. More...
 
std::string htmlAttributeValue (const std::string &text)
 Escape the given text for inclusion in an HTML attribute. More...
 
std::string urlEncode (const std::string &text)
 Performs Url encoding (aka percentage encoding). More...
 
std::string urlDecode (const std::string &text)
 Performs Url decoding. More...
 
bool removeScript (WString &text)
 Remove tags/attributes from text that are not passive. More...
 
std::string guessImageMimeTypeData (const std::vector< unsigned char > &header)
 Guess the image mime type from an image. More...
 
std::string guessImageMimeType (const std::string &file)
 Guess the image mime type from an image. More...
 
std::string hmac (const std::string &text, const std::string &key, std::string(*hashfunction)(const std::string &), size_t blocksize, size_t keysize)
 Computes a hash-based message authentication code. More...
 
std::string hmac_md5 (const std::string &text, const std::string &key)
 Computes a hash-based message authentication code. More...
 
std::string hmac_sha1 (const std::string &text, const std::string &key)
 Computes a hash-based message authentication code. More...
 

Detailed Description

Namespace with utility functions.

This namespace contains functions for computing message digests with cryptographic hash functions (md5, sha1), and functions that implement encoding and decoding for common encodings.

These utility functions can be accessed by including the Wt/Utils.h header.

Enumeration Type Documentation

◆ HtmlEncodingFlag

An enumeration for HTML encoding flags.

Enumerator
EncodeNewLines 

Encode new-lines as line breaks (<br>)

Function Documentation

◆ base32Decode()

WT_API std::string Wt::Utils::base32Decode ( const std::string &  data)

Performs Base32-decoding of data.

This utility function implements a Base32 decoding (RFC 4328) of the data. Illegal characters are discarded and skipped.

See also
base32Encode()

◆ base32Encode()

WT_API std::string Wt::Utils::base32Encode ( const std::string &  data,
bool  crlf = true 
)

Performs Base32-encoding of data.

This utility function implements a Base32 encoding (RFC 4648) of the data.

When the crlf argument is true, a CRLF character will be added after each sequence of 76 characters.

See also
base32Decode()

◆ base64Decode()

WT_API std::string Wt::Utils::base64Decode ( const std::string &  data)

Performs Base64-decoding of data.

This utility function implements a Base64 decoding (RFC 2045) of the data. Illegal characters are discarded and skipped.

See also
base64Encode()

◆ base64Encode()

WT_API std::string Wt::Utils::base64Encode ( const std::string &  data,
bool  crlf = true 
)

Performs Base64-encoding of data.

This utility function implements a Base64 encoding (RFC 2045) of the data.

When the crlf argument is true, a CRLF character will be added after each sequence of 76 characters.

See also
base64Decode()

◆ guessImageMimeType()

WT_API std::string Wt::Utils::guessImageMimeType ( const std::string &  file)

Guess the image mime type from an image.

This function opens the image file, reads the first 25 bytes and calls guessImageMimeTypeData() to infer the mime type.

◆ guessImageMimeTypeData()

WT_API std::string Wt::Utils::guessImageMimeTypeData ( const std::vector< unsigned char > &  header)

Guess the image mime type from an image.

This function examines the header of an image and tries to identify the image type.

At the moment, it recognizes and returns as mime type :

  • image/png
  • image/jpeg
  • image/gif
  • image/bmp

The header should contain (at least) the 25 first bytes of the image data.

If no mime-type could be derived, an empty string is returned.

See also
guessImageMimeTypeData()

◆ hexDecode()

WT_API std::string Wt::Utils::hexDecode ( const std::string &  data)

Performs Hex-decoding of data.

Illegal characters are discarded and skipped.

See also
hexEncode()

◆ hexEncode()

WT_API std::string Wt::Utils::hexEncode ( const std::string &  data)

Performs Hex-encoding of data.

A hex-encoding outputs the value of every byte as as two-digit hexadecimal number.

See also
hexDecode()

◆ hmac()

WT_API std::string Wt::Utils::hmac ( const std::string &  text,
const std::string &  key,
std::string(*)(const std::string &)  hashfunction,
size_t  blocksize,
size_t  keysize 
)

Computes a hash-based message authentication code.

This utility function computes a HMAC, and returns the raw (binary) hash value. Takes as arguments the text to be hashed, a secret key, a function pointer to a hashfunction, the internal block size of the hashfunction in bytes and the size of the resulting hash value the function produces. A maximum blocksize of 2048 bits (256 bytes) is supported.

See also
hmac_sha1()
hmac_md5()

◆ hmac_md5()

WT_API std::string Wt::Utils::hmac_md5 ( const std::string &  text,
const std::string &  key 
)

Computes a hash-based message authentication code.

Uses the md5 hashfunction, returns a raw (binary) hash value.

See also
hmac()

◆ hmac_sha1()

WT_API std::string Wt::Utils::hmac_sha1 ( const std::string &  text,
const std::string &  key 
)

Computes a hash-based message authentication code.

Uses the sha1 hashfunction, returns a raw (binary) hash value.

See also
hmac()

◆ htmlAttributeValue()

WT_API std::string Wt::Utils::htmlAttributeValue ( const std::string &  text)

Escape the given text for inclusion in an HTML attribute.

This utility function escapes characters so that the text can be used as the value of an HTML attribute between double quotes.

The double quotes are not included in the output.

Example usage:

std::string attribute = "name=\"" + htmlAttributeValue(value) + "\"";
std::string htmlAttributeValue(const std::string &text)
Escape the given text for inclusion in an HTML attribute.
Definition: Utils.C:191

◆ htmlEncode() [1/2]

WT_API std::string Wt::Utils::htmlEncode ( const std::string &  text,
WFlags< HtmlEncodingFlag flags = None 
)

Performs HTML encoding of text.

This utility function escapes characters so that the text can be embodied verbatim in a HTML text block.

◆ htmlEncode() [2/2]

WT_API WString Wt::Utils::htmlEncode ( const WString text,
WFlags< HtmlEncodingFlag flags = None 
)

Performs HTML encoding of text.

This utility function escapes characters so that the text can be embodied verbatim in a HTML text block.

By default, newlines are ignored. By passing the HtmlEncodingFlag::EncodeNewLines flag, these may be encoded as line breaks (<br>).

◆ md5()

WT_API std::string Wt::Utils::md5 ( const std::string &  data)

Computes an MD5 hash.

This utility function computes an MD5 hash, and returns the raw (binary) hash value.

See also
sha1()

◆ removeScript()

WT_API bool Wt::Utils::removeScript ( WString text)

Remove tags/attributes from text that are not passive.

This removes tags and attributes from XHTML-formatted text that do not simply display something but may trigger scripting, and could have been injected by a malicious user for Cross-Site Scripting (XSS).

This method is used by the library to sanitize XHTML-formatted text set in WText, but it may also be useful outside the library to sanitize user content when directly using JavaScript.

Modifies the text if needed. When the text is not proper XML, returns false.

◆ sha1()

WT_API std::string Wt::Utils::sha1 ( const std::string &  data)

Computes a SHA-1 hash.

This utility function computes a SHA-1 hash, and returns the raw (binary) hash value.

See also
md5()

◆ urlDecode()

WT_API std::string Wt::Utils::urlDecode ( const std::string &  text)

Performs Url decoding.

This utility function percent encodes a text so that it can be embodied verbatim in a URL (e.g. as a fragment).

Note
To url decode a unicode string, the de-facto standard practice is to interpret the string as a UTF-8 encoded string.
See also
WString::fromUTF8(), urlEncode()

◆ urlEncode()

WT_API std::string Wt::Utils::urlEncode ( const std::string &  text)

Performs Url encoding (aka percentage encoding).

This utility function percent encodes a text so that it can be embodied verbatim in a URL (e.g. as a fragment).

Note
To url encode a unicode string, the de-facto standard practice is to encode a UTF-8 encoded string.
See also
WString::toUTF8(), urlDecode()