# 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