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.

264 lines
4.9KB

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