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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 666d446c66e74463183e738bd50dad4d366a0be4
4
- data.tar.gz: 64d015b46bfe342bb7c135057b4e72f1b394184a
3
+ metadata.gz: cb4153d0fd5e8bb8179dc43e3adfa658c54495f8
4
+ data.tar.gz: aac8f150884b06394c9a65977155c9b0a904393f
5
5
  SHA512:
6
- metadata.gz: d923ff1197164a19d94d6fe140bf9bf5bb7d75777cbc7b83a1e356d9de981f83c2de4b6f2fe8d3e71c371339ef78d35e9d4575a4310e9a8c3d9cd0b774c9c242
7
- data.tar.gz: 82423a6dbe5a1d04002900fdb7d22f8299aa12dbadef6ea48e7593d0ffc37101dc8c956ba143089bf5d77b9f20e4501b7952195faba077f4da177d3ff3f6a473
6
+ metadata.gz: ed4849a302cb80cf7b2511b1cd13d50523a535e4b7c4f131f1ef58b44491432286bd76fe50c5693723dcef65bd5d3efabd96eb8eef6069d7ec2c479d2927c7b2
7
+ data.tar.gz: 326c8e1a9038d248751a9d87d4da5eef2e7fc3735e1162b058fa1ed1652e3b4e4f4ac6c0970f17442a8f4ccd65caeba4acdad70ef177b292efa45bec478c3d50
@@ -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
@@ -1,4 +1,4 @@
1
1
  module PadUtils
2
2
  # PadUtils version number
3
- VERSION = "1.13.0"
3
+ VERSION = "1.14.0"
4
4
  end
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.13.0
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-03-19 00:00:00.000000000 Z
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