blackbox 1.1.2 → 3.1.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.
- checksums.yaml +4 -4
- data/.rubocop.yml +25 -0
- data/Gemfile +1 -0
- data/Guardfile +3 -2
- data/Makefile +1 -0
- data/README.md +1 -1
- data/Rakefile +14 -16
- data/blackbox.gemspec +24 -18
- data/coverage/.last_run.json +1 -1
- data/coverage/.resultset.json +136 -37
- data/coverage/.resultset.json.lock +0 -0
- data/coverage/assets/0.10.0/application.css +799 -0
- data/coverage/assets/0.10.0/application.js +1707 -0
- data/coverage/assets/0.10.0/colorbox/border.png +0 -0
- data/coverage/assets/0.10.0/colorbox/controls.png +0 -0
- data/coverage/assets/0.10.0/colorbox/loading.gif +0 -0
- data/coverage/assets/0.10.0/colorbox/loading_background.png +0 -0
- data/coverage/assets/0.10.0/favicon_green.png +0 -0
- data/coverage/assets/0.10.0/favicon_red.png +0 -0
- data/coverage/assets/0.10.0/favicon_yellow.png +0 -0
- data/coverage/assets/0.10.0/loading.gif +0 -0
- data/coverage/assets/0.10.0/magnify.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.10.0/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/index.html +1153 -543
- data/doc/BB.html +22 -11
- data/doc/BB/Crypto.html +30 -34
- data/doc/BB/Gem.html +415 -0
- data/doc/BB/Hash.html +16 -16
- data/doc/BB/Number.html +100 -68
- data/doc/BB/String.html +12 -12
- data/doc/_index.html +22 -7
- data/doc/class_list.html +8 -3
- data/doc/css/style.css +2 -1
- data/doc/file.README.html +7 -7
- data/doc/file_list.html +7 -2
- data/doc/frames.html +5 -7
- data/doc/index.html +7 -7
- data/doc/js/app.js +7 -2
- data/doc/js/full_list.js +11 -3
- data/doc/method_list.html +25 -38
- data/doc/top-level-namespace.html +6 -6
- data/lib/blackbox.rb +7 -5
- data/lib/blackbox/crypto.rb +8 -10
- data/lib/blackbox/gem.rb +82 -0
- data/lib/blackbox/hash.rb +5 -5
- data/lib/blackbox/number.rb +127 -110
- data/lib/blackbox/string.rb +5 -5
- data/lib/blackbox/version.rb +2 -1
- data/spec/blackbox_spec.rb +1 -1
- data/spec/crypto_spec.rb +23 -23
- data/spec/gem_spec.rb +48 -0
- data/spec/hash_spec.rb +26 -26
- data/spec/number_spec.rb +95 -44
- data/spec/spec_helper.rb +2 -1
- data/spec/string_spec.rb +3 -3
- metadata +76 -4
data/lib/blackbox/gem.rb
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module BB
|
|
3
|
+
# Gem utilities.
|
|
4
|
+
module Gem
|
|
5
|
+
class << self
|
|
6
|
+
# Return information about the currently installed gem
|
|
7
|
+
# version and the latest available version on rubygems.org.
|
|
8
|
+
#
|
|
9
|
+
# @param [Hash] opts the options to create a message with.
|
|
10
|
+
# @option opts [Fixnum] :check_interval how frequently to query rubygems.org (default: 3600)
|
|
11
|
+
# @option opts [String] :disabling_env_var (default: #{GEMNAME}_DISABLE_VERSION_CHECK)
|
|
12
|
+
# @option opts [] :from ('nobody') From address
|
|
13
|
+
# @return [Hash] result
|
|
14
|
+
# * :gem_name => name of current gem
|
|
15
|
+
# * :gem_installed_version => installed version
|
|
16
|
+
# * :gem_latest_version => latest version on rubygems.org
|
|
17
|
+
# * :last_checked_for_update => timestamp of last query to rubygems.org
|
|
18
|
+
# * :next_check_for_update => timestamp of next query to rubygems.org
|
|
19
|
+
# * :gem_update_available => update available?
|
|
20
|
+
# * :installed_is_latest => is installed version == latest available version?
|
|
21
|
+
def version_info(*_, **opts)
|
|
22
|
+
ret = {
|
|
23
|
+
gem_name: :unknown,
|
|
24
|
+
gem_installed_version: :unknown,
|
|
25
|
+
gem_latest_version: :unknown,
|
|
26
|
+
gem_update_available: false,
|
|
27
|
+
last_checked_for_update: :unknown,
|
|
28
|
+
next_check_for_update: :unknown,
|
|
29
|
+
installed_is_latest: true
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
spec = ::Gem::Specification.find do |s|
|
|
33
|
+
File.fnmatch(File.join(s.full_gem_path, '*'), __FILE__)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
ret[:gem_installed_version] = spec&.version.to_s || :unknown
|
|
37
|
+
ret[:gem_name] = spec&.name || :unknown
|
|
38
|
+
|
|
39
|
+
opts = { # defaults
|
|
40
|
+
check_interval: 3600,
|
|
41
|
+
disabling_env_var: "#{spec.name.upcase}_DISABLE_VERSION_CHECK"
|
|
42
|
+
}.merge(opts)
|
|
43
|
+
|
|
44
|
+
return ret if ret[:gem_name] == :unknown
|
|
45
|
+
return ret if ret[:gem_installed_version] == :unknown
|
|
46
|
+
if opts[:disabling_env_var] && ENV.include?(opts[:disabling_env_var])
|
|
47
|
+
ret[:next_check_for_update] = :never
|
|
48
|
+
return ret
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
require 'gem_update_checker'
|
|
52
|
+
require 'tmpdir'
|
|
53
|
+
require 'fileutils'
|
|
54
|
+
|
|
55
|
+
statefile_path = File.join(Dir.tmpdir, "#{ret[:gem_name]}-#{ret[:gem_installed_version]}.last_update_check")
|
|
56
|
+
|
|
57
|
+
last_check_at = nil
|
|
58
|
+
begin
|
|
59
|
+
last_check_at = File.stat(statefile_path).mtime
|
|
60
|
+
rescue
|
|
61
|
+
last_check_at = Time.at(0)
|
|
62
|
+
end
|
|
63
|
+
return ret if last_check_at > Time.now - opts[:check_interval]
|
|
64
|
+
|
|
65
|
+
checker = GemUpdateChecker::Client.new(ret[:gem_name], ret[:gem_installed_version])
|
|
66
|
+
last_check_at = Time.now
|
|
67
|
+
|
|
68
|
+
ret.merge!(
|
|
69
|
+
gem_latest_version: checker.latest_version,
|
|
70
|
+
last_checked_for_update: last_check_at,
|
|
71
|
+
next_check_for_update: last_check_at + opts[:check_interval],
|
|
72
|
+
installed_is_latest: ret[:gem_installed_version] == checker.latest_version,
|
|
73
|
+
gem_update_available: checker.update_available
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
FileUtils.touch(statefile_path) if ret[:installed_is_latest]
|
|
77
|
+
|
|
78
|
+
ret
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
data/lib/blackbox/hash.rb
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
module BB
|
|
2
3
|
# Hash utilities.
|
|
3
4
|
module Hash
|
|
@@ -7,7 +8,7 @@ module BB
|
|
|
7
8
|
# @param [Hash] hash Input hash
|
|
8
9
|
# @return [Hash] Output hash (with symbolized keys)
|
|
9
10
|
def symbolize_keys(hash)
|
|
10
|
-
hash.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
|
|
11
|
+
hash.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
|
|
11
12
|
end
|
|
12
13
|
|
|
13
14
|
# Recursively flatten a hash to property-style format.
|
|
@@ -29,12 +30,12 @@ module BB
|
|
|
29
30
|
# Key delimiter (Default: '.')
|
|
30
31
|
# @param [Hash] output (leave this blank)
|
|
31
32
|
# @return [Hash] Output hash (flattened)
|
|
32
|
-
def flatten_prop_style(input={}, opts={}, output={})
|
|
33
|
+
def flatten_prop_style(input = {}, opts = {}, output = {})
|
|
33
34
|
input.each do |key, value|
|
|
34
|
-
key = opts[:prefix].nil? ?
|
|
35
|
+
key = opts[:prefix].nil? ? key.to_s : "#{opts[:prefix]}#{opts[:delimiter] || '.'}#{key}"
|
|
35
36
|
case value
|
|
36
37
|
when ::Hash
|
|
37
|
-
flatten_prop_style(value, opts.merge(
|
|
38
|
+
flatten_prop_style(value, opts.merge(prefix: key), output)
|
|
38
39
|
when Array
|
|
39
40
|
output[key] = value.join(',')
|
|
40
41
|
else
|
|
@@ -46,4 +47,3 @@ module BB
|
|
|
46
47
|
end
|
|
47
48
|
end
|
|
48
49
|
end
|
|
49
|
-
|
data/lib/blackbox/number.rb
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
require 'blackbox/hash'
|
|
2
3
|
|
|
3
4
|
module BB
|
|
4
5
|
# String utilities.
|
|
5
6
|
module Number
|
|
6
7
|
class << self
|
|
7
|
-
STORAGE_UNITS =
|
|
8
|
+
STORAGE_UNITS = %w(byte k M G T P E Z Y).freeze
|
|
8
9
|
|
|
9
10
|
##
|
|
10
|
-
# Formats the bytes in +
|
|
11
|
+
# Formats the bytes in +number+ into a more understandable representation
|
|
11
12
|
# (e.g., giving it 1500 yields 1.5k). This method is useful for
|
|
12
13
|
# reporting file sizes to users. This method returns nil if
|
|
13
|
-
# +
|
|
14
|
+
# +number+ cannot be converted into a number. You can customize the
|
|
14
15
|
# format in the +options+ hash.
|
|
15
16
|
#
|
|
16
17
|
# @overload to_human_size(number, options={})
|
|
@@ -42,124 +43,140 @@ module BB
|
|
|
42
43
|
# to_human_size(483989, :precision => 0) # => 473k
|
|
43
44
|
# to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18M
|
|
44
45
|
#
|
|
45
|
-
def to_human_size(number, args={})
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
separator ||= (options[:separator] || '.')
|
|
52
|
-
delimiter ||= (options[:delimiter] || '')
|
|
53
|
-
kilo ||= (options[:kilo] || 1024)
|
|
54
|
-
storage_units_format ||= (options[:format] || '%n%u')
|
|
46
|
+
def to_human_size(number, args = {})
|
|
47
|
+
begin
|
|
48
|
+
Float(number)
|
|
49
|
+
rescue
|
|
50
|
+
return nil
|
|
51
|
+
end
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, '')
|
|
58
|
-
else
|
|
59
|
-
max_exp = STORAGE_UNITS.size - 1
|
|
60
|
-
number = Float(number)
|
|
61
|
-
exponent = (Math.log(number) / Math.log(kilo)).to_i # Convert to base
|
|
62
|
-
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
|
|
63
|
-
number /= kilo ** exponent
|
|
53
|
+
options = BB::Hash.symbolize_keys(args)
|
|
64
54
|
|
|
65
|
-
|
|
55
|
+
precision ||= (options[:precision] || 1)
|
|
56
|
+
separator ||= (options[:separator] || '.')
|
|
57
|
+
delimiter ||= (options[:delimiter] || '')
|
|
58
|
+
kilo ||= (options[:kilo] || 1024)
|
|
59
|
+
storage_units_format ||= (options[:format] || '%n%u')
|
|
66
60
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
number
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
61
|
+
begin
|
|
62
|
+
if number.to_i < kilo
|
|
63
|
+
storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, '')
|
|
64
|
+
else
|
|
65
|
+
max_exp = STORAGE_UNITS.size - 1
|
|
66
|
+
number = Float(number)
|
|
67
|
+
exponent = (Math.log(number) / Math.log(kilo)).to_i # Convert to base
|
|
68
|
+
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
|
|
69
|
+
number /= kilo**exponent
|
|
80
70
|
|
|
81
|
-
|
|
82
|
-
# Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
|
|
83
|
-
# You can customize the format in the +options+ hash.
|
|
84
|
-
#
|
|
85
|
-
# @overload with_precision(number, options={})
|
|
86
|
-
# @param [Fixnum, Float] number
|
|
87
|
-
# Number value to format.
|
|
88
|
-
# @param [Hash] options
|
|
89
|
-
# Options for formatter.
|
|
90
|
-
# @option options [Fixnum] :precision (3)
|
|
91
|
-
# Sets the level of precision.
|
|
92
|
-
# @option options [String] :separator (".")
|
|
93
|
-
# Sets the separator between the units.
|
|
94
|
-
# @option options [String] :delimiter ("")
|
|
95
|
-
# Sets the thousands delimiter.
|
|
96
|
-
#
|
|
97
|
-
# @return [String] The formatted representation of the number.
|
|
98
|
-
#
|
|
99
|
-
# @example
|
|
100
|
-
# with_precision(111.2345) # => 111.235
|
|
101
|
-
# with_precision(111.2345, :precision => 2) # => 111.23
|
|
102
|
-
# with_precision(13, :precision => 5) # => 13.00000
|
|
103
|
-
# with_precision(389.32314, :precision => 0) # => 389
|
|
104
|
-
# with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
|
|
105
|
-
# # => 1.111,23
|
|
106
|
-
#
|
|
107
|
-
def with_precision(number, args)
|
|
108
|
-
options = BB::Hash.symbolize_keys(args)
|
|
71
|
+
unit = STORAGE_UNITS[exponent]
|
|
109
72
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
73
|
+
escaped_separator = Regexp.escape(separator)
|
|
74
|
+
formatted_number = with_precision(number,
|
|
75
|
+
precision: precision,
|
|
76
|
+
separator: separator,
|
|
77
|
+
delimiter: delimiter
|
|
78
|
+
).sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
|
|
79
|
+
storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
|
|
80
|
+
end
|
|
81
|
+
rescue
|
|
82
|
+
number
|
|
83
|
+
end
|
|
84
|
+
end
|
|
113
85
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
86
|
+
##
|
|
87
|
+
# Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
|
|
88
|
+
# This method returns nil if +number+ cannot be converted into a number.
|
|
89
|
+
# You can customize the format in the +options+ hash.
|
|
90
|
+
#
|
|
91
|
+
# @overload with_precision(number, options={})
|
|
92
|
+
# @param [Fixnum, Float] number
|
|
93
|
+
# Number value to format.
|
|
94
|
+
# @param [Hash] options
|
|
95
|
+
# Options for formatter.
|
|
96
|
+
# @option options [Fixnum] :precision (3)
|
|
97
|
+
# Sets the level of precision.
|
|
98
|
+
# @option options [String] :separator (".")
|
|
99
|
+
# Sets the separator between the units.
|
|
100
|
+
# @option options [String] :delimiter ("")
|
|
101
|
+
# Sets the thousands delimiter.
|
|
102
|
+
#
|
|
103
|
+
# @return [String] The formatted representation of the number.
|
|
104
|
+
#
|
|
105
|
+
# @example
|
|
106
|
+
# with_precision(111.2345) # => 111.235
|
|
107
|
+
# with_precision(111.2345, :precision => 2) # => 111.23
|
|
108
|
+
# with_precision(13, :precision => 5) # => 13.00000
|
|
109
|
+
# with_precision(389.32314, :precision => 0) # => 389
|
|
110
|
+
# with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
|
|
111
|
+
# # => 1.111,23
|
|
112
|
+
#
|
|
113
|
+
def with_precision(number, args)
|
|
114
|
+
begin
|
|
115
|
+
Float(number)
|
|
119
116
|
rescue
|
|
120
|
-
|
|
117
|
+
return nil
|
|
121
118
|
end
|
|
122
|
-
end
|
|
123
119
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
# @param [Fixnum, Float] number
|
|
130
|
-
# Number value to format.
|
|
131
|
-
# @param [Hash] options
|
|
132
|
-
# Options for formatter.
|
|
133
|
-
# @option options [String] :delimiter (", ")
|
|
134
|
-
# Sets the thousands delimiter.
|
|
135
|
-
# @option options [String] :separator (".")
|
|
136
|
-
# Sets the separator between the units.
|
|
137
|
-
#
|
|
138
|
-
# @return [String] The formatted representation of the number.
|
|
139
|
-
#
|
|
140
|
-
# @example
|
|
141
|
-
# with_delimiter(12345678) # => 12,345,678
|
|
142
|
-
# with_delimiter(12345678.05) # => 12,345,678.05
|
|
143
|
-
# with_delimiter(12345678, :delimiter => ".") # => 12.345.678
|
|
144
|
-
# with_delimiter(12345678, :separator => ",") # => 12,345,678
|
|
145
|
-
# with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
|
|
146
|
-
# # => 98 765 432,98
|
|
147
|
-
#
|
|
148
|
-
def with_delimiter(number, args)
|
|
149
|
-
options = BB::Hash.symbolize_keys(args)
|
|
120
|
+
options = BB::Hash.symbolize_keys(args)
|
|
121
|
+
|
|
122
|
+
precision ||= (options[:precision] || 3)
|
|
123
|
+
separator ||= (options[:separator] || '.')
|
|
124
|
+
delimiter ||= (options[:delimiter] || '')
|
|
150
125
|
|
|
151
|
-
|
|
152
|
-
|
|
126
|
+
begin
|
|
127
|
+
rounded_number = (Float(number) * (10**precision)).round.to_f / 10**precision
|
|
128
|
+
with_delimiter("%01.#{precision}f" % rounded_number,
|
|
129
|
+
separator: separator,
|
|
130
|
+
delimiter: delimiter)
|
|
131
|
+
rescue
|
|
132
|
+
number
|
|
133
|
+
end
|
|
134
|
+
end
|
|
153
135
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
136
|
+
##
|
|
137
|
+
# Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324).
|
|
138
|
+
# This method returns nil if +number+ cannot be converted into a number.
|
|
139
|
+
# You can customize the format in the +options+ hash.
|
|
140
|
+
#
|
|
141
|
+
# @overload with_delimiter(number, options={})
|
|
142
|
+
# @param [Fixnum, Float] number
|
|
143
|
+
# Number value to format.
|
|
144
|
+
# @param [Hash] options
|
|
145
|
+
# Options for formatter.
|
|
146
|
+
# @option options [String] :delimiter (", ")
|
|
147
|
+
# Sets the thousands delimiter.
|
|
148
|
+
# @option options [String] :separator (".")
|
|
149
|
+
# Sets the separator between the units.
|
|
150
|
+
#
|
|
151
|
+
# @return [String] The formatted representation of the number.
|
|
152
|
+
#
|
|
153
|
+
# @example
|
|
154
|
+
# with_delimiter(12345678) # => 12,345,678
|
|
155
|
+
# with_delimiter(12345678.05) # => 12,345,678.05
|
|
156
|
+
# with_delimiter(12345678, :delimiter => ".") # => 12.345.678
|
|
157
|
+
# with_delimiter(12345678, :separator => ",") # => 12,345,678
|
|
158
|
+
# with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
|
|
159
|
+
# # => 98 765 432,98
|
|
160
|
+
#
|
|
161
|
+
def with_delimiter(number, args = {})
|
|
162
|
+
begin
|
|
163
|
+
Float(number)
|
|
158
164
|
rescue
|
|
159
|
-
|
|
165
|
+
return nil
|
|
160
166
|
end
|
|
161
|
-
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
end
|
|
167
|
+
options = BB::Hash.symbolize_keys(args)
|
|
165
168
|
|
|
169
|
+
delimiter ||= (options[:delimiter] || ',')
|
|
170
|
+
separator ||= (options[:separator] || '.')
|
|
171
|
+
|
|
172
|
+
begin
|
|
173
|
+
parts = number.to_s.split('.')
|
|
174
|
+
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
|
|
175
|
+
parts.join(separator)
|
|
176
|
+
rescue
|
|
177
|
+
number
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
data/lib/blackbox/string.rb
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
module BB
|
|
2
3
|
# String utilities.
|
|
3
4
|
module String
|
|
4
|
-
class << self
|
|
5
|
+
class << self
|
|
5
6
|
# Strip ANSI escape sequences from String.
|
|
6
7
|
#
|
|
7
8
|
# @param [String] text Input string (dirty)
|
|
8
9
|
# @return [String] Output string (cleaned)
|
|
9
10
|
def strip_ansi(text)
|
|
10
|
-
text.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
text.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
|
|
12
|
+
.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
|
|
13
|
+
.gsub(/(\x03|\x1a)/, '')
|
|
13
14
|
end
|
|
14
15
|
end
|
|
15
16
|
end
|
|
16
17
|
end
|
|
17
|
-
|
data/lib/blackbox/version.rb
CHANGED
data/spec/blackbox_spec.rb
CHANGED
data/spec/crypto_spec.rb
CHANGED
|
@@ -1,49 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
require 'spec_helper'
|
|
2
3
|
require 'blackbox/crypto'
|
|
3
4
|
require 'base64'
|
|
4
5
|
|
|
5
|
-
OPENSSL_CIPHERS = %w
|
|
6
|
-
%w
|
|
7
|
-
%w
|
|
8
|
-
%w
|
|
9
|
-
%w
|
|
10
|
-
%w
|
|
11
|
-
%w
|
|
12
|
-
%w
|
|
13
|
-
%w
|
|
6
|
+
OPENSSL_CIPHERS = %w(aes-128-cbc aes-128-ecb aes-192-cbc aes-192-ecb aes-256-cbc) +
|
|
7
|
+
%w(aes-256-ecb bf bf-cbc bf-cfb) +
|
|
8
|
+
%w(bf-ecb bf-ofb cast cast-cbc cast5-cbc) +
|
|
9
|
+
%w(cast5-cfb cast5-ecb cast5-ofb des des-cbc) +
|
|
10
|
+
%w(des-cfb des-ecb des-ede des-ede-cbc des-ede-cfb) +
|
|
11
|
+
%w(des-ede-ofb des-ede3 des-ede3-cbc des-ede3-cfb des-ede3-ofb) +
|
|
12
|
+
%w(des-ofb des3 desx rc2 rc2-40-cbc) +
|
|
13
|
+
%w(rc2-64-cbc rc2-cbc rc2-cfb rc2-ecb rc2-ofb) +
|
|
14
|
+
%w(rc4 rc4-40)
|
|
14
15
|
|
|
15
|
-
TEST_KEY='12345678901234567890123456789012'
|
|
16
|
-
TEST_IV ='x234567890123456789012345678901x'
|
|
17
|
-
TEST_TEXT_SHORT='[SHORT_TEST]'*16
|
|
18
|
-
TEST_TEXT_LONG='[LONG_TEST]'*8192
|
|
16
|
+
TEST_KEY = '12345678901234567890123456789012'
|
|
17
|
+
TEST_IV = 'x234567890123456789012345678901x'
|
|
18
|
+
TEST_TEXT_SHORT = '[SHORT_TEST]' * 16
|
|
19
|
+
TEST_TEXT_LONG = '[LONG_TEST]' * 8192
|
|
19
20
|
|
|
20
21
|
describe BB::Crypto do
|
|
21
22
|
OPENSSL_CIPHERS.each do |cipher|
|
|
22
|
-
describe
|
|
23
|
+
describe cipher.to_s do
|
|
23
24
|
[[:encrypt, :decrypt],
|
|
24
25
|
[:encrypt_base64, :decrypt_base64],
|
|
25
26
|
[:encrypt_urlsafe_base64, :decrypt_urlsafe_base64]].each do |e|
|
|
26
27
|
m_enc, m_dec = e
|
|
27
28
|
describe "#{m_enc}, #{m_dec}" do
|
|
28
|
-
it
|
|
29
|
+
it 'can decrypt what it encrypted (short string, random iv)' do
|
|
29
30
|
ct = BB::Crypto.send(m_enc, TEST_TEXT_SHORT, TEST_KEY, cipher)
|
|
30
31
|
pt = BB::Crypto.send(m_dec, ct, TEST_KEY, cipher)
|
|
31
32
|
expect(pt).to eq(TEST_TEXT_SHORT)
|
|
32
33
|
end
|
|
33
34
|
|
|
34
|
-
it
|
|
35
|
+
it 'can decrypt what it encrypted (long string, random iv)' do
|
|
35
36
|
ct = BB::Crypto.send(m_enc, TEST_TEXT_LONG, TEST_KEY, cipher)
|
|
36
37
|
pt = BB::Crypto.send(m_dec, ct, TEST_KEY, cipher)
|
|
37
38
|
expect(pt).to eq(TEST_TEXT_LONG)
|
|
38
39
|
end
|
|
39
40
|
|
|
40
|
-
it
|
|
41
|
+
it 'can decrypt what it encrypted (long string, static iv)' do
|
|
41
42
|
ct = BB::Crypto.send(m_enc, TEST_TEXT_LONG, TEST_KEY, cipher, TEST_IV)
|
|
42
43
|
pt = BB::Crypto.send(m_dec, ct, TEST_KEY, cipher, TEST_IV)
|
|
43
44
|
expect(pt).to eq(TEST_TEXT_LONG)
|
|
44
45
|
end
|
|
45
46
|
|
|
46
|
-
it
|
|
47
|
+
it 'returns consistent output with static iv' do
|
|
47
48
|
a = BB::Crypto.send(m_enc, TEST_TEXT_SHORT, TEST_KEY, cipher, TEST_IV)
|
|
48
49
|
b = BB::Crypto.send(m_enc, TEST_TEXT_SHORT, TEST_KEY, cipher, TEST_IV)
|
|
49
50
|
expect(a).to eq(b)
|
|
@@ -51,15 +52,15 @@ describe BB::Crypto do
|
|
|
51
52
|
end
|
|
52
53
|
end
|
|
53
54
|
|
|
54
|
-
describe
|
|
55
|
-
it
|
|
55
|
+
describe 'encrypt_base64' do
|
|
56
|
+
it 'returns base64 string' do
|
|
56
57
|
ct = BB::Crypto.encrypt_base64(TEST_TEXT_LONG, TEST_KEY)
|
|
57
58
|
Base64.decode64(ct)
|
|
58
59
|
end
|
|
59
60
|
end
|
|
60
61
|
|
|
61
|
-
describe
|
|
62
|
-
it
|
|
62
|
+
describe 'encrypt_urlsafe_base64' do
|
|
63
|
+
it 'returns urlsafe base64 string' do
|
|
63
64
|
ct = BB::Crypto.encrypt_urlsafe_base64(TEST_TEXT_LONG, TEST_KEY)
|
|
64
65
|
Base64.urlsafe_decode64(ct)
|
|
65
66
|
end
|
|
@@ -67,4 +68,3 @@ describe BB::Crypto do
|
|
|
67
68
|
end
|
|
68
69
|
end
|
|
69
70
|
end
|
|
70
|
-
|