<commit_before>/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.bitcoin.crypto;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Ignore;
import org.junit.Test;

import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.crypto.BIP38PrivateKey.BadPassphraseException;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.params.TestNet3Params;

public class BIP38PrivateKeyTest {

    private static final MainNetParams MAINNET = MainNetParams.get();
    private static final TestNet3Params TESTNET = TestNet3Params.get();

    @Test
    public void bip38testvector_noCompression_noEcMultiply_test1() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MAINNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("TestingOneTwoThree");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_noCompression_noEcMultiply_test2() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MAINNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("Satoshi");
        assertEquals("5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi5", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    @Ignore("Test disabled because you cannot pass \\u0000 in Strings.")
    public void bip38testvector_noCompression_noEcMultiply_test3() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MAINNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("\u03d2\u0301\u0000\u00010400\u0001f4a9");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_compression_noEcMultiply_test1() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("TestingOneTwoThree");
        assertEquals("<KEY>vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_compression_noEcMultiply_test2() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("Satoshi");
        assertEquals("KwYgW8gcxj1JWJXhPSu4Fqwzfhp5Yfi42mdYmMa4XqK7NJxXUSK7", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_ecMultiply_noCompression_noLotAndSequence_test1() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("TestingOneTwoThree");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_ecMultiply_noCompression_noLotAndSequence_test2() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("Satoshi");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_ecMultiply_noCompression_lotAndSequence_test1() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("MOLON LABE");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_ecMultiply_noCompression_lotAndSequence_test2() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("ΜΟΛΩΝ ΛΑΒΕ");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bitcoinpaperwallet_testnet() throws Exception {
        // values taken from bitcoinpaperwallet.com
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(TESTNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("password");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(TESTNET)
                .toString());
    }

    @Test
    public void bitaddress_testnet() throws Exception {
        // values taken from bitaddress.org
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(TESTNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("password");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(TESTNET)
                .toString());
    }

    @Test(expected = BadPassphraseException.class)
    public void badPassphrase() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MAINNET,
                "<KEY>");
        encryptedKey.decrypt("BAD");
    }

    @Test
    public void testJavaSerialization() throws Exception {
        BIP38PrivateKey key = new BIP38PrivateKey(TESTNET, "<KEY>");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        new ObjectOutputStream(os).writeObject(key);
        BIP38PrivateKey keyCopy = (BIP38PrivateKey) new ObjectInputStream(new ByteArrayInputStream(os.toByteArray()))
                .readObject();
        assertEquals(key, keyCopy);
    }
}
<commit_msg>Fix reason why BIP38 test vector is failing, Java strings can contain null characters just fine.<commit_after>/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.bitcoin.crypto;

import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.crypto.BIP38PrivateKey.BadPassphraseException;
import com.google.bitcoin.params.MainNetParams;
import com.google.bitcoin.params.TestNet3Params;
import org.junit.Ignore;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.junit.Assert.assertEquals;

public class BIP38PrivateKeyTest {

    private static final MainNetParams MAINNET = MainNetParams.get();
    private static final TestNet3Params TESTNET = TestNet3Params.get();

    @Test
    public void bip38testvector_noCompression_noEcMultiply_test1() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MAINNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("TestingOneTwoThree");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_noCompression_noEcMultiply_test2() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MAINNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("Satoshi");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    @Ignore("Test disabled because this test vector appears to be broken in the spec, see discussion on bitcoin-development in July 2014")
    public void bip38testvector_noCompression_noEcMultiply_test3() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MAINNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("\u03d2\u0301\u0000\u00010400\u0001f4a9");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_compression_noEcMultiply_test1() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("TestingOneTwoThree");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_compression_noEcMultiply_test2() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("Satoshi");
        assertEquals("<KEY>USK7", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_ecMultiply_noCompression_noLotAndSequence_test1() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("TestingOneTwoThree");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_ecMultiply_noCompression_noLotAndSequence_test2() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("Satoshi");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_ecMultiply_noCompression_lotAndSequence_test1() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("MOLON LABE");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bip38testvector_ecMultiply_noCompression_lotAndSequence_test2() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MainNetParams.get(),
                "<KEY>");
        ECKey key = encryptedKey.decrypt("ΜΟΛΩΝ ΛΑΒΕ");
        assertEquals("5KMKKuUmAkiNbA3DazMQiLfDq47qs8MAEThm4yL8R2PhV1ov33D", key.getPrivateKeyEncoded(MAINNET)
                .toString());
    }

    @Test
    public void bitcoinpaperwallet_testnet() throws Exception {
        // values taken from bitcoinpaperwallet.com
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(TESTNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("password");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(TESTNET)
                .toString());
    }

    @Test
    public void bitaddress_testnet() throws Exception {
        // values taken from bitaddress.org
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(TESTNET,
                "<KEY>");
        ECKey key = encryptedKey.decrypt("password");
        assertEquals("<KEY>", key.getPrivateKeyEncoded(TESTNET)
                .toString());
    }

    @Test(expected = BadPassphraseException.class)
    public void badPassphrase() throws Exception {
        BIP38PrivateKey encryptedKey = new BIP38PrivateKey(MAINNET,
                "<KEY>");
        encryptedKey.decrypt("BAD");
    }

    @Test
    public void testJavaSerialization() throws Exception {
        BIP38PrivateKey key = new BIP38PrivateKey(TESTNET, "<KEY>");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        new ObjectOutputStream(os).writeObject(key);
        BIP38PrivateKey keyCopy = (BIP38PrivateKey) new ObjectInputStream(new ByteArrayInputStream(os.toByteArray()))
                .readObject();
        assertEquals(key, keyCopy);
    }
}

{'fraction_non_alphanumeric': 0.09478330658105939, 'fraction_numerical': 0.02335473515248796, 'mean_word_length': 3.191389169189371, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4231082', 'n_tokens_mistral': 3787, 'n_tokens_neox': 3497, 'n_words': 761}