Ejemplo n.º 1
0
//Test for Set 1 challenge 2
func TestXor(t *testing.T) {
	text1 := encoding.HexToBytes("1c0111001f010100061a024b53535009181c")
	text2 := encoding.HexToBytes("686974207468652062756c6c277320657965")

	expected_output := "746865206b696420646f6e277420706c6179"
	output := encoding.BytesToHex(Xor(text1, text2))
	if output != expected_output {
		t.Errorf("Xor: Expected output does not match actual output.")
	}
}
//Test for Set 1 challenge 4
func TestSingleByteXorScore(t *testing.T) {
	expected_plaintext := "Now that the party is jumping\n"

	file, err := os.Open("../files/4.txt")
	if err != nil {
		t.Errorf("SingleByteXorScore: Error opening 4.txt")
	}

	defer file.Close()

	scanner := bufio.NewScanner(file)

	var winner []byte
	for scanner.Scan() {
		line := encoding.HexToBytes(scanner.Text())
		if SingleByteXorScore(line) {
			//t.Log("Detected Single Byte XOR: ", line)
			winner = line
			continue
		}
		if SingleByteXorScore(line) && winner != nil {
			t.Errorf("SingleByteXorScore: Detected more than one possible single character xor ciphertext")
		}
	}
	if actual_plaintext, _ := BreakSingleCharXor(winner); string(actual_plaintext) != expected_plaintext {
		t.Errorf("SingleByteXorScore: Expected decrypted text does not match actual decrypted text.")
		t.Errorf("Expected: %v\n", expected_plaintext)
		t.Errorf("Actual:   %v\n", actual_plaintext)
	}

}
//Test for Set 1 challenge 3
func TestPlaintextScore(t *testing.T) {
	plaintext := "Cooking MC's like a pound of bacon"
	ciphertext := encoding.HexToBytes("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")
	maxPT, _ := BreakSingleCharXor(ciphertext)
	if string(maxPT) != plaintext {
		t.Errorf("PlaintextScore: Expected output does not match actual output.")
		t.Errorf("Expected: %v\n", plaintext)
		t.Errorf("Actual: %v\n", string(maxPT))
	}

}
//Test for Set 1 challenge 8
func TestDetectECBMode(t *testing.T) {
	file, err := os.Open("../files/8.txt")
	if err != nil {
		t.Errorf("DetectECBMode: Error opening file.")
	}

	defer file.Close()

	scanner := bufio.NewScanner(file)

	var detected bool
	for scanner.Scan() {
		line := encoding.HexToBytes(scanner.Text())
		if DetectECB256Mode(line) && detected {
			t.Errorf("DetectECBMode: Detected multiple lines.")
			detected = true
		}
	}

}