0

I have a .sh file generating an openssl sha256 key

$(echo -n ${DATA} | openssl sha256 -binary | base64)

And need to generate same key to compare it to using sha256 in my php file, the old one is sha1. This is my php code right now, which outputs fine sha1 string.

$params['Code'] = base64_encode(sha1($params['Product'] . $params['Model'] . $params['Number'],true));

I changed sha1 to sha256, and I get internal server error.

keneso
  • 301
  • 1
  • 2
  • 10
  • 1
    What do you mean by _"need to generate same key"_? sha1 and sha256 will generate different hashes. Also, there isn't any `sha256()` function. Use [`hash('sha256', $theValueToHash)`](https://www.php.net/manual/en/function.hash.php). – M. Eriksson Apr 29 '19 at 15:51
  • Sorry for the confusion I mean same key that the .sh file generates. Right now the sha1 is different than that of the .sh file. – keneso Apr 29 '19 at 15:53
  • Then change your `sha1()`-function to the suggested function in the first comment and see if that works better. – M. Eriksson Apr 29 '19 at 15:54
  • Thank you, that is my question, I don't know how, like said I just changed sha1 with sha256, and apparently is not correct. Thus asking for help. – keneso Apr 29 '19 at 15:57
  • For the future, you need to do some proper research before posting. If you googled something like: _"php sha256"_, you would have found the answer faster than it took you to write the question. – M. Eriksson Apr 29 '19 at 16:01
  • Believe it or not I did for longer, but perhaps I am not good to understand, not everyone is as smart as you, as an example I had found this https://hotexamples.com/examples/-/-/sha256/php-sha256-function-examples.html – keneso Apr 29 '19 at 16:04

1 Answers1

1

It seems you want to use this:

$data = $params['Product'] . $params['Model'] . $params['Number'];
$params['Code'] = base64_encode(hash('sha256', $data, true));

but I also fail to understand why this is difficult to arrive at, given the first comment by Magnus Eriksson.

Note that we have no way to verify that this will result in the same output, as you expect.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • Thanks. I'll try it. I had already read the php manual ref. indicated by Magnus Eriksson prior of asking here, but failed. My apologies to him. – keneso Apr 29 '19 at 16:34
  • 1
    That's no problem. The devil is always in the details. Just in case my code doesn't work for you, and you want to report that, don't just say: "it failed", be very specific, like: "I gave it this string (copy of string) and expected this string (again copy it) as a result, for these reasons (name them), but I got this (same here).". The better we are able to understand your specific problem, the better we can help you. – KIKO Software Apr 29 '19 at 16:39
  • It seems to output the expected lenght, although the value doesn't match the .sh one, but that is not the question of this thread, I will open a new one to understand what I am missing. – keneso Apr 30 '19 at 08:05