-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge50.go
51 lines (40 loc) · 1.16 KB
/
challenge50.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
46
47
48
49
50
51
package set7
import (
"bytes"
"fmt"
"os"
"github.com/alokmenghrajani/go-cryptopals/utils"
)
func Challenge50() {
utils.PrintTitle(7, 50)
// compute first hash
key := []byte("YELLOW SUBMARINE")
iv := make([]byte, 16)
expectedHash := utils.HexToByteSlice("296b8d7cb78a243dda4d0a61d33bbdd1")
js := []byte("alert('MZA who was that?');\n")
hash := cbcMac(js, iv, key)
if !bytes.Equal(expectedHash, hash) {
panic("unexpected hash")
}
// forge different string
js2 := []byte("alert('Ayo, the Wu is back!');//")
hash2 := cbcMac(js2, iv, key)
finalJs := utils.Pad(js2, 16)
xorBlock := utils.Xor(js[0:16], hash2)
finalJs = append(finalJs, xorBlock...)
finalJs = append(finalJs, js[16:]...)
hash = cbcMac(finalJs, iv, key)
if !bytes.Equal(expectedHash, hash) {
panic("unexpected hash")
}
encodedJs := []byte("<html><body><script>")
encodedJs = append(encodedJs, finalJs...)
encodedJs = append(encodedJs, []byte("</script></body></html>")...)
fmt.Println(utils.ByteSliceToBase64(encodedJs))
file, err := os.Create("50.html")
utils.PanicOnErr(err)
defer file.Close()
_, err = file.WriteString(string(encodedJs))
utils.PanicOnErr(err)
fmt.Println()
}