Class: UmfRSACryptUtil
- Inherits:
-
Object
- Object
- UmfRSACryptUtil
- Defined in:
- lib/UmfRSACryptUtil.rb
Instance Method Summary collapse
-
#encrypt(data) ⇒ Object
使用公钥对明文字符串进行加密 data 明文字符串.
- #mobileGenerateSign(plain, merId, signType) ⇒ Object
-
#sign(plain, merId, signType) ⇒ Object
rsa私钥签名.
-
#verify(plain, sign, algorithmType) ⇒ Object
联动返回签名的验签.
Instance Method Details
#encrypt(data) ⇒ Object
使用公钥对明文字符串进行加密 data 明文字符串
18 19 20 21 22 |
# File 'lib/UmfRSACryptUtil.rb', line 18 def encrypt(data) public_key = OpenSSL::PKey::RSA.new(UMF_PUBLIC_KEY) encryptData = Base64.encode64(public_key.public_encrypt(data)).to_s.gsub(/\n/, '') return encryptData end |
#mobileGenerateSign(plain, merId, signType) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/UmfRSACryptUtil.rb', line 62 def mobileGenerateSign(plain,merId,signType) priv_key_file = $privateKeyPath[merId] if !(File.exist?(priv_key_file)) UmfLogger.logInfoMsg("[UMF SDK]传入的私钥文件不存在,私钥文件绝对路径 " + priv_key_file) return end pkey = OpenSSL::PKey::RSA.new(File.read(priv_key_file)) signature = "" if signType == "sha1" signature = pkey.sign(OpenSSL::Digest::SHA1.new, plain).unpack("H*")[0].to_s.upcase elsif signType == "sha256" end UmfLogger.logInfoMsg("[UMF SDK]签名后密文 " + signature) return signature end |
#sign(plain, merId, signType) ⇒ Object
rsa私钥签名
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/UmfRSACryptUtil.rb', line 26 def sign(plain, merId, signType) priv_key_file = $privateKeyPath[merId] if !(File.exist?(priv_key_file)) UmfLogger.logInfoMsg("[UMF SDK]传入的私钥文件不存在,私钥文件绝对路径 " + priv_key_file) exit end pkey = OpenSSL::PKey::RSA.new(File.read(priv_key_file)) signature = "" if signType == "sha1" signature = Base64.strict_encode64(pkey.sign(OpenSSL::Digest::SHA1.new, plain)) elsif signType == "sha256" signature = Base64.strict_encode64(pkey.sign(OpenSSL::Digest::SHA256.new, plain)) end UmfLogger.logInfoMsg("[UMF SDK]签名后密文 " + signature) return signature end |
#verify(plain, sign, algorithmType) ⇒ Object
联动返回签名的验签
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/UmfRSACryptUtil.rb', line 45 def verify(plain,sign,algorithmType) public_key = OpenSSL::PKey::RSA.new(UMF_PUBLIC_KEY) if algorithmType == "sha1" verifyRet = public_key.verify(OpenSSL::Digest::SHA1.new, Base64.decode64(sign), plain) elsif algorithmType == "sha256" verifyRet = public_key.verify(OpenSSL::Digest::SHA256.new, Base64.decode64(sign), plain) end if verifyRet == true # puts "[UMF SDK] 平台响应数据验签成功" return true elsif verifyRet == false # puts "[UMF SDK] 平台响应数据验签失败" return false end end |