class Utils::EcbDetector
Public Instance Methods
detect(ciphers)
click to toggle source
# File lib/crypto-toolbox/utils/ecb_detector.rb, line 9 def detect(ciphers) result = ciphers.map.with_index do|c,i| ecb_mode?(c) ? [i,c] : [] end sanitize_result(result) end
is_ecb?(ciphertext)
click to toggle source
# File lib/crypto-toolbox/utils/ecb_detector.rb, line 4 def is_ecb?(ciphertext) is_ecb_aligned?(CryptBuffer(ciphertext)) || is_ecb_unaligned?(CryptBuffer(ciphertext)) end
Private Instance Methods
duplicate_chunk?(chunks)
click to toggle source
search for any chunks whose byte-pattern occours more than once, in that case the number of entries is reduced by uniq
# File lib/crypto-toolbox/utils/ecb_detector.rb, line 32 def duplicate_chunk?(chunks) chunks.map(&:bytes).uniq.length < chunks.length end
ecb_mode?(ciphertext)
click to toggle source
# File lib/crypto-toolbox/utils/ecb_detector.rb, line 36 def ecb_mode?(ciphertext) duplicate_chunk?(ciphertext.chunks_of(16)) end
is_ecb_aligned?(buffer)
click to toggle source
# File lib/crypto-toolbox/utils/ecb_detector.rb, line 18 def is_ecb_aligned?(buffer) ecb_mode?(buffer) end
is_ecb_unaligned?(buffer)
click to toggle source
# File lib/crypto-toolbox/utils/ecb_detector.rb, line 22 def is_ecb_unaligned?(buffer) false end
sanitize_result(result)
click to toggle source
# File lib/crypto-toolbox/utils/ecb_detector.rb, line 26 def sanitize_result(result) result.reject(&:empty?) end