Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are the differences between PHP base64_encode and *nix base64

PHP's base64_encode is returning a different string to the linux base64 command. Why is this?

PHP:

$ php <?php echo base64_encode('test'); ?> dGVzdA== 

Linux base64:

$ echo 'test' | base64 dGVzdAo= 
like image 688
psx Avatar asked Jan 11 '12 09:01

psx


People also ask

What is Base64 encoding in PHP?

base64_encode() function can encode the given data with base64. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean such as mail bodies. Base64-encoded data can take about 33% more space than original data.

How decode Base64 in PHP?

The base64_decode() is an inbuilt function in PHP which is used to Decodes data which is encoded in MIME base64. Parameters: This function accepts two parameter as mentioned above and described below: $data: It is mandatory parameter which contains the encoded string. $strict: It is an optional parameter.

What type of encoding is Base64?

Base64 is a binary to text encoding scheme that is generally used to transfer content-based messages over the Internet. It works by dividing every three bits of binary data into six bit units. The newly created data is represented in a 64-radix numeral system and as seven-bit ASCII text.

Are certificates Base64 encoded?

Base64 is the industry standard format for SSL certificate content. The most common web servers will generate a certificate signing requests as well as accept SSL certificates in base-64 format. The size of the certificate content will depend on the encryption strength of the certificate.


1 Answers

echo usually outputs a new line character at the end of the string, to suppress that use the -n switch:

$ echo -n 'test' | base64 dGVzdA== 

Similarly for PHP:

$ php <?php echo base64_encode("test\n"); ?> dGVzdAo= 
like image 121
knittl Avatar answered Oct 05 '22 15:10

knittl