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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题