From dc6cca42113ea7e9a292175e1704061d742014dd Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Tue, 8 Dec 2020 17:59:28 -0500 Subject: lib/blocks: move isECB from ch. 8 in here as BlockIsECB --- lib/blocks.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib') diff --git a/lib/blocks.go b/lib/blocks.go index 1ba811f..452766b 100644 --- a/lib/blocks.go +++ b/lib/blocks.go @@ -61,6 +61,29 @@ func BlocksEqual(a, b []byte) bool { return true } +func BlockIsECB(bs []byte) []byte { + blocks := BreakIntoBlocks(bs, 16) + + for i := 0; i < len(blocks); i++ { + if hasMatchingBlock(i, blocks) { + return blocks[i] + } + } + return nil +} + +func hasMatchingBlock(id int, blocks [][]byte) bool { + for i := 0; i < len(blocks); i++ { + if i == id { + continue + } + if BlocksEqual(blocks[i], blocks[id]) { + return true + } + } + return false +} + // Performs PKCS#7 Padding on the input `in` and block size `k`. // Assumes 0 > `k` < 256 // Reference: https://tools.ietf.org/html/rfc5652#section-6.3 -- cgit v1.2.3