mnemonic终结篇

废话不多述,封装完毕上代码

// It would take more than 10^12 years to brute-force a 128 bit seed using $1B worth of computing equipment.
    public static final int DEFAULT_SEED_ENTROPY_BITS = 128;
    public static final int DEFAULT_SEED_ENTROPY_BITS_15 = 160;

 public synchronized boolean generateRandomSeed(final Context ctx, int size, String passphrase) {
        String languageCode = Locale.getDefault().getLanguage();
        Log.d("chendy", "default languageCode:" + languageCode);//zh
        List wordList = Bip39Reader.getBip39Words(ctx, "zh-Hans");
        if (wordList.size() != Bip39Reader.WORD_LIST_SIZE) {
            BRReportsManager.reportBug(new IllegalArgumentException("the list is wrong, size: " + wordList.size()), true);
            return false;
        }

        int entropyBits=DEFAULT_SEED_ENTROPY_BITS;
        if(size==Bip39Reader.WORD_DIGIT_CAPACITY_15){
            entropyBits=DEFAULT_SEED_ENTROPY_BITS_15;
        }
        SecureRandom sr = new SecureRandom();
        byte[] randomSeed = sr.generateSeed(size);
        if (randomSeed.length != size)
            throw new NullPointerException("failed to create the seed, seed length is not " +entropyBits+":"+ randomSeed.length);

        List mnemonicCode ;
        if(false){
            DeterministicSeed seed = new DeterministicSeed(sr,entropyBits,passphrase);
            mnemonicCode = seed.getMnemonicCode();
            Log.d("chendy", "a MnemonicCode:" + mnemonicCode);//zh
        }else{
            //或者用这个接口,复用前面的只产生了9个单词
            randomSeed = BRCoreMasterPubKey.getEntropy(new SecureRandom(),entropyBits);
            try {
                mnemonicCode = BRCoreMasterPubKey.toMnemonic(wordList,randomSeed);
                Log.d("chendy", "b MnemonicCode" );//zh
            } catch (MnemonicException.MnemonicLengthException e) {
                // cannot happen
                throw new RuntimeException(e);
            }
        }
        String mnemonic = org.bitcoinj.core.Utils.SPACE_JOINER.join(mnemonicCode);
        Log.d("chendy", size+"位:" + mnemonic);//zh
        if (mnemonicCode == null || mnemonicCode.size() == 0) {
            BRReportsManager.reportBug(new NullPointerException("failed to encodeSeed"), true);
            return false;
        }
        if (mnemonicCode.size() != size) {
            BRReportsManager.reportBug(new NullPointerException("phrase does not have " +size+ " words:" +mnemonicCode.size() + ", lang: " + languageCode), true);
            return false;
        }
        return true;
    }

 /**
     * Convert entropy data to mnemonic word list.
     */
    public static List toMnemonic(List wordList, byte[] entropy) throws MnemonicException.MnemonicLengthException {
        if (entropy.length % 4 > 0)
            throw new MnemonicException.MnemonicLengthException("Entropy length not multiple of 32 bits.");

        if (entropy.length == 0)
            throw new MnemonicException.MnemonicLengthException("Entropy is empty.");

        // We take initial entropy of ENT bits and compute its
        // checksum by taking first ENT / 32 bits of its SHA256 hash.

        byte[] hash = Sha256Hash.hash(entropy);
        boolean[] hashBits = bytesToBits(hash);

        boolean[] entropyBits = bytesToBits(entropy);
        int checksumLengthBits = entropyBits.length / 32;
        Log.d("chendy","checksumLengthBits:"+checksumLengthBits);
        // We append these bits to the end of the initial entropy.
        boolean[] concatBits = new boolean[entropyBits.length + checksumLengthBits];
        System.arraycopy(entropyBits, 0, concatBits, 0, entropyBits.length);
        System.arraycopy(hashBits, 0, concatBits, entropyBits.length, checksumLengthBits);

        // Next we take these concatenated bits and split them into
        // groups of 11 bits. Each group encodes number from 0-2047
        // which is a position in a wordlist.  We convert numbers into
        // words and use joined words as mnemonic sentence.

        ArrayList words = new ArrayList<>();
        int nwords = concatBits.length / 11;
        Log.d("chendy","nwords:"+nwords);
        for (int i = 0; i < nwords; ++i) {
            int index = 0;
            for (int j = 0; j < 11; ++j) {
                index <<= 1;
                if (concatBits[(i * 11) + j])
                    index |= 0x1;
            }
            words.add(wordList.get(index));
        }

        return words;
    }

    private static boolean[] bytesToBits(byte[] data) {
        boolean[] bits = new boolean[data.length * 8];
        for (int i = 0; i < data.length; ++i)
            for (int j = 0; j < 8; ++j)
                bits[(i * 8) + j] = (data[i] & (1 << (7 - j))) != 0;
        return bits;
    }

    public static byte[] getEntropy(SecureRandom random, int bits) {
        checkArgument(bits <= MAX_SEED_ENTROPY_BITS, "requested entropy size too large");

        //先产生一个16位随机字节数-->
        byte[] seed = new byte[bits / 8];
        random.nextBytes(seed);
        return seed;
    }

你可能感兴趣的:(mnemonic终结篇)