ns_base64 - Binary-to-text and text-to-binary encoding
These commands perform a binary-to-text encoding and vice versa. The encoded text string will be approximately 33% longer than the potentially (binary) original. These commands use either the classical alphabet (see e.g. RFC 2045) referred to as "base64" or the newer URL and filename safe alphabet, referred to as "base64url" (see RFC 4648). The latter does not use padding and inserts no linebreaks.
Encode the potentially binary argument to classical base64 encoding. When the option -binary is used, the Tcl byte array value of the provided input argument is used and no UTF-8 interpretation is performed.
Decode the argument containing only characters of the classical base64 alphabet. In case, the result might contain binary content, it is recommend to use the option -binary, which causes the result to be a Tcl byte array.
Encode the potentially binary argument to the URL and filename safe alphabet. No padding with "=" signs is performed, no line breaks are inserted in the text. When the option -binary is used, the Tcl byte array value of the provided input argument is used and no UTF-8 interpretation is performed.
Decode the argument containing only characters of the URL and filename safe alphabet. In case, the result might contain binary content, it is recommend to use the option -binary, which causes the result to be a Tcl byte array.
Classical base64 alphabet:
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
URL and filename safe alphabet:
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
Encoding and decoding with classical base64 alphabet:
% ns_base64encode Lorem?ipsum=1 TG9yZW0/aXBzdW09MQ== % ns_base64decode TG9yZW0/aXBzdW09MQ== Lorem?ipsum=1
Encoding and decoding with URL and filename safe alphabet:
% ns_base64urlencode Lorem?ipsum=1 TG9yZW0_aXBzdW09MQ % ns_base64urldecode TG9yZW0_aXBzdW09MQ Lorem?ipsum=1