The Base64
class provides functions for encoding and decoding data
to and from base64 encoding.
Description: Decodes base64 encoded data
This static method decodes supplied base64 encoded data. The data has
to be wrapped into the lines of 80 characters followed by a new line
character. This format is typical for encoding crypto keys (e.g. PEM).
If there is a line with more than 80 characters or data are incorrectly
encoded, then Base64Exception
is thrown.
data : string
- base64 encoded data for decoding
It can throw Base64Exception
with code
Base64Exception::DECODE_UPDATE_FAILED
- if the data are incorrectly encoded or wrapped.Base64Exception::INPUT_DATA_LENGTH_HIGH
- if the data length exceeds CINT_MAX
string
: Decoded data.
try {
$data = \Crypto\Base64::decode($base64_data);
} catch (\Crypto\Base64Exception $e) {
echo $e->getMessage();
}
Description: Encodes data to base64 encoding
This static method encodes supplied data using base64 encoding. The data is written in lines of 80 characters. This format is typical for encoding crypto keys (e.g. PEM).
data : string
- data to encode
It can throw Base64Exception
with code
Base64Exception::INPUT_DATA_LENGTH_HIGH
- if the data length exceeds CINT_MAX
string
: Base64 encoded data.
$base64_data = \Crypto\Base64::encode($data);
Description: Creates a new Base64 object
The constructor initializes Base64
context for encoding or decoding.
The constructor does not have any parameters.
The constructor does not throw any exception.
Base64
: New instances of the Base64
class.
$base64 = new \Crypto\Base64();
Description: Finishes the base64 decoding
This method finishes base64 decoding and returns resulted data if they are buffered (mostly it returns just empty string)
This method does not have any parameters.
It can throw Base64Exception
with code
Base64Exception::ENCODE_FINISH_FORBIDDEN
- if the context has not been updated usingBase64::decodeUpdate
string
: Decoded string if there is something in the buffer, otherwise
an empty string.
$base64 = new \Crypto\Base64();
$decoded_data = $base64->decodeUpdate($base64_data);
$decoded_data .= $base64->decodeFinish();
Description: Updates the base64 decoding context
This method updates base64 decoding context with suppplied data. It also returns the decoded data.
The data has to be wrapped into the lines of 80 characters followed by
a new line character. This format is typical for encoding crypto keys
(e.g. PEM). If there is a line with more than 80 characters or data
are incorrectly encoded, then Base64Exception
is thrown.
data : string
- base64 encoded data for decoding
It can throw Base64Exception
with code
Base64Exception::DECODE_UPDATE_FAILED
- if the data are incorrectly encoded or wrapped.Base64Exception::INPUT_DATA_LENGTH_HIGH
- if the data length exceeds CINT_MAX
string
: Decoded data.
try {
$base64 = new \Crypto\Base64();
$data = '';
while (($base64_data = read_base64_encoded_data()) !== null) {
$data .= $base64->decodeUpdate($base64_data);
}
$data .= $base64->decodeFinish();
} catch (\Crypto\Base64Exception $e) {
echo $e->getMessage();
}
Description: Finishes the base64 encoding
This method finishes base64 encoding and returns base64 encoded data if they are any buffered or empty string otherwise.
This method does not have any parameters.
This method doesn't throw any exception.
string
: Encoded string if there is something in the buffer, otherwise
an empty string.
$base64 = new \Crypto\Base64();
$base64_data = $base64->encodeUpdate($data);
$base64_data .= $base64->decodeFinish();
Description: Updates the base64 encoding context
This method updates base64 encoding context with suppplied data. It also returns the encoded data.
The data is written in lines of 80 characters. This format is typical for encoding crypto keys (e.g. PEM).
data : string
- data to encode
It can throw Base64Exception
with code
Base64Exception::INPUT_DATA_LENGTH_HIGH
- if the data length exceeds CINT_MAX
string
: Base64 encoded data.
$base64 = new \Crypto\Base64();
$base64_data = '';
while (($data = read_data_for_encoding()) !== null) {
$base64_data .= $base64->encodeUpdate($data);
}
$base64_data .= $base64->encodeFinish();