naunga has asked for the wisdom of the Perl Monks concerning the following question:

I've been all over the internet trying to figure out why I get two different results when trying to sign data using Crypt::OpenSSL::RSA and the following command line:

$ printf "$req_data" | openssl rsaautl -sign -inkey ~/.chef/asalvo13.pem | openssl env -base64

I'm trying to write a small interface between a Perl app and the Chef Server REST API. The code below generates the authentication headers to be send. The headers taken from the shell commands, when sent to our Chef server result in a successful request, but the Pure Perl headers do not.

use Crypt::OpenSSL::RSA; use MIME::Base64; use strict; use warnings; my $req_data = qq[Method:GET Hashed Path:1DfW7438O+6WjXaWOV69lrPO5z0= X-Ops-Content-Hash:2jmj7l5rSw0yVb/vlWAYkK/YBwk= X-Ops-Timestamp:2015-08-07T19:32:19Z X-Ops-UserId:asalvo13]; my $keyfile = "$ENV{HOME}/.chef/asalvo13.pem"; my $keystr; open(KEYFILE, "<$keyfile") || die "Cannot open $keyfile: $!\n"; { undef $/; $keystr = <KEYFILE>; close KEYFILE; } my $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($keystr); my $pure_perl = encode_base64($rsa_priv->sign($req_data)); my $shell_cmds = `printf "$req_data" | openssl rsautl -sign -inkey $ke +yfile | openssl enc -base64`; print "=" x 20,"BEGIN PURE PERL","=" x 20,"\n"; print $pure_perl; print "=" x 20,"END PURE PERL","=" x 20,"\n\n"; print "=" x 20,"BEGIN SHELL COMMANDS","=" x 20,"\n"; print $shell_cmds; print "=" x 20,"END SHELL COMMANDS","=" x 20,"\n";

Here's the output:

====================BEGIN PURE PERL==================== Ox4nMjzgrFKSV2GpSH6z80Mbk8zS2gTy0rvN6AyBYM/o9Bw+BbwEa5HuD8v7xlWUkGghE1 +8MT8A4 mkBuTFJlNRimpno1Dj+oifvcpsb1YQ+1uCvyDJAFNjJnlt0S5gDFejHg2Gn8/dp0Lx7dTp +b6QRmk 1+VeKaHMwbkjGlihLNoDftrJlCsAqsQRGM3flXQrdXcOqfSeUVeRrUKzvV6W08i+bTwfvE +ONLqRd kqr3/nem1RuIp7G+BkZezmNdU372iPTf2JPZWtJS9SSTgzM+hzhsY/jEZ48USmjLKUcpHy +55SRhy aH9RpACL5Xfx6nqtK/K5Lk6ZD3x3nVJgVHV29Q== ====================END PURE PERL==================== ====================BEGIN SHELL COMMANDS==================== jjLfh5dprypLNw+5KM2kB0k7ywVBYfL54zLCpgnhmi6xdFqn/CWBwOcOMnOe0Idt CJhm+pkpIucOc/ABbgJ7r5+4r5rp9NvOasNcb9HRX3e8zs4aNQWOXTW8OwGgvw3R PlUAqISW33mwytzcQjzlbQJ5DR/NAzTPBzoD/x/T3a/fuf6CySx0ZtbV0vsq9pBm ekDAdZKS/ssw1OCqowYgdV3jMmcfSlVNnRLb0uY2wVxqhCA6waZ0Qq/DgV5MLRv2 DC9VdpiQJ+tG6L7Ncn8WN0hOY5yIiDpjsXecwNkbkSOoR0pzPe+Z00rsaFMMzFcY TYgeoemNE2uBmFjlmDgF+g== ====================END SHELL COMMANDS====================

Obviously I could simply have my library just make calls out to the shell, but I really would rather not. At the end of it all I'd really like to understand why the difference exists. So any insight would be appreciated.

I'm using..

- Crypt::OpenSSL::RSA version 0.28

- OpenSSL 0.9.8zf 19 Mar 2015

- OS X 10.10.4 (14E46)

- perl 5, version 18, subversion 1 (v5.18.1)

Replies are listed 'Best First'.
Re: Difference between Crypt::OpenSSL::RSA and openssl command
by Anonymous Monk on Aug 10, 2015 at 22:26 UTC

      Where would I use binmode? I'm not reading any binary files.