rsa-tools 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65968de63d290da2bd846edecf503aa38d5e6376b24febd522b243af6ef9d0aa
4
- data.tar.gz: 89f43d09a673470842ef3adc722d79f7b4681a7097b1dd0a10c0a9157fb1c066
3
+ metadata.gz: 87c32d05e60ad507b21efc98df0e21d84bec0f03a305f8843a32b60a1ebdddd0
4
+ data.tar.gz: 0e991bc21db0d654d622c509a6c6a5ee39d001187e793bdfed8c1535bb006a26
5
5
  SHA512:
6
- metadata.gz: 5d448f1b8f20eca0ca7df34deec0c2ccf65eeb39d864d5000b8daa12ec724fc4a293c96006be656ace8367f44b3eaa0e5a9fb6251689ea5149e33311c4efaed5
7
- data.tar.gz: 6ef7a1ba0433997b194006c250cea82d346598a73ae07387ad685d9e21088a6284f33202ea3cc04b68d6592694232593cc7ab1ec4583632d67277eedb2cdc276
6
+ metadata.gz: '092306579ed871ef15eea6a2be9e0c467656668f769941b915e71503e25a094588cb6031138081d6b8f12eb638a79ed7a931ea06070c8a0fd957134b063ffbb8'
7
+ data.tar.gz: 8d19aca66995578c280ba8bca24f1cf497e961eaf2a9f74f7e02a2a6f0da3a2aa859b8c9c9c37cf4d452d418a5888e2b7b1b04d71720c20b5f1d6727f4d45a24
data/.gitignore CHANGED
@@ -6,6 +6,10 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /keys/public_key.pem
10
+ /keys/private_key.pem
11
+
12
+ *.gem
9
13
 
10
14
  # rspec failure tracking
11
15
  .rspec_status
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Rsa::Tools
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rsa/tools`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ This Gem offered 8 frequently uesed methods for Sign & Verify & Encrypt & Decrypt with RSA.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,57 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ I offered 3 pairs of methods and 2 generator method in this tool
24
+
25
+ #### 2 Generator method
26
+
27
+ * `Rsa::Tools#key_pairs`
28
+
29
+ ```ruby
30
+ # This method will return an array of text, the first text string is private_key and the last text is public_key
31
+
32
+ Rsa::Tools.key_pairs
33
+ ```
34
+
35
+ * `Rsa::Tools#pem_pairs(pub_path = nil, pri_path = nil)`
36
+
37
+ ```ruby
38
+ # This method intend to offer you pems of key pairs, the first param is the path you want to store your private key
39
+ # And the default path is keys/private_key.pem & keys/public_key.pem
40
+ # Use the default path is my advice
41
+
42
+ Rsa::Tools.pem_pairs
43
+ ```
44
+
45
+ #### 3 pairs of methods
46
+
47
+ * `Sign & Verify`
48
+
49
+ ```ruby
50
+ # This pair of method offer the ablity to verify the source of data
51
+ # The returned signature were encoded by Base64
52
+
53
+ Rsa::Tools.sign(private_key, data) # this returns the signature
54
+ Rsa::Tools.verify(public_key, data, original_data) # this returns true or false
55
+ ```
56
+
57
+ * `Encrypt & Decrypt`
58
+
59
+ ```ruby
60
+ # I advice you use this pair while your user do not need have his own private_key (ToC)
61
+
62
+ Rsa::Tools.encrypt(private_key, data) # this returns the encrypted string
63
+ Rsa::Tools.decrypt(public_key, encrypted) # this returns the original data
64
+ ```
65
+
66
+ * `PubEncrypt & PriDecrypt`
67
+
68
+ ```ruby
69
+ # I advice you use this pair while your user should offer his public key to you (Which means ToB)
70
+
71
+ Rsa::Tools.pub_encrypt(public_key, data) # this returns the encrypted string
72
+ Rsa::Tools.pri_decrypt(private_key, encrypted) # this returns the original data
73
+ ```
26
74
 
27
75
  ## Development
28
76
 
@@ -32,7 +80,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
80
 
33
81
  ## Contributing
34
82
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rsa-tools. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
+ Bug reports and pull requests are welcome on GitHub at [Here](https://github.com/w-zengtao/rsa-tools). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
84
 
37
85
  ## License
38
86
 
@@ -40,4 +88,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
88
 
41
89
  ## Code of Conduct
42
90
 
43
- Everyone interacting in the Rsa::Tools project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rsa-tools/blob/master/CODE_OF_CONDUCT.md).
91
+ Everyone interacting in the Rsa::Tools project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/w-zengtao/rsa-tools/blob/master/CODE_OF_CONDUCT.md).
@@ -4,23 +4,38 @@ require "rsa/tools/generator"
4
4
 
5
5
  module Rsa::Tools
6
6
  # 验签 & 用对方的公钥验签
7
- def self.verify(public_key, data)
7
+ def self.verify(public_key, data, original_data)
8
+ Utility.verify(public_key, data, original_data)
8
9
  end
9
10
 
10
11
  # 签名 & 用自己私钥签名 & RSAWithSha256 的签名
11
12
  def self.sign(private_key, data)
12
- pri_key = OpenSSL::PKey::RSA.new private_key
13
+ Utility.sign(private_key, data)
13
14
  end
14
15
 
15
- def self.key_pairs
16
- return Generator.key_pairs
16
+ # TO C的业务 & 私钥加密 公钥解密
17
+ def self.encrypt(private_key, data)
18
+ Utility.encrypt(private_key, data)
19
+ end
20
+
21
+ def self.decrypt(public_key, encrypted)
22
+ Utility.decrypt(public_key, encrypted)
23
+ end
24
+
25
+ # TO B的业务 & 公钥加密 私钥解密
26
+ def self.pub_encrypt(public_key, data)
27
+ Utility.pub_encrypt(public_key, data)
28
+ end
29
+
30
+ def self.pri_decrypt(private_key, encrypted)
31
+ Utility.pri_decrypt(private_key, encrypted)
17
32
  end
18
33
 
19
- def self.pem_pairs(pub_path = nil, pri_path = nil)
20
-
34
+ def self.key_pairs
35
+ return Generator.key_pairs
21
36
  end
22
37
 
23
- def self.test
24
- Utility.new.test
38
+ def self.pem_pairs(pri_path = nil, pub_path = nil)
39
+ return Generator.pem_pairs(pri_path, pub_path)
25
40
  end
26
41
  end
@@ -2,7 +2,7 @@ require 'openssl'
2
2
 
3
3
  module Rsa::Tools
4
4
  class Generator
5
-
5
+
6
6
  # call this function if you just want to save it to your database & send string to others
7
7
  def self.key_pairs
8
8
  private_key, public_key = generate_pairs
@@ -10,7 +10,7 @@ module Rsa::Tools
10
10
  end
11
11
 
12
12
  # call this function if pem files were wanted
13
- def self.pem_pairs(pub_path = nil, pri_path = nil)
13
+ def self.pem_pairs(pri_path = nil, pub_path = nil)
14
14
  private_key, public_key = generate_pairs
15
15
  open 'keys/private_key.pem', 'w' do |io| io.write private_key.to_pem end
16
16
  open 'keys/public_key.pem', 'w' do |io| io.write public_key.to_pem end
@@ -22,4 +22,4 @@ module Rsa::Tools
22
22
  return pri_key, pri_key.public_key
23
23
  end
24
24
  end
25
- end
25
+ end
@@ -1,16 +1,40 @@
1
+ require "base64"
2
+
1
3
  module Rsa::Tools
2
4
  class Utility
3
5
 
4
6
  # 我们是服务端的时候验证别人的请求的时候用这个方法
5
- def verify(public_key, data)
7
+ def self.verify(public_key, data, original_data)
8
+ key = OpenSSL::PKey::RSA.new(public_key)
9
+ key.verify(OpenSSL::Digest::SHA256.new, Base64.decode64(data), Digest::MD5.hexdigest(original_data.force_encoding("utf-8")))
6
10
  end
7
11
 
8
12
  # 三方要求我们私钥加签的时候调用这方法签名
9
- def sign(private_key, data)
13
+ def self.sign(private_key, data)
14
+ key = OpenSSL::PKey::RSA.new(private_key)
15
+ sign = key.sign(OpenSSL::Digest::SHA256.new, Digest::MD5.hexdigest(data.force_encoding("utf-8")))
16
+ Base64.strict_encode64(sign)
17
+ end
18
+
19
+ def self.encrypt(private_key, data)
20
+ key = OpenSSL::PKey::RSA.new(private_key)
21
+ return Base64.strict_encode64(key.private_encrypt(data))
22
+ end
23
+
24
+ def self.decrypt(public_key, encrypted)
25
+ key = OpenSSL::PKey::RSA.new(public_key)
26
+ return key.public_decrypt(Base64.decode64(encrypted))
27
+ end
28
+
29
+
30
+ def self.pub_encrypt(public_key, data)
31
+ key = OpenSSL::PKey::RSA.new(public_key)
32
+ return Base64.strict_encode64(key.public_encrypt(data))
10
33
  end
11
34
 
12
- def test
13
- puts "abc"
35
+ def self.pri_decrypt(private_key, encrypted)
36
+ key = OpenSSL::PKey::RSA.new(private_key)
37
+ return key.private_decrypt(Base64.decode64(encrypted))
14
38
  end
15
39
  end
16
- end
40
+ end
@@ -1,5 +1,5 @@
1
1
  module Rsa
2
2
  module Tools
3
- VERSION = "0.1.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsa-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 我天真无邪
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-15 00:00:00.000000000 Z
11
+ date: 2018-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler