util.encodings

Provides an interface for translating a string into various common encodings.

Currently provided are: base64, idna, stringprep.

Usage

   local encodings = require "util.encodings";
 
   print(encodings.base64.encode("Hello world!")) -- prints "SGVsbG8gd29ybGQh"
 
   -- or individually --
   local base64 = require "util.encodings".base64;
   local idna = require "util.encodings".idna;
   local stringprep = require "util.encodings".stringprep;

Reference

base64

encode(string)

Returns a base64-encoded equivalent of a string.

decode(string)

Returns a decoded equivalent of a base64 string, or nil if the string is invalid base64.

idna

IDNA provides a way to safely convert unicode domain names to an ASCII equivalent, and back again.

to_ascii(string)

Returns an ASCII-safe version of a unicode domain name, or nil for an invalid domain.

Example:

   print(encodings.idna.to_ascii("➡.ws"))
 
   xn--hgi.ws
 

to_unicode(string)

Returns the original unicode version of an ASCII IDNA-encoded domain.

Example:

   print(encodings.idna.to_unicode("xn--hgi.ws"))
 
   ➡.ws
 

stringprep

Stringprep defines a way of processing and normalizing strings according to a set of rules (a profile). Prosody implements 4 profiles:

  • nodeprep (for usernames, the node part of a JID)
  • resourceprep (for resources and MUC nicknames)
  • saslprep (for applying to strings prior to calculating SASL challenges)
  • nameprep (for applying to hostnames/domains)

Example:

   print(encodings.stringprep.nodeprep("HelloWorld"))
 
  helloworld
 

All stringprep functions return nil when the input is not a valid string in that particular profile.

utf8

UTF-8 is the character encoding used in XMPP. Prosody has a function for validating that a string is a valid UTF-8 encoded string, primarily used internally in util.encodings before passing data to the libidn library.

valid(string)
Returns boolean true if the string passed is a valid UTF-8 encoded string, false otherwise.