You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

check_helper.rb 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. module CheckHelper
  2. private def __label(value, color, state=true)
  3. color = :default unless color
  4. color = "state-#{color}" if state
  5. "<span class=\"label label-#{color}\">#{value}</span>"
  6. end
  7. def label(value, color, state=true)
  8. __label(value, color, state).html_safe
  9. end
  10. def cell(value, color, state=true)
  11. "<td class=\"label-state-#{color}\">#{value}</td>".html_safe
  12. end
  13. def labels(level, states, state=true)
  14. states.each_pair.collect do |name, value|
  15. color = if value.nil?
  16. :default
  17. elsif ::CryptCheck::State.bad? level
  18. value ? :danger : :success
  19. else
  20. value ? :success : :danger
  21. end
  22. __label name, color, state
  23. end.join(' ').html_safe
  24. end
  25. def states(states)
  26. ::CryptCheck::State.collect do |level|
  27. states[level].each_pair
  28. .select { |_, v| v == true }
  29. .collect { |name, _| __label name, level }
  30. end.flatten(1).join(' ').html_safe
  31. end
  32. def rank_color(rank)
  33. case rank
  34. when :'A+' then
  35. :great
  36. when :A then
  37. :best
  38. when :B then
  39. :good
  40. when :C, :D then
  41. :warning
  42. when :E, :F then
  43. :error
  44. else
  45. :critical
  46. end
  47. end
  48. def rank_label(rank)
  49. label rank, rank_color(rank)
  50. end
  51. def progress_color(percentage)
  52. case percentage
  53. when 0...20 then
  54. :error
  55. when 20...40 then
  56. :danger
  57. when 40...60 then
  58. :warning
  59. when 60...80 then
  60. :default
  61. when 80...90 then
  62. :success
  63. else
  64. :primary
  65. end
  66. end
  67. def score_progress(score)
  68. %Q(<div class="progress">
  69. <div class="progress-bar progress-bar-striped progress-bar-#{progress_color score}"
  70. style="width: #{score}%">
  71. #{score} / 100
  72. </div>
  73. </div>).html_safe
  74. end
  75. def protocol_label(protocol)
  76. label protocol.to_sym, protocol.status
  77. end
  78. def protocol_labels(protocols)
  79. protocols.collect { |p| protocol_label p }.join("\n").html_safe
  80. end
  81. def key_label(key)
  82. return label('Aucune', :error) unless key
  83. label "#{key[:type].upcase} #{key[:size]} bits", key_color(key)
  84. end
  85. def key_labels(keys)
  86. return label('Aucune', :error) if keys.empty?
  87. keys.sort { |a, b| -1 * (a[:size] <=> b[:size]) }.collect { |k| key_label k }.join("\n").html_safe
  88. end
  89. def cipher_size_label(cipher)
  90. size = cipher.size if cipher.is_a? CryptCheck::Tls::Cipher
  91. label "#{size} bits", cipher_color(size)
  92. end
  93. def key_color(key)
  94. case key[:size]
  95. when nil then
  96. :default
  97. when 0...1024 then
  98. :error
  99. when 1024...2048 then
  100. :danger
  101. when 2048...4096 then
  102. :warning
  103. else
  104. :success
  105. end
  106. end
  107. def cipher_color(key)
  108. case key
  109. when nil then
  110. :default
  111. when 0...128 then
  112. :error
  113. when 112...128 then
  114. :danger
  115. when 128...256 then
  116. :success
  117. else
  118. :primary
  119. end
  120. end
  121. def cipher_name_label(cipher)
  122. status = cipher.status
  123. status = :success if status == :good
  124. label("&nbsp;", status) + "&nbsp;#{cipher.name}".html_safe
  125. end
  126. def cipher_labels(cipher)
  127. cipher.state.collect { |c, ls| ls.collect { |l| label l.upcase, c } }
  128. .flatten(1).join("\n").html_safe
  129. end
  130. def cipher_kex_type_cell(kex)
  131. color = case kex
  132. when :ecdh then
  133. nil
  134. when :dh then
  135. :warning
  136. when :rsa then
  137. :error
  138. else
  139. :critical
  140. end
  141. kex ||= 'None'
  142. cell kex.to_s.upcase, color
  143. end
  144. def cipher_kex_size_cell(kex)
  145. color = key_color kex
  146. cell kex&.[](:size), color
  147. end
  148. def cipher_auth_type_cell(auth)
  149. color = case auth
  150. when :ecdsa, :rsa then
  151. nil
  152. else
  153. :critical
  154. end
  155. auth ||= 'None'
  156. cell auth.to_s.upcase, color
  157. end
  158. def cipher_auth_size_cell(auth)
  159. color = key_color auth
  160. cell auth&.[](:size), color
  161. end
  162. def cipher_enc_type_cell(enc)
  163. color = case enc
  164. when :chacha20
  165. :success
  166. when nil, :rc4
  167. :critical
  168. end
  169. enc ||= 'NONE'
  170. cell enc.to_s.upcase, color
  171. end
  172. def cipher_enc_block_size_cell(enc)
  173. color = case
  174. when enc.nil?
  175. nil
  176. when enc <= 64
  177. :critical
  178. when enc < 128
  179. :error
  180. end
  181. cell enc, color
  182. end
  183. def cipher_enc_key_size_cell(enc)
  184. color = case
  185. when enc.nil?
  186. nil
  187. when enc < 128
  188. :critical
  189. end
  190. cell enc, color
  191. end
  192. def cipher_enc_mode_cell(enc)
  193. color = case enc
  194. when :gcm, :ccm, :aead
  195. :success
  196. end
  197. cell enc.to_s.upcase, color
  198. end
  199. def cipher_mac_type_cell(mac)
  200. color = case mac
  201. when :poly1305 then
  202. :success
  203. when :sha384, :sha256 then
  204. nil
  205. when :sha1 then
  206. :warning
  207. else
  208. :critical
  209. end
  210. cell mac.to_s.upcase, color
  211. end
  212. def cipher_mac_size_cell(mac)
  213. cell mac, nil
  214. end
  215. def cipher_pfs_cell(pfs)
  216. return cell 'PFS', nil if pfs
  217. cell 'No PFS', :error
  218. end
  219. end