PHP
<?php
$string = base64_encode(sha1( 'ABCD' , true ) );
echo sha1('ABCD');
echo $string;
?>
Output:
fb2f85c88567f3c8ce9b799c7c54642d0c7b41f6
+y+FyIVn88jOm3mcfFRkLQx7QfY=
Python
import base64
import hashlib
s = hashlib.sha1()
s.update('ABCD')
myhash = s.hexdigest()
print myhash
print base64.encodestring(myhash)
Output:
'fb2f85c88567f3c8ce9b799c7c54642d0c7b41f6'
ZmIyZjg1Yzg4NTY3ZjNjOGNlOWI3OTljN2M1NDY0MmQwYzdiNDFmNg==
Both the PHP and Python SHA1 works fine, but the base64.encodestring() in python returns a different value compared to base64_encode() in PHP.
What is the equivalent method for PHP base64_encode in Python?
base64.b64encode(s.digest()) have correct response
use sha1.digest() instead of sha1.hexdigest()
s = hashlib.sha1()
s.update('ABCD')
print base64.encodestring(s.digest())
The base64.encodestring expects to the string while you give it the hex representation of it.
You're encoding different sha1 results in PHP and Python.
In PHP:
// The second argument (true) to sha1 will make it return the raw output
// which means that you're encoding the raw output.
$string = base64_encode(sha1( 'ABCD' , true ) );
// Here you print the non-raw output
echo sha1('ABCD');
In Python:
s = hashlib.sha1()
s.update('ABCD')
// Here you're converting the raw output to hex
myhash = s.hexdigest()
print myhash
// Here you're actually encoding the hex version instead of the raw
// (which was the one you encoded in PHP)
print base64.encodestring(myhash)
If you base64 encode the raw and non-raw output, you will get different results.
It doesn't matter which you encode, as long as you're consistent.
Related
I have a slice of php encoded code but I don't know how it encoded i mean by which why they encoded this code when i use unphp.net successfully it decode the code and get the real code
simple of encoded code define("\x53\x54\117\103\113\x5f\103\x48\105\x43\113", false) my question is how i can encode another code to be like this and it by which why is encoded please anyone have a knowledge about tell me or know any tool to suggest
I would need more information for me to be able to discern what obfuscator was actually used to encode this, but for this specific string, it appears to be a mix of hex- and octal- encoded letters
"\x53\x54\117\103\113\x5f\103\x48\105\x43\113"
\xFF - Hex
\000 - Octal
The sequence above is encoded as follows (where x is hex and 8 is octal): xx888x8x8x8
This sequence may be random or intentional. Assuming it's random, the following function may be used to encode any string in like manner using random_int() for selecting encoding method at random.
<?php
function hex_octal_rand_encode($s) {
$result = '';
foreach (str_split($s) as $c) {
$d = ord($c);
// select encoding method at random (0 = hex, 1 = oct)
$result .= (random_int(0, 1) === 0)
? '\\x' . dechex($d)
: '\\' . decoct($d);
}
return $result;
}
// Test
$input = "STOCK_CHECK";
$encoded = hex_octal_rand_encode($input);
echo sprintf("Encoded: %s\n", $encoded);
$cmd = sprintf('return "%s";', $encoded);
echo sprintf("Decoded: %s\n", eval($cmd));
?>
Sample outputs (notice the encoded value changes):
Encoded: \x53\x54\117\103\x4b\137\x43\x48\x45\x43\x4b
Decoded: STOCK_CHECK
Encoded: \x53\124\x4f\x43\113\x5f\x43\110\x45\x43\x4b
Decoded: STOCK_CHECK
Encoded: \123\124\x4f\x43\113\137\x43\x48\105\x43\113
Decoded: STOCK_CHECK
How to multilevel base64 decoder and show proper php code....
i.e.....
base64_decode(
base64_decode(
'SkhSb2FYTXRQazh3TUU4d01FOVBUMDh3VDA4Z1BTQm1ZV3h6WlRzZ0NRa2tUekF3VDA4d1R6QXdUMDh3VHlBOUlITjBjblJ2Ykc5M1pYSW9jSEpsWjE5eVpYQnNZV05sS0dKaGMyVTJORjlrWldOdlpHVW9KMGw1T0hWTGFWRnFKeWtzSUNjbkxDQWtYMU5GVWxaRlVsdGlZWE5sTmpSZlpHVmpiMlJsS0NkVk1GWlRWbXRXVTFneFFsTlVNVkpRVVRBNVRTY3BYU2twT3c9PQ=='
)
);
Any buddy have php script to decode multi level base64_decode() and show proper php code.
Note:
Encoded string will be decoded to exact string if the string encoded or decoded equal no. of times.
It means two level encoded string can be decoded 2 level.
<?php
$string="Hello Stack Overflow";
echo $encodedString=base64_encode(base64_encode($string));//Multilevel(2) encode
echo base64_decode(base64_decode($encodedString));//Multilevel(2) decode
I would recommend to create a function similar to this:
function multiBase64Decode($string, int $iteration = 1){
for($i=0;$i<$iteration;$i++){
$string = base64_decode($string);
}
return $string;
}
Here is the base64 encoded smilies generated form my android phone and saved into MySQL when I try to display it in browser first I need to check if the string is base64 encoded then I decoded and print it, but below code fails to
$mystring = "8J+YgPCfmInwn5iZ8J+YkPCflIfwn5Oi8J+OtfCfjqfwn5C18J+QiPCfpoTwn5CX8J+MjfCfl7vw\nn4+f8J+PofCfj4Himb/wn5uC8J+atA==\n";
if (base64_encode(base64_decode($mystring)) == $mystring) {
echo base64_decode($mystring);
} else {
echo $mystring;
}
but if I directly decode same string and print it works
echo base64_decode($mystring);
// output ππππππ’π΅π§π΅ππ¦πππ»ππ‘πβΏππ΄
however same code worked for iOS phone generated smiles.
$mystring = "8J+YgvCfkoHwn5iK8J+RqeKAjfCfkanigI3wn5Gn8J+ZiPCfpKPwn5mJ8J+QvPCfkLbwn5CT8J+MmeKaoe+4j/CfjY7wn42U8J+NlfCfpYPimr3vuI8=";
if(base64_encode(base64_decode($mystring)) == $mystring) {
echo base64_decode($mystring);
} else {
echo $mystring;
}
//output ππππ©βπ©βπ§ππ€£ππΌπΆππβ‘οΈππππ₯β½οΈ
I don't understand what is the problem with this block:
if(base64_encode(base64_decode($mystring)) == $mystring)
if(base64_encode(base64_decode($mystring)) == $mystring)
You cannot compare the content of two strings in that way.
Use strcmp() function.
Moreover you cannot check in this way if $mystring is base64 encoded.
You will always get true as encoding a string and then decoding the result will always deliver the original string.
Comparing wil only tell you if encoding/decoding works.
I have the hex representation and I'm trying to convert this to the type of hex. For example if you execute:
echo "\xFF\xAA";
echo '<br>';
$first = "FF";
$second = "AA";
echo "\x$first\x$second";
die();
The result you get is:
So the first line is weird symbols. This indicates to me this is actually of type hex. Where the second result although is valid representations of hex, they are in fact not actually hex.
So my question is how can I make the actual result hex? I want to be able to use a variable as the representation but actually convert it to hex. (I want line two to show up as symbols)
hex2bin β Decodes a hexadecimally encoded binary string
<?php
$hex = hex2bin("6578616d706c65206865782064617461");
echo $hex;
?>
Output:
example hex data
Source: http://php.net/manual/en/function.hex2bin.php
So just use
echo hex2bin("$first$second");
Can anyone help me with converting the following Python script to PHP?
data = json.dumps({'text': 'Hello world'})
content_sha256 = base64.b64encode(SHA256.new(data).digest())
The value of content_sha256 should be
oWVxV3hhr8+LfVEYkv57XxW2R1wdhLsrfu3REAzmS7k=
I have tried to use the base64_encode function, and will only get the desired result if I use the string:
$data_string = "{\"text\": \"Hello world\"}";
base64_encode(hash("sha256", $data_string, true));
but I want to get the desired result by using and array, not a quote-escaped string...
You need to replace python json.dumps with php json_encode
$data_string = json_encode(array('text' => 'Hello world'));
base64_encode(hash("sha256", $data_string, true));
Both of these functions take an associative array and transform it into a string representation. It is then the string that you do a hash/base64 encoding on.
Paul Crovella, you pointed out the correct direction.
I have to do a string replace on the json encoded variable before I send it through the base64, to get the same string as the one made by Python:
$data_array = array("text" => "Hello world");
$data_string_json_encoded = json_encode($data_array);
$data_string_json_encoded_with_space_after_colon = str_replace(":", ": ", $data_string_json_encoded);
$data_string_base64 = base64_encode(hash("sha256", $data_string_json_encoded_with_space_after_colon , true));
Then I get the desired result, the same as in the Python script:
oWVxV3hhr8+LfVEYkv57XxW2R1wdhLsrfu3REAzmS7k=