Class: Rsa::Tools::Utility

Inherits:
Object
  • Object
show all
Defined in:
lib/rsa/tools/utility.rb

Class Method Summary collapse

Class Method Details

.decrypt(public_key, encrypted) ⇒ Object



24
25
26
27
# File 'lib/rsa/tools/utility.rb', line 24

def self.decrypt(public_key, encrypted)
  key = OpenSSL::PKey::RSA.new(public_key)
  return key.public_decrypt(Base64.decode64(encrypted))
end

.encrypt(private_key, data) ⇒ Object



19
20
21
22
# File 'lib/rsa/tools/utility.rb', line 19

def self.encrypt(private_key, data)
  key = OpenSSL::PKey::RSA.new(private_key)
  return Base64.strict_encode64(key.private_encrypt(data))
end

.pri_decrypt(private_key, encrypted) ⇒ Object



35
36
37
38
# File 'lib/rsa/tools/utility.rb', line 35

def self.pri_decrypt(private_key, encrypted)
  key = OpenSSL::PKey::RSA.new(private_key)
  return key.private_decrypt(Base64.decode64(encrypted))
end

.pub_encrypt(public_key, data) ⇒ Object



30
31
32
33
# File 'lib/rsa/tools/utility.rb', line 30

def self.pub_encrypt(public_key, data)
  key = OpenSSL::PKey::RSA.new(public_key)
  return Base64.strict_encode64(key.public_encrypt(data))
end

.sign(private_key, data) ⇒ Object

三方要求我们私钥加签的时候调用这方法签名



13
14
15
16
17
# File 'lib/rsa/tools/utility.rb', line 13

def self.sign(private_key, data)
  key = OpenSSL::PKey::RSA.new(private_key)
  sign = key.sign(OpenSSL::Digest::SHA256.new, Digest::MD5.hexdigest(data.force_encoding("utf-8")))
  Base64.strict_encode64(sign)
end

.verify(public_key, data, original_data) ⇒ Object

我们是服务端的时候验证别人的请求的时候用这个方法



7
8
9
10
# File 'lib/rsa/tools/utility.rb', line 7

def self.verify(public_key, data, original_data)
  key = OpenSSL::PKey::RSA.new(public_key)
  key.verify(OpenSSL::Digest::SHA256.new, Base64.decode64(data), Digest::MD5.hexdigest(original_data.force_encoding("utf-8")))
end