2015-08-22 21:50:17 +00:00
|
|
|
require 'openssl'
|
|
|
|
|
2016-11-16 23:53:29 +00:00
|
|
|
class String
|
|
|
|
alias :colorize_old :colorize
|
|
|
|
|
|
|
|
COLORS = {
|
|
|
|
critical: { color: :white, background: :red },
|
2017-01-02 10:22:18 +00:00
|
|
|
error: :red,
|
|
|
|
warning: :light_red,
|
|
|
|
good: :green,
|
|
|
|
perfect: :blue,
|
|
|
|
best: :magenta,
|
|
|
|
unknown: { background: :black }
|
2016-11-16 23:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def colorize(state)
|
2017-01-02 10:22:18 +00:00
|
|
|
color = COLORS[state] || state
|
|
|
|
self.colorize_old color
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Exception
|
|
|
|
BACKTRACE_REGEXP = /^(.*):(\d+):in `(.*)'$/
|
|
|
|
|
|
|
|
def colorize
|
|
|
|
$stderr.puts self.message.colorize(:red)
|
|
|
|
self.backtrace.each do |line|
|
|
|
|
line = BACKTRACE_REGEXP.match line
|
|
|
|
line = '%s:%s:in `%s\'' % [
|
|
|
|
line[1].colorize(:yellow),
|
|
|
|
line[2].colorize(:blue),
|
|
|
|
line[3].colorize(:magenta)
|
|
|
|
]
|
|
|
|
$stderr.puts line
|
|
|
|
end
|
2016-11-16 23:53:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-03 17:57:34 +00:00
|
|
|
class Integer
|
|
|
|
def humanize
|
|
|
|
secs = self
|
2017-01-02 10:22:18 +00:00
|
|
|
[[60, :second],
|
|
|
|
[60, :minute],
|
|
|
|
[24, :hour],
|
|
|
|
[30, :day],
|
|
|
|
[12, :month]].map do |count, name|
|
2016-05-03 17:57:34 +00:00
|
|
|
if secs > 0
|
2016-05-10 15:23:15 +00:00
|
|
|
secs, n = secs.divmod count
|
2017-01-02 10:22:18 +00:00
|
|
|
n = n.to_i
|
2016-05-10 15:23:15 +00:00
|
|
|
n > 0 ? "#{n} #{name}#{n > 1 ? 's' : ''}" : nil
|
2016-05-03 17:57:34 +00:00
|
|
|
end
|
2017-01-02 10:22:18 +00:00
|
|
|
end.compact.reverse.join ' '
|
2016-05-03 17:57:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-19 16:04:13 +00:00
|
|
|
class ::OpenSSL::PKey::EC
|
|
|
|
def type
|
|
|
|
:ecc
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
self.group.degree
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"ECC #{self.size} bits"
|
|
|
|
end
|
2016-11-11 15:59:33 +00:00
|
|
|
|
|
|
|
def status
|
|
|
|
case self.size
|
2017-01-02 10:22:18 +00:00
|
|
|
when 0...160
|
|
|
|
:critical
|
|
|
|
when 160...192
|
|
|
|
:error
|
|
|
|
when 192...256
|
|
|
|
:warning
|
2017-01-06 20:10:26 +00:00
|
|
|
when 256...364
|
|
|
|
else
|
2017-01-02 10:22:18 +00:00
|
|
|
:good
|
2016-11-11 15:59:33 +00:00
|
|
|
end
|
|
|
|
end
|
2015-08-19 16:04:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class ::OpenSSL::PKey::RSA
|
|
|
|
def type
|
|
|
|
:rsa
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
self.n.num_bits
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"RSA #{self.size} bits"
|
|
|
|
end
|
2016-11-11 15:59:33 +00:00
|
|
|
|
|
|
|
def status
|
|
|
|
case self.size
|
2017-01-02 10:22:18 +00:00
|
|
|
when 0...1024
|
|
|
|
:critical
|
|
|
|
when 1024...2048
|
|
|
|
:error
|
2017-01-06 20:10:26 +00:00
|
|
|
when 2048...4096
|
|
|
|
else
|
2017-01-02 10:22:18 +00:00
|
|
|
:good
|
2016-11-11 15:59:33 +00:00
|
|
|
end
|
|
|
|
end
|
2015-08-19 16:04:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class ::OpenSSL::PKey::DSA
|
|
|
|
def type
|
|
|
|
:dsa
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
self.p.num_bits
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"DSA #{self.size} bits"
|
|
|
|
end
|
2016-11-11 15:59:33 +00:00
|
|
|
|
|
|
|
def status
|
2016-11-16 23:53:29 +00:00
|
|
|
return :critical
|
2016-11-11 15:59:33 +00:00
|
|
|
end
|
2015-08-19 16:04:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class ::OpenSSL::PKey::DH
|
|
|
|
def type
|
|
|
|
:dh
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
self.p.num_bits
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"DH #{self.size} bits"
|
|
|
|
end
|
2016-11-11 15:59:33 +00:00
|
|
|
|
|
|
|
def status
|
|
|
|
case self.size
|
2017-01-02 10:22:18 +00:00
|
|
|
when 0...1024
|
|
|
|
:critical
|
|
|
|
when 1024...2048
|
|
|
|
:error
|
|
|
|
when 2048...4096
|
|
|
|
else
|
|
|
|
:good
|
2016-11-11 15:59:33 +00:00
|
|
|
end
|
|
|
|
end
|
2015-08-19 16:04:13 +00:00
|
|
|
end
|