-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (40 loc) · 777 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"bytes"
"crypto/aes"
"encoding/hex"
"fmt"
"os"
"github.com/doc-smith/cryptopals/util/inp"
)
func hasEqualBlocks(ct []byte, bLen int) bool {
const bs = aes.BlockSize
for i := bs; i < len(ct); i += bs {
for j := 0; j < i; j += bs {
bi := ct[i : i+bs]
bj := ct[j : j+bs]
if bytes.Equal(bi, bj) {
return true
}
}
}
return false
}
func solve(inp []string) int {
equalBlockCnt := 0
for _, line := range inp {
ct, err := hex.DecodeString(line)
if err != nil {
panic(err)
}
if hasEqualBlocks(ct, aes.BlockSize) {
equalBlockCnt++
}
}
return equalBlockCnt
}
func main() {
inp := inp.ReadLines(os.Stdin)
ans := solve(inp)
fmt.Printf("The number of lines with at least one pair of equal blocks: %d\n", ans)
}