Beispiel #1
0
func main() {
	fmt.Println("My Cryptopals Challenge Results:")

	// Set 1 - Challenge 1
	fmt.Println("Set 1 - Challenge 1")
	expected := "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
	input := "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
	hex := util.Atoh(input)
	encoded := b64.Encode(hex)
	actual := util.B64toa(encoded)
	fmt.Printf("\tInput:\t\t%s\n\tResult:\t\t%s\n\tExpected:\t%s\n\tSuccess? %t\n", input, actual, expected, actual == expected)

	// Set 1 - Challenge 2
	fmt.Println("Set 1 - Challenge 2")
	input = "1c0111001f010100061a024b53535009181c"
	xorWith := "686974207468652062756c6c277320657965"
	expected = "746865206b696420646f6e277420706c6179"
	result := xor.FixedXor(util.Atoh(input), util.Atoh(xorWith))
	actual = util.Htoa(result)
	fmt.Printf("\tInput:\t\t%s\n\tResult:\t\t%s\n\tExpected:\t%s\n\tSuccess? %t\n", input, actual, expected, actual == expected)

	// Set 1 - Challenge 3
	fmt.Println("Set 1 - Challenge 3")
	input = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
	results := xor.CipherDecryption(util.Atoh(input), 0.25)
	for _, r := range results {
		fmt.Printf("\t%s\n", r)
	}
}
func TestChallenge2(t *testing.T) {
	input := "1c0111001f010100061a024b53535009181c"
	xorWith := "686974207468652062756c6c277320657965"
	expected := "746865206b696420646f6e277420706c6179"

	result := xor.FixedXor(util.Atoh(input), util.Atoh(xorWith))
	actual := util.Htoa(result)

	if actual != expected {
		fmt.Println("Expected:", expected, "Actual:", actual)
		t.Fail()
	}
}
func TestByte2(t *testing.T) {
	expected := []byte{0xaF}
	actual := util.Atoh("af")

	if expected[0] != actual[0] {
		fmt.Println("Expected:", expected, "Actual:", actual)
		t.Fail()
	}
}
func TestChar3(t *testing.T) {
	expected := []byte{0x02}
	actual := util.Atoh("2")

	if expected[0] != actual[0] {
		fmt.Println("Expected:", expected, "Actual:", actual)
		t.Fail()
	}
}
func TestLongWord2(t *testing.T) {
	expected := []byte{0x01, 0x23, 0x45, 0x67}
	actual := util.Atoh("1234567")

	for i, actualVal := range actual {
		if expected[i] != actualVal {
			fmt.Println("Expected:", expected, "Actual:", actual)
			t.FailNow()
		}
	}
}
func TestLongWord1(t *testing.T) {
	expected := []byte{0x12, 0x34, 0x56, 0x78}
	actual := util.Atoh("12345678")

	for i, actualVal := range actual {
		if expected[i] != actualVal {
			fmt.Println("Expected:", expected, "Actual:", actual)
			t.FailNow()
		}
	}
}
func TestWord2(t *testing.T) {
	expected := []byte{0x0F, 0x01}
	actual := util.Atoh("f01")

	for i, actualVal := range actual {
		if expected[i] != actualVal {
			fmt.Println("Expected:", expected, "Actual:", actual)
			t.FailNow()
		}
	}
}
func TestReallyLong(t *testing.T) {
	// This is the input from set 1 challenge 1
	expected := []byte{0x49, 0x27, 0x6d, 0x20, 0x6b, 0x69, 0x6c, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x62, 0x72, 0x61, 0x69, 0x6e, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, 0x20, 0x70, 0x6f, 0x69, 0x73, 0x6f, 0x6e, 0x6f, 0x75, 0x73, 0x20, 0x6d, 0x75, 0x73, 0x68, 0x72, 0x6f, 0x6f, 0x6d}
	actual := util.Atoh("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d")

	for i, actualVal := range actual {
		if expected[i] != actualVal {
			fmt.Println("Expected:", expected, "Actual:", actual)
			t.FailNow()
		}
	}
}
func TestMediumLengthHexInput(t *testing.T) {
	expected := "SSdtIGtp"
	input := "49276d206b69"

	hex := util.Atoh(input)
	encoded := b64.Encode(hex)
	actual := util.B64toa(encoded)
	if expected != actual {
		fmt.Println("Expected:", expected, "Actual:", actual)
		t.Fail()
	}
}
func TestChallenge1(t *testing.T) {
	expected := "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
	input := "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"

	hex := util.Atoh(input)
	encoded := b64.Encode(hex)
	actual := util.B64toa(encoded)
	if expected != actual {
		fmt.Println("Expected:", expected, "Actual:", actual)
		t.Fail()
	}
}