Convert openssl command to php

88

i have line of command

echo -n 2bRePass1234 | openssl dgst -sha1 -binary | openssl enc -base64

output : 3O3bhZbGKheK+g/M4pp18AwGqq8= (28 char)

i need to convert it php

what i tried. openssl_encrypt need $iv and $key. i just use random_bytes because dont know whats is the default value like the command above

return base64_encode(
           openssl_encrypt(
                sha1('testPass1234'),
                'AES-128-CBC',
                random_bytes(16),
                OPENSSL_RAW_DATA,
                random_bytes(16)
            )
);

But its doest return same result as the command did.. is there any way just use default key and cipher to generate it?

Note: i put random_bytes(16) because i dont know what to fill it based on the command.

291

Answer

Solution:

Two things ...

  1. You are doing adgst (digest) in your first call ofopenssl therefore you cannot callopenssl_encrypt in your php.
  2. By doing a digest you does not need random bytes.

There are at least 2 ways to do it in php

1) Using sha1 function

php -r "var_dump(base64_encode(pack('H*', sha1('testPass1234'))));"
string(28) "wW+fqYWyUoXq+c76j0j2mnD5Oyc="

2) Usingopenssl_digest function

php -r "var_dump(base64_encode(pack('H*', openssl_digest('testPass1234', 'SHA1'))));"
string(28) "wW+fqYWyUoXq+c76j0j2mnD5Oyc="

They will both return the same. The string returned fromsha1 will be packed into a string of real bits and not treated as string.

PS: I saw you passed a different string in the command line sample and the php sample, but whenever passing the same string the results will be the same.

echo -n testPass1234 | openssl dgst -sha1 -binary | openssl enc -base64
wW+fqYWyUoXq+c76j0j2mnD5Oyc=

Hope it's what you whore looking for. Cheers!

People are also looking for solutions to the problem: php - Elasticsearch combine two different queries types in one request

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.