Reputation: 39
I have this function and I tried to convert it to ruby 1.9.3 (to be used on ruby on rails 3.2.13) but I can't get the same hash that this php function returns.
this what I am using in ruby so far:
keypair = OpenSSL::PKey::RSA.new(File.open(Rails.root.join('sec', 'private_key.pem')))
@encrypted_string = keypair.sign(OpenSSL::Digest::MD5.new, @data)
@encrypted_string = Base64.encode64(@encrypted_string)
This is the function in php:
function SignData($text, $privateKeyFile) {
$private_cert = $privateKeyFile;
$f = fopen($private_cert,"r");
if($f)
$private_key = fread( $f, filesize($private_cert) );
else
return "";
fclose($f);
$private_key = openssl_get_privatekey($private_key);
if(openssl_private_encrypt(md5($text), $crypt_text, $private_key))
{
base64_url_encode($crypt_text) . "\n";
}
return "";
}
This is the hash returned in php:
xJCl3YZVEkXjt_pTPHl9FjpebpDcdMtgZzGFo0LsO_PFyQ8lwdUpKxR_XhK1DGfywVr4-hPxtDqSOHMcp7fM-eYK5GqGVasUh80qRiVLjw6Zeh4NPCk1qxsSm4X3gl0sv13dBb5FvDwV6QcLyo7vyNweqUH_Cpq_WmWrNY3px5Y,
This is the one I got in ruby:
DTilW98pHOep/5qc7H+iBYPdbFNZGKWW0c0XFo5YfrWfqKLPzzLTygRQAiFY whVX8+I0FYAOg3+QqyH0jpcGaFbSVQefU7gDIfT+tHKqSCKmLZwVQas5SQ3j o+m8V+iv/ZgGuHD0U8dNZwO4zkqJAPMPJFvdOeHJVxb77lCBtNU=
Maybe I need to remove the header and footer in the .pem file?
Upvotes: 3
Views: 497
Reputation: 1594
For the convinience:
This has done it for the OP:
private_key_file = File.open(Rails.root.join('sec', 'private_key.pem'))
private_key = OpenSSL::PKey::RSA.new(File.read(private_key_file))
digest = Digest::MD5.hexdigest(@data)
@encrypted_string = private_key.private_encrypt(digest)
@encrypted_string = Base64.encode64(@encrypted_string)
Upvotes: 1