pad_utils 1.13.0 → 1.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pad_utils.rb +17 -1
- data/lib/pad_utils/pad_security.rb +98 -0
- data/lib/pad_utils/version.rb +1 -1
- metadata +2 -3
- data/lib/pad_utils/padstone/connection.rb +0 -70
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb4153d0fd5e8bb8179dc43e3adfa658c54495f8
|
4
|
+
data.tar.gz: aac8f150884b06394c9a65977155c9b0a904393f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed4849a302cb80cf7b2511b1cd13d50523a535e4b7c4f131f1ef58b44491432286bd76fe50c5693723dcef65bd5d3efabd96eb8eef6069d7ec2c479d2927c7b2
|
7
|
+
data.tar.gz: 326c8e1a9038d248751a9d87d4da5eef2e7fc3735e1162b058fa1ed1652e3b4e4f4ac6c0970f17442a8f4ccd65caeba4acdad70ef177b292efa45bec478c3d50
|
data/lib/pad_utils.rb
CHANGED
@@ -10,7 +10,6 @@ require_relative "pad_utils/pad_code"
|
|
10
10
|
require_relative "pad_utils/pad_security"
|
11
11
|
require_relative "pad_utils/pad_http"
|
12
12
|
require_relative "pad_utils/pad_compression"
|
13
|
-
require_relative "pad_utils/padstone/connection"
|
14
13
|
|
15
14
|
# Main namespace for PadUtils.
|
16
15
|
#
|
@@ -37,11 +36,28 @@ module PadUtils
|
|
37
36
|
else
|
38
37
|
PadUtils.puts_c PadUtils.decrypt content: arg[1], key: arg[2]
|
39
38
|
end
|
39
|
+
elsif arg[0] == '--rsa'
|
40
|
+
if arg[1].nil? || arg[2].nil?
|
41
|
+
puts
|
42
|
+
PadUtils.puts_c "padutils --rsa <private | public> <path/to/key.pem>", :blue
|
43
|
+
else
|
44
|
+
generate_rsa arg[1], arg[2]
|
45
|
+
end
|
40
46
|
else
|
41
47
|
help
|
42
48
|
end
|
43
49
|
end
|
44
50
|
|
51
|
+
def self.generate_rsa(type, path)
|
52
|
+
if type == "private"
|
53
|
+
PadUtils.generate_rsa_private_key path: path
|
54
|
+
end
|
55
|
+
|
56
|
+
if type == "public"
|
57
|
+
PadUtils.generate_rsa_public_key private_key: path, path: "public.pem"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
45
61
|
# Display version of PadUtils if no switches are passed.
|
46
62
|
def self.help
|
47
63
|
puts
|
@@ -64,4 +64,102 @@ module PadUtils
|
|
64
64
|
decrypted = "invalid"
|
65
65
|
end
|
66
66
|
|
67
|
+
# Generates a RSA private key.
|
68
|
+
#
|
69
|
+
# @param key_size [Integer] key size (*defaults to 2048*)
|
70
|
+
# @param path [String] optional path to save the private key
|
71
|
+
# @return [RSA] the RSA private key
|
72
|
+
# @example
|
73
|
+
# PadUtils.generate_rsa_private_key 3096 # => <RSA key...>
|
74
|
+
def self.generate_rsa_private_key(key_size: 2048, path: nil)
|
75
|
+
key = OpenSSL::PKey::RSA.generate key_size
|
76
|
+
if !path.nil?
|
77
|
+
File.open(path, 'w') { |file| file.write(key.to_pem) }
|
78
|
+
end
|
79
|
+
key
|
80
|
+
end
|
81
|
+
|
82
|
+
# Generates a RSA public key.
|
83
|
+
#
|
84
|
+
# @param private_key [RSA, String] the private key as an RSA key or a pem file path
|
85
|
+
# @param path [String] optional path to save the public key
|
86
|
+
# @return [RSA] the RSA public key
|
87
|
+
# @example
|
88
|
+
# PadUtils.generate_rsa_public_key "private.pem" # => <RSA key...>
|
89
|
+
def self.generate_rsa_public_key(private_key: nil, path: nil)
|
90
|
+
key = nil
|
91
|
+
if private_key.class == OpenSSL::PKey::RSA
|
92
|
+
key = private_key.public_key
|
93
|
+
elsif private_key.class == String
|
94
|
+
private_key = OpenSSL::PKey::RSA.new(File.read(private_key))
|
95
|
+
key = PadUtils.generate_rsa_public_key private_key: private_key
|
96
|
+
end
|
97
|
+
|
98
|
+
if !path.nil? && !key.nil?
|
99
|
+
File.open(path, 'w') { |file| file.write(key.to_pem) }
|
100
|
+
end
|
101
|
+
key
|
102
|
+
end
|
103
|
+
|
104
|
+
# Encrypts a string with a RSA public key.
|
105
|
+
#
|
106
|
+
# @param content [String] the string to encrypt
|
107
|
+
# @param public_key [RSA, String] the public key as an RSA key or a pem file path
|
108
|
+
# @return [String] the encrypted string
|
109
|
+
# @example
|
110
|
+
# PadUtils.rsa_public_encrypt "Hello!", "public.pem" # => 'mwRAHtpE9...'
|
111
|
+
def self.rsa_public_encrypt(content: nil, public_key: nil)
|
112
|
+
if public_key.class == String
|
113
|
+
public_key = OpenSSL::PKey::RSA.new(File.read(public_key))
|
114
|
+
end
|
115
|
+
|
116
|
+
Base64.encode64(public_key.public_encrypt(content))
|
117
|
+
end
|
118
|
+
|
119
|
+
# Decrypts a string with a RSA private key.
|
120
|
+
#
|
121
|
+
# @param content [String] the string to decrypt
|
122
|
+
# @param public_key [RSA, String] the private key as an RSA key or a pem file path
|
123
|
+
# @return [String] the decrypted string
|
124
|
+
# @example
|
125
|
+
# PadUtils.rsa_private_decrypt "mwRAHtpE9...", "private.pem" # => 'Hello!'
|
126
|
+
def self.rsa_private_decrypt(content: nil, private_key: nil)
|
127
|
+
if private_key.class == String
|
128
|
+
private_key = OpenSSL::PKey::RSA.new(File.read(private_key))
|
129
|
+
end
|
130
|
+
|
131
|
+
private_key.private_decrypt(Base64.decode64(content))
|
132
|
+
end
|
133
|
+
|
134
|
+
# Encrypts a string with a RSA private key.
|
135
|
+
#
|
136
|
+
# @param content [String] the string to encrypt
|
137
|
+
# @param public_key [RSA, String] the private key as an RSA key or a pem file path
|
138
|
+
# @return [String] the encrypted string
|
139
|
+
# @example
|
140
|
+
# PadUtils.rsa_private_encrypt "Hello!", "private.pem" # => 'mwRAHtpE9...'
|
141
|
+
def self.rsa_private_encrypt(content: nil, private_key: nil)
|
142
|
+
if private_key.class == String
|
143
|
+
private_key = OpenSSL::PKey::RSA.new(File.read(private_key))
|
144
|
+
end
|
145
|
+
|
146
|
+
Base64.encode64(private_key.private_encrypt(content))
|
147
|
+
end
|
148
|
+
|
149
|
+
# Decrypts a string with a RSA public key.
|
150
|
+
#
|
151
|
+
# @param content [String] the string to decrypt
|
152
|
+
# @param public_key [RSA, String] the public key as an RSA key or a pem file path
|
153
|
+
# @return [String] the decrypted string
|
154
|
+
# @example
|
155
|
+
# PadUtils.rsa_public_decrypt "mwRAHtpE9...", "public.pem" # => 'Hello!'
|
156
|
+
def self.rsa_public_decrypt(content: nil, public_key: nil)
|
157
|
+
if public_key.class == String
|
158
|
+
public_key = OpenSSL::PKey::RSA.new(File.read(public_key))
|
159
|
+
end
|
160
|
+
|
161
|
+
public_key.public_decrypt(Base64.decode64(content))
|
162
|
+
end
|
163
|
+
|
164
|
+
|
67
165
|
end
|
data/lib/pad_utils/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pad_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nico Schuele
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -95,7 +95,6 @@ files:
|
|
95
95
|
- lib/pad_utils/pad_security.rb
|
96
96
|
- lib/pad_utils/pad_text.rb
|
97
97
|
- lib/pad_utils/pad_time.rb
|
98
|
-
- lib/pad_utils/padstone/connection.rb
|
99
98
|
- lib/pad_utils/version.rb
|
100
99
|
homepage: http://padstone.io
|
101
100
|
licenses:
|
@@ -1,70 +0,0 @@
|
|
1
|
-
module PadUtils
|
2
|
-
module Padstone
|
3
|
-
|
4
|
-
# Prod server URL
|
5
|
-
SERVER = "http://padstone.io/services/v1"
|
6
|
-
|
7
|
-
# Dev server URL
|
8
|
-
DEV_SERVER = "http://localhost:3000/services/v1"
|
9
|
-
|
10
|
-
# Retrieves Padstone servers connection status.
|
11
|
-
#
|
12
|
-
# @return [Hash]
|
13
|
-
# @example
|
14
|
-
# result = PadUtils::Padstone::connection_status
|
15
|
-
# # => {dev: :up, prod: :down}
|
16
|
-
def self.connection_status
|
17
|
-
result = {}
|
18
|
-
result[:dev] = dev_connection_status
|
19
|
-
result[:prod] = prod_connection_status
|
20
|
-
result
|
21
|
-
end
|
22
|
-
|
23
|
-
# Checks if Padstone dev or prod server answers.
|
24
|
-
#
|
25
|
-
# @note Dev or Prod will be chosen based on `ENV['PADSTONE']` which can
|
26
|
-
# be `development` or `production`
|
27
|
-
# @return [Boolean]
|
28
|
-
# @example
|
29
|
-
# ENV['PADSTONE'] = 'development'
|
30
|
-
# PadUtils::Padstone.connected? # => true
|
31
|
-
def self.connected?
|
32
|
-
up = false
|
33
|
-
if ENV['PADSTONE'] == "development"
|
34
|
-
up = dev_connection_status == :up
|
35
|
-
else
|
36
|
-
up = prod_connection_status == :up
|
37
|
-
end
|
38
|
-
up
|
39
|
-
end
|
40
|
-
|
41
|
-
# Gets the connection status of the dev server.
|
42
|
-
#
|
43
|
-
# @return [Symbol] can be `:up` or `:down`
|
44
|
-
# @example
|
45
|
-
# PadUtils::Padstone.dev_connection_status # => :up
|
46
|
-
def self.dev_connection_status
|
47
|
-
reply = PadUtils.http_get("#{DEV_SERVER}/connection/rick")
|
48
|
-
if reply.nil? || reply[:message].nil? || reply[:message] != "morty"
|
49
|
-
:down
|
50
|
-
else
|
51
|
-
:up
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
# Gets the connection status of the prod server.
|
56
|
-
#
|
57
|
-
# @return [Symbol] can be `:up` or `:down`
|
58
|
-
# @example
|
59
|
-
# PadUtils::Padstone.prod_connection_status # => :down
|
60
|
-
def self.prod_connection_status
|
61
|
-
reply = PadUtils.http_get("#{SERVER}/connection/rick")
|
62
|
-
if reply.nil? || reply[:message].nil? || reply[:message] != "morty"
|
63
|
-
:down
|
64
|
-
else
|
65
|
-
:up
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
end
|