1MIME::Base64(3)       User Contributed Perl Documentation      MIME::Base64(3)
2
3
4

NAME

6       MIME::Base64 - Encoding and decoding of base64 strings
7

SYNOPSIS

9        use MIME::Base64;
10
11        $encoded = encode_base64('Aladdin:open sesame');
12        $decoded = decode_base64($encoded);
13

DESCRIPTION

15       This module provides functions to encode and decode strings into and
16       from the base64 encoding specified in RFC 2045 - MIME (Multipurpose
17       Internet Mail Extensions). The base64 encoding is designed to represent
18       arbitrary sequences of octets in a form that need not be humanly
19       readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
20       enabling 6 bits to be represented per printable character.
21
22       The following primary functions are provided:
23
24       encode_base64( $bytes )
25       encode_base64( $bytes, $eol );
26           Encode data by calling the encode_base64() function.  The first
27           argument is the byte string to encode.  The second argument is the
28           line-ending sequence to use.  It is optional and defaults to "\n".
29           The returned encoded string is broken into lines of no more than 76
30           characters each and it will end with $eol unless it is empty.  Pass
31           an empty string as second argument if you do not want the encoded
32           string to be broken into lines.
33
34           The function will croak with "Wide character in subroutine entry"
35           if $bytes contains characters with code above 255.  The base64
36           encoding is only defined for single-byte characters.  Use the
37           Encode module to select the byte encoding you want.
38
39       decode_base64( $str )
40           Decode a base64 string by calling the decode_base64() function.
41           This function takes a single argument which is the string to decode
42           and returns the decoded data.
43
44           Any character not part of the 65-character base64 subset is
45           silently ignored.  Characters occurring after a '=' padding
46           character are never decoded.
47
48       If you prefer not to import these routines into your namespace, you can
49       call them as:
50
51           use MIME::Base64 ();
52           $encoded = MIME::Base64::encode($decoded);
53           $decoded = MIME::Base64::decode($encoded);
54
55       Additional functions not exported by default:
56
57       encode_base64url( $bytes )
58       decode_base64url( $str )
59           Encode and decode according to the base64 scheme for "URL
60           applications" [1].  This is a variant of the base64 encoding which
61           does not use padding, does not break the string into multiple lines
62           and use the characters "-" and "_" instead of "+" and "/" to avoid
63           using reserved URL characters.
64
65       encoded_base64_length( $bytes )
66       encoded_base64_length( $bytes, $eol )
67           Returns the length that the encoded string would have without
68           actually encoding it.  This will return the same value as
69           "length(encode_base64($bytes))", but should be more efficient.
70
71       decoded_base64_length( $str )
72           Returns the length that the decoded string would have without
73           actually decoding it.  This will return the same value as
74           "length(decode_base64($str))", but should be more efficient.
75

EXAMPLES

77       If you want to encode a large file, you should encode it in chunks that
78       are a multiple of 57 bytes.  This ensures that the base64 lines line up
79       and that you do not end up with padding in the middle. 57 bytes of data
80       fills one complete base64 line (76 == 57*4/3):
81
82          use MIME::Base64 qw(encode_base64);
83
84          open(FILE, "/var/log/wtmp") or die "$!";
85          while (read(FILE, $buf, 60*57)) {
86              print encode_base64($buf);
87          }
88
89       or if you know you have enough memory
90
91          use MIME::Base64 qw(encode_base64);
92          local($/) = undef;  # slurp
93          print encode_base64(<STDIN>);
94
95       The same approach as a command line:
96
97          perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file
98
99       Decoding does not need slurp mode if every line contains a multiple of
100       four base64 chars:
101
102          perl -MMIME::Base64 -ne 'print decode_base64($_)' <file
103
104       Perl v5.8 and better allow extended Unicode characters in strings.
105       Such strings cannot be encoded directly, as the base64 encoding is only
106       defined for single-byte characters.  The solution is to use the Encode
107       module to select the byte encoding you want.  For example:
108
109           use MIME::Base64 qw(encode_base64);
110           use Encode qw(encode);
111
112           $encoded = encode_base64(encode("UTF-8", "\x{FFFF}\n"));
113           print $encoded;
114
116       Copyright 1995-1999, 2001-2004, 2010 Gisle Aas.
117
118       This library is free software; you can redistribute it and/or modify it
119       under the same terms as Perl itself.
120
121       Distantly based on LWP::Base64 written by Martijn Koster
122       <m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and
123       code posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans
124       Mulder <hansm@wsinti07.win.tue.nl>
125
126       The XS implementation uses code from metamail.  Copyright 1991 Bell
127       Communications Research, Inc. (Bellcore)
128

SEE ALSO

130       MIME::QuotedPrint
131
132       [1] <http://en.wikipedia.org/wiki/Base64#URL_applications>
133
134
135
136perl v5.38.0                      2023-07-20                   MIME::Base64(3)
Impressum