How to fully decode a base64 string in PHP? - php

I'm trying to implement the Google Safebrowsing update API v4 in PHP.
But I can't figure how to correctly decode the rawHashes.
(The rawHashes are 4-bytes-truncated sha256 hashes and then concatenated).
I am trying the native base64_decode of PHP but I can't fully decode the string, and I don't know what the next step is.
According to the API documentation here's how the rawhashes are encoded :
string (bytes format)
The hashes, in binary format, concatenated into one long string. Hashes are sorted in lexicographic order. For JSON API users, hashes are base64-encoded.
A base64-encoded string.
I an very simply decoding the string like so:
$decoded = base64_decode($rawHashes);
The base64 encoded string look like this:
"AAAIYAAAC90AABOxAAAjDgAALZIAAEbKAABIHwAA..."
And the base64 decoded string look like this:
b"\x00\x00\x08`\x00\x00\vÝ\x00\x00\x13±\x00\x00#\x0E\x00\x00-’\x00\x00FÊ\x00\x00H\x1F\x00\x00^\x06\x00\x00bF\x00\x00h²"
As you can see something is not right and I must have missed a step but I can't figure which one.

As Mjh said in the discussion nothing is wrong about base64_decode and nothing else is needed.
Nothing's wrong. You just aren't reading carefully. Here, read what it says: The hashes, in binary format. It says binary format. After decoding, you got binary representation of the data. Using bin2hex should return a human-readable hash. $hash = bin2hex(base64_decode($your_encoded_hash)); - Mjh
The decoded string was looking weird as it is binary data (Raw SHA256 hash) although it is totally correct. To get the hashes in a more convenient encoding it's possible to convert the binary represented data to hex represented data with the php function bin2hex
$hash = bin2hex(base64_decode($your_encoded_hash));

From what I know of base64_decode, it just works. Something must be wrong in your $rawHashes string. If you have line breaks in your string, you need to get rid of them by replacing them with an empty string. The hash that base64_decode needs should be one long line of base64 encoded string. It is not uncommon to receive a hash that is broken into multiple lines.
Try this ...
$decoded = base64_decode(str_replace(PHP_EOL, "", $rawHashes));

Related

How to encode url in base64 that no body can decode it

I want to encode my urls in base64 and decode it in my functions and no body can decode it how can i do this.
<?php
$somestring = 'here is my some string';
$url = 'http://google.com/'.$somestring;
base64_encode($url);
?>
Base64 is not meant for encrypting sensible data. It's merely a means of representing data in another way. What it does, it takes binary data and converts it to ASCII, so that binary data can be represented with ASCII characters.
If you want to encrypt strings like your URLs, you have to choose an encryption algorithm like Blowfish, which is available in the PHP bcrypt extension.

String to sha1, then to hexadecimal (length 40)

I want a string to sha1 encoding, then to hex, with a length of 40 characters. This is a Java webservice and the client side is to be done with PHP.
Original code is Java (I don't have the source, only the documentation) and it hashes the following string:
chNFe=43120910585504000174650010000000541123456781&nVersao=100&tpAmb=2&
dhEmi=323031322d30392d32375431363a32303a33342d30333a3030&vNF=1000.00&vICMS=180.00&digVal=37327151612b623074616f514f3966414a7766646c5875715176383d&cIdToken=0000011058550420130001
To the following hex:
3FACB55248244D98C658FC8A826413BCEF10A4AE
The example above is from the webservice documentation and it says string was encoded with sha1, then the result was encoded to hex.
I tried sha1 then dechex and many other ways, but cannot get the same result. Has anyone an idea of what type of encoding PHP have to do to get this hash?
Thank you.
The NFE manual is wrong. The example String has a white space at the end of string..
where appears
5176383d&cIdToken=000001105855042013000 is really
5176383d &cIdToken=000001105855042013000
Convencional functions the cript this using sha1 resolves the problem ;)
in mysql you can do :
sha1(yourExampleString)...
in php could have the something like...

Why do PHP and Obj-C encode strings differently?

I'm trying to convert a string to UTF8, on both obj-c and php.
I get different results:
"\xd7\x91\xd7\x93\xd7\x99\xd7\xa7\xd7\x94" //Obj-C
"\u05d1\u05d3\u05d9\u05e7\u05d4" //PHP
Obj-C code:
const char *cData = [#"בדיקה" cStringUsingEncoding:NSUTF8StringEncoding]
PHP code:
utf8_encode('בדיקה')
This difference breaks my hash algorithm that follows.
How can I make the two strings encoded the same way? Should I change the obj-c\php ?
Go to http://www.utf8-chartable.de/unicode-utf8-table.pl
In the combo box switch to “U+0590 … U+5FF Hebrew”
Scroll down to “U+05D1” which is the rightmost character of your input string.
The third column shows the two UTF-8 bytes: “d7 91”
If you keep looking you will see that the PHP and the Objective-C are actually the same. The “problem” you are seeing is that while PHP uses an Unicode escape (\u), Objective-C uses direct byte hexadecimal escapes (\x). Those are only visual representations of the strings, the bytes in memory are actually the same.
If your hash algorithm deals with bytes correctly, you should not see differences.
What are you using to do the encoding on PHP? It looks like you're generating a UTF-16 string.
Try utf8_encode() and see if that gives better results.

What is Unpack's "binary string"

I'm trying to the unpack function. The PHP documentation says
Unpacks from a binary string into an
array according to the given format.
Does the string passed have to be a binary string? and what exactly is a binary string?
A binary string just means data that is in it's base binary format. The data it's self is being displayed as a string to you if you where to echo it, but it would be meaningless without applying it's correct structure to it. So for example a number would not look like a number, because it's in binary. While the data is there, it has to be parsed as a number for it to be readable as a number otherwise it could look like 'abcd'.
if you read down slightly further you will see examples of what binary strings look like
$binarydata = "\x32\x42\x00\xa0";
Also the description of the method clearly says Unpacks from a binary string, so yes it requires a binary string.
more information on binary

convert openssl_private_encrypt output to hex php

I have an encrypted message from the openssl_private_encrypt methode. But I need it in 32 bit hex. How can I convert it? Can someone tell me witch format it the encrypted message has?
I would run the resulting string through base64_encode(): http://php.net/manual/en/function.base64-encode.php

Categories