pelock-stringencrypt 1.0.1

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.
@@ -0,0 +1,286 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "zlib"
5
+
6
+ class StringEncrypt
7
+ DEFAULT_API_URL = "https://www.stringencrypt.com/api.php"
8
+ USER_AGENT = "pelock/stringencrypt (+https://www.stringencrypt.com)"
9
+
10
+ def initialize(api_key = "", prefer_curl = false)
11
+ @api_key = api_key.to_s
12
+ @prefer_curl = prefer_curl # ignored; Ruby client always uses Net::HTTP
13
+ @decompress_encrypt_source = true
14
+ reset_defaults(constructor: true)
15
+ end
16
+
17
+ def is_demo
18
+ previous = @command
19
+ self.command = Command::IS_DEMO
20
+ result = send
21
+ @command = previous
22
+ result
23
+ end
24
+
25
+ def encrypt_file_contents(file_path, label)
26
+ raw = File.binread(file_path)
27
+ return false if raw.nil? || raw.empty?
28
+
29
+ saved = snapshot_input
30
+ self.command = Command::ENCRYPT
31
+ set_bytes(raw)
32
+ self.label = label
33
+ result = send
34
+ restore_input(saved)
35
+ result
36
+ rescue StandardError
37
+ false
38
+ end
39
+
40
+ def encrypt_string(string, label)
41
+ saved = snapshot_input
42
+ self.command = Command::ENCRYPT
43
+ set_string(string)
44
+ self.label = label
45
+ result = send
46
+ restore_input(saved)
47
+ result
48
+ end
49
+
50
+ attr_accessor :prefer_curl, :decompress_encrypt_source, :command, :label,
51
+ :compression, :language, :highlight, :cmd_min, :cmd_max,
52
+ :local, :unicode, :lang_locale, :ansi_encoding, :new_lines,
53
+ :template, :return_template, :include_tags, :include_example,
54
+ :include_debug_comments
55
+
56
+ def set_command(command)
57
+ @command = command
58
+ self
59
+ end
60
+
61
+ def set_label(label)
62
+ @label = label
63
+ self
64
+ end
65
+
66
+ def set_string(string)
67
+ @input_string = string
68
+ @input_bytes = nil
69
+ self
70
+ end
71
+
72
+ def set_bytes(bytes)
73
+ @input_bytes = bytes
74
+ @input_string = nil
75
+ self
76
+ end
77
+
78
+ def set_compression(compression)
79
+ @compression = compression
80
+ self
81
+ end
82
+
83
+ def set_language(language)
84
+ @language = language
85
+ self
86
+ end
87
+
88
+ def set_highlight(highlight)
89
+ @highlight = highlight
90
+ self
91
+ end
92
+
93
+ def set_cmd_min(cmd_min)
94
+ @cmd_min = cmd_min
95
+ self
96
+ end
97
+
98
+ def set_cmd_max(cmd_max)
99
+ @cmd_max = cmd_max
100
+ self
101
+ end
102
+
103
+ def set_local(local)
104
+ @local = local
105
+ self
106
+ end
107
+
108
+ def set_unicode(unicode)
109
+ @unicode = unicode
110
+ self
111
+ end
112
+
113
+ def set_lang_locale(lang_locale)
114
+ @lang_locale = lang_locale
115
+ self
116
+ end
117
+
118
+ def set_ansi_encoding(ansi_encoding)
119
+ @ansi_encoding = ansi_encoding
120
+ self
121
+ end
122
+
123
+ def set_new_lines(new_lines)
124
+ @new_lines = new_lines
125
+ self
126
+ end
127
+
128
+ def set_template(template)
129
+ @template = template
130
+ self
131
+ end
132
+
133
+ def set_return_template(return_template)
134
+ @return_template = return_template
135
+ self
136
+ end
137
+
138
+ def set_include_tags(include_tags)
139
+ @include_tags = include_tags
140
+ self
141
+ end
142
+
143
+ def set_include_example(include_example)
144
+ @include_example = include_example
145
+ self
146
+ end
147
+
148
+ def set_include_debug_comments(include_debug_comments)
149
+ @include_debug_comments = include_debug_comments
150
+ self
151
+ end
152
+
153
+ def set_decompress_encrypt_source(decompress_encrypt_source)
154
+ @decompress_encrypt_source = decompress_encrypt_source
155
+ self
156
+ end
157
+
158
+ def reset
159
+ reset_defaults(constructor: false)
160
+ self
161
+ end
162
+
163
+ def to_request_array
164
+ raise ArgumentError, "Command must be set (use set_command)." if @command.nil?
165
+
166
+ case @command
167
+ when Command::INFO then build_info_params
168
+ when Command::IS_DEMO then build_is_demo_params
169
+ when Command::ENCRYPT then build_encrypt_params
170
+ else
171
+ raise ArgumentError, "Unknown command: #{@command}"
172
+ end
173
+ end
174
+
175
+ # PHP/JS `send()`. A Symbol first argument is forwarded to Object#send
176
+ # so metaprogramming still works.
177
+ def send(method_or_curl = :__http__, *args, &block)
178
+ unless method_or_curl == :__http__ || method_or_curl == true || method_or_curl == false || method_or_curl.nil?
179
+ return super(method_or_curl, *args, &block)
180
+ end
181
+
182
+ send_request
183
+ rescue ArgumentError
184
+ false
185
+ end
186
+
187
+ def send_request
188
+ params = to_request_array
189
+ raw = Http.post_urlencoded(DEFAULT_API_URL, params, user_agent: USER_AGENT)
190
+ return false if raw.nil? || raw.empty?
191
+
192
+ decoded = Http.parse_json(raw)
193
+ return false unless decoded.is_a?(Hash)
194
+
195
+ apply_decryptor_source_decompression(decoded)
196
+ end
197
+
198
+ private
199
+
200
+ def reset_defaults(constructor:)
201
+ @command = nil
202
+ @label = constructor ? "Label" : "$label"
203
+ @input_string = nil
204
+ @input_bytes = nil
205
+ @compression = false
206
+ @language = Language::PHP
207
+ @highlight = false
208
+ @cmd_min = 1
209
+ @cmd_max = 3
210
+ @local = false
211
+ @unicode = true
212
+ @lang_locale = "en_US.utf8"
213
+ @ansi_encoding = "WINDOWS-1250"
214
+ @new_lines = NewLine::LF
215
+ @template = nil
216
+ @return_template = false
217
+ @include_tags = false
218
+ @include_example = false
219
+ @include_debug_comments = false
220
+ end
221
+
222
+ def snapshot_input
223
+ {
224
+ command: @command,
225
+ input_string: @input_string,
226
+ input_bytes: @input_bytes,
227
+ label: @label
228
+ }
229
+ end
230
+
231
+ def restore_input(saved)
232
+ @command = saved[:command]
233
+ @input_string = saved[:input_string]
234
+ @input_bytes = saved[:input_bytes]
235
+ @label = saved[:label]
236
+ end
237
+
238
+ def build_info_params
239
+ { "command" => Command::INFO, "code" => @api_key }
240
+ end
241
+
242
+ def build_is_demo_params
243
+ { "command" => Command::IS_DEMO, "code" => @api_key }
244
+ end
245
+
246
+ def build_encrypt_params
247
+ params = {
248
+ "command" => Command::ENCRYPT,
249
+ "code" => @api_key,
250
+ "label" => @label,
251
+ "compression" => @compression,
252
+ "lang" => @language,
253
+ "cmd_min" => @cmd_min,
254
+ "cmd_max" => @cmd_max,
255
+ "local" => @local,
256
+ "unicode" => @unicode,
257
+ "lang_locale" => @lang_locale,
258
+ "ansi_encoding" => @ansi_encoding,
259
+ "new_lines" => @new_lines,
260
+ "return_template" => @return_template,
261
+ "include_tags" => @include_tags,
262
+ "include_example" => @include_example,
263
+ "include_debug_comments" => @include_debug_comments
264
+ }
265
+ if !@input_string.nil?
266
+ params["string"] = @input_string
267
+ elsif !@input_bytes.nil?
268
+ params["bytes"] = @input_bytes
269
+ end
270
+ params["highlight"] = @highlight unless @highlight == false
271
+ params["template"] = @template unless @template.nil?
272
+ params
273
+ end
274
+
275
+ def apply_decryptor_source_decompression(response)
276
+ return response unless @command == Command::ENCRYPT && @compression && @decompress_encrypt_source
277
+ return response unless response["error"] == ErrorCode::SUCCESS
278
+ return response unless response["source"].is_a?(String)
279
+
280
+ binary = Base64.decode64(response["source"])
281
+ response["source"] = Zlib::Inflate.inflate(binary)
282
+ response
283
+ rescue StandardError
284
+ response
285
+ end
286
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StringEncrypt
4
+ module Command
5
+ ENCRYPT = "encrypt"
6
+ IS_DEMO = "is_demo"
7
+ INFO = "info"
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StringEncrypt
4
+ module ErrorCode
5
+ SUCCESS = 0
6
+ EMPTY_LABEL = 1
7
+ LENGTH_LABEL = 2
8
+ EMPTY_STRING = 3
9
+ EMPTY_BYTES = 4
10
+ EMPTY_INPUT = 5
11
+ LENGTH_STRING = 6
12
+ INVALID_LANG = 7
13
+ INVALID_LOCALE = 8
14
+ CMD_MIN = 9
15
+ CMD_MAX = 10
16
+ LENGTH_BYTES = 11
17
+ DEMO = 100
18
+ end
19
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "openssl"
6
+ require "securerandom"
7
+ require "uri"
8
+
9
+ class StringEncrypt
10
+ # Stdlib HTTP helper. Multipart for API form posts / file uploads; urlencoded when needed.
11
+ module Http
12
+ class << self
13
+ def post_multipart(url, fields, user_agent:, files: {})
14
+ boundary = "----PELock#{SecureRandom.hex(16)}"
15
+ body = build_multipart(fields, files, boundary)
16
+ request(url, body, user_agent, "multipart/form-data; boundary=#{boundary}")
17
+ end
18
+
19
+ def post_urlencoded(url, fields, user_agent:)
20
+ pairs = []
21
+ fields.each do |key, value|
22
+ next if value.nil?
23
+
24
+ pairs << [key.to_s, normalize_form_value(value)]
25
+ end
26
+ request(url, URI.encode_www_form(pairs), user_agent, "application/x-www-form-urlencoded")
27
+ end
28
+
29
+ def parse_json(body)
30
+ return nil if body.nil? || body.to_s.empty?
31
+
32
+ JSON.parse(body)
33
+ rescue JSON::ParserError
34
+ nil
35
+ end
36
+
37
+ def request(url, body, user_agent, content_type)
38
+ uri = URI.parse(url)
39
+ http = Net::HTTP.new(uri.host, uri.port)
40
+ http.use_ssl = uri.scheme == "https"
41
+ http.open_timeout = 30
42
+ http.read_timeout = 180
43
+ req = Net::HTTP::Post.new(uri.request_uri)
44
+ req["User-Agent"] = user_agent
45
+ req["Content-Type"] = content_type
46
+ req.body = body
47
+ http.request(req).body
48
+ rescue StandardError
49
+ nil
50
+ end
51
+
52
+ def build_multipart(fields, files, boundary)
53
+ body = String.new(encoding: Encoding::ASCII_8BIT)
54
+ fields.each do |name, value|
55
+ next if value.nil?
56
+
57
+ body << "--#{boundary}\r\n"
58
+ body << "Content-Disposition: form-data; name=\"#{name}\"\r\n\r\n"
59
+ body << value.to_s.dup.force_encoding(Encoding::UTF_8).b
60
+ body << "\r\n"
61
+ end
62
+ files.each do |name, file|
63
+ filename = file[:filename] || File.basename(file[:path].to_s)
64
+ content = file[:content] || File.binread(file[:path])
65
+ content_type = file[:content_type] || "application/octet-stream"
66
+ body << "--#{boundary}\r\n"
67
+ body << "Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{filename}\"\r\n"
68
+ body << "Content-Type: #{content_type}\r\n\r\n"
69
+ body << content.b
70
+ body << "\r\n"
71
+ end
72
+ body << "--#{boundary}--\r\n"
73
+ body
74
+ end
75
+
76
+ def normalize_form_value(value)
77
+ case value
78
+ when true then "1"
79
+ when false then "0"
80
+ else value.to_s
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StringEncrypt
4
+ module Language
5
+ CPP = "cpp"
6
+ CSHARP = "csharp"
7
+ VBNET = "vbnet"
8
+ DELPHI = "delphi"
9
+ JAVA = "java"
10
+ JAVASCRIPT = "js"
11
+ PYTHON = "python"
12
+ RUBY = "ruby"
13
+ AUTOIT = "autoit"
14
+ POWERSHELL = "powershell"
15
+ HASKELL = "haskell"
16
+ MASM = "masm"
17
+ FASM = "fasm"
18
+ GO = "go"
19
+ RUST = "rust"
20
+ SWIFT = "swift"
21
+ KOTLIN = "kotlin"
22
+ LUA = "lua"
23
+ DART = "dart"
24
+ PHP = "php"
25
+ OBJC = "objc"
26
+ NASM = "nasm"
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StringEncrypt
4
+ module NewLine
5
+ LF = "lf"
6
+ CRLF = "crlf"
7
+ CR = "cr"
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StringEncrypt
4
+ VERSION = "1.0.1"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "stringencrypt/version"
4
+ require_relative "stringencrypt/http"
5
+ require_relative "stringencrypt/command"
6
+ require_relative "stringencrypt/error_code"
7
+ require_relative "stringencrypt/language"
8
+ require_relative "stringencrypt/new_line"
9
+ require_relative "stringencrypt/client"
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pelock-stringencrypt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bartosz Wójcik
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email:
13
+ - support@pelock.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - LICENSE
19
+ - README.md
20
+ - lib/stringencrypt.rb
21
+ - lib/stringencrypt/client.rb
22
+ - lib/stringencrypt/command.rb
23
+ - lib/stringencrypt/error_code.rb
24
+ - lib/stringencrypt/http.rb
25
+ - lib/stringencrypt/language.rb
26
+ - lib/stringencrypt/new_line.rb
27
+ - lib/stringencrypt/version.rb
28
+ homepage: https://www.stringencrypt.com
29
+ licenses:
30
+ - Apache-2.0
31
+ metadata:
32
+ homepage_uri: https://www.stringencrypt.com
33
+ source_code_uri: https://github.com/PELock/StringEncrypt-Ruby
34
+ bug_tracker_uri: https://www.pelock.com
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.7.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 4.0.20
50
+ specification_version: 4
51
+ summary: StringEncrypt Web API Ruby SDK
52
+ test_files: []