cryptopals/test/set_1_test.go
2024-10-28 22:15:03 +02:00

55 lines
1.4 KiB
Go

package cryptopals_test
import (
"testing"
"mvvasilev.dev/cryptopals/set1"
)
const Challenge1HexValue = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
func Test_Challenge1(t *testing.T) {
const expected = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
value, success := set1.HexToBase64(Challenge1HexValue)
if !success || value != expected {
t.Fatal("Set 1, Challenge 1 has failed")
}
}
func Test_Challenge2(t *testing.T) {
const expected = "746865206b696420646f6e277420706c6179"
const content = "1c0111001f010100061a024b53535009181c"
const password = "686974207468652062756c6c277320657965"
value := set1.XORHexString(content, password)
if value != expected {
t.Fatal(("Set 1, Challenge 2 has failed"))
}
}
func Test_Challenge3(t *testing.T) {
const encodedString = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var passChar, mostCommonByte byte
var text string
for i := 0; i < len(letters); i++ {
letter := letters[i]
result := set1.XORDecode(encodedString, letter)
mostCommonLetter := set1.FindMostCommonByteInText(result)
if mostCommonLetter > mostCommonByte {
mostCommonByte = mostCommonLetter
text = result
passChar = letter
}
}
t.Logf("Result:\n\tEncoding char: %c\n\tMost common letter: %c\n\tText: %v", passChar, mostCommonByte, text)
}