doushoubu5360 2013-08-28 17:18
浏览 59

将此函数从php转换为ruby

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) . "
";   
}   
  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?

  • 写回答

1条回答 默认 最新

  • dsa80833 2013-10-01 13:59
    关注

    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)
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮