rsa-tools 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/README.md +54 -6
- data/lib/rsa/tools.rb +23 -8
- data/lib/rsa/tools/generator.rb +3 -3
- data/lib/rsa/tools/utility.rb +29 -5
- data/lib/rsa/tools/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87c32d05e60ad507b21efc98df0e21d84bec0f03a305f8843a32b60a1ebdddd0
|
4
|
+
data.tar.gz: 0e991bc21db0d654d622c509a6c6a5ee39d001187e793bdfed8c1535bb006a26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '092306579ed871ef15eea6a2be9e0c467656668f769941b915e71503e25a094588cb6031138081d6b8f12eb638a79ed7a931ea06070c8a0fd957134b063ffbb8'
|
7
|
+
data.tar.gz: 8d19aca66995578c280ba8bca24f1cf497e961eaf2a9f74f7e02a2a6f0da3a2aa859b8c9c9c37cf4d452d418a5888e2b7b1b04d71720c20b5f1d6727f4d45a24
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Rsa::Tools
|
2
2
|
|
3
|
-
|
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
|
-
|
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/
|
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/
|
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).
|
data/lib/rsa/tools.rb
CHANGED
@@ -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
|
-
|
13
|
+
Utility.sign(private_key, data)
|
13
14
|
end
|
14
15
|
|
15
|
-
|
16
|
-
|
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.
|
20
|
-
|
34
|
+
def self.key_pairs
|
35
|
+
return Generator.key_pairs
|
21
36
|
end
|
22
37
|
|
23
|
-
def self.
|
24
|
-
|
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
|
data/lib/rsa/tools/generator.rb
CHANGED
@@ -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(
|
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
|
data/lib/rsa/tools/utility.rb
CHANGED
@@ -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
|
13
|
-
|
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
|
data/lib/rsa/tools/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2018-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|