SSL [SOLVED]: Translating SSL command to PHP

SSL [SOLVED]: Translating SSL command to PHP

Home Forums SSL SSL [SOLVED]: Translating SSL command to PHP

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #36776



    I have an SSL command which signs a string with a given key:
    openssl dgst -sha256 -hex -sign key.pem data.txt. I’d like to know how I can translate it to a PHP function. The equivalent in PHP should be something like this:
    openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256). However, the unix command outputs the signature in hex, while the I don’t really get what the PHP command outputs.



    Accepted AnswerAnswer

    This will produce the same hex representation as the openssl command:

    $signature = "";
    $data = file_get_contents("data.txt");
    $key = file_get_contents("key.pem");
    openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256);
    echo bin2hex($signature);

    Essentially, the php version produces the binary bytes, so in my code above, I just use the bin2hex function to output it in the same format you are used to in the openssl command.

    Author: Nick
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.