RSA plus sign and check method in Ruby

Source: Internet
Author: User
Tags sha1

# Coding:utf-8
Require ' OpenSSL '
Require ' base64 '
# RSA signature, text content and private key path
def rsa_sign (Data,private_key_path)
Private_key = File.read (Private_key_path)
pri = OpenSSL::P key::rsa.new (Private_key)
Sign = pri.sign (' SHA1 ', data.force_encoding ("Utf-8"))
# signature = Cgi.escape (base64.encode64 (sign))
Signature = Base64.encode64 (sign)
Signature = signature.gsub! ("\ n", ")
return signature
End

# RSA verification, text content and signature content, public key path
def rsa_verify (Data,sign,public_key_path)
Public_key = File.read (Public_key_path)
Pub = OpenSSL::P key::rsa.new Public_key
Digester = OpenSSL::D igest::sha1.new
Sign = BASE64.DECODE64 (sign)
Return pub.verify (digester, sign, data)
End

# RSA Signature Verification test
def test_rsa data = "123"
Puts Rsa_sign (data, ' RSA_PRIVATE_KEY.PEM ')
Sign= "krxgyocurl3wl6w96dkr906le1rerlqs2t5zalfialr6re78qcqlihin+ ixdyqk6okc8sqlwugdarsll0n1wfgm36owzfhb2dg4mpfake3oq88jsrxtr0uaqrr/ M13qe5qxpn30gcsrnlaqsluer8ts8cfi9ewtktqqz6twagio= "
Puts Rsa_verify (data,sign, ' Rsa_public_key.pem ')
End

RSA plus sign and check method in Ruby

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.