• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bernardladenthin / BitcoinAddressFinder / #339

04 Jul 2025 08:12PM UTC coverage: 66.867% (+0.6%) from 66.224%
#339

push

bernardladenthin
Add BIP39 and legacy PRNG support with config, docs, and tests

32 of 34 new or added lines in 4 files covered. (94.12%)

1332 of 1992 relevant lines covered (66.87%)

0.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/main/java/net/ladenthin/bitcoinaddressfinder/KeyProducerJavaRandom.java
1
// @formatter:off
2
/**
3
 * Copyright 2024 Bernard Ladenthin bernard.ladenthin@gmail.com
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *    http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
// @formatter:on
19
package net.ladenthin.bitcoinaddressfinder;
20

21
import java.math.BigInteger;
22
import java.security.NoSuchAlgorithmException;
23
import java.security.SecureRandom;
24
import java.util.Random;
25
import net.ladenthin.bitcoinaddressfinder.configuration.CKeyProducerJavaRandom;
26

27
public class KeyProducerJavaRandom extends KeyProducerJava {
28

29
    private final CKeyProducerJavaRandom cKeyProducerJavaRandom;
30
    private final KeyUtility keyUtility;
31
    
32
    /**
33
     * It is already thread local, no need for {@link java.util.concurrent.ThreadLocalRandom}.
34
     */
35
    private final Random random;
36
    private final BitHelper bitHelper;
37
    
38
    public KeyProducerJavaRandom(CKeyProducerJavaRandom cKeyProducerJavaRandom, KeyUtility keyUtility, BitHelper bitHelper) {
39
        super(cKeyProducerJavaRandom);
1✔
40
        this.cKeyProducerJavaRandom = cKeyProducerJavaRandom;
1✔
41
        this.keyUtility = keyUtility;
1✔
42
        this.bitHelper = bitHelper;
1✔
43
        
44
        switch (cKeyProducerJavaRandom.keyProducerJavaRandomInstance) {
1✔
45
            case SECURE_RANDOM:
46
                try {
47
                    random = SecureRandom.getInstanceStrong();
1✔
48
                } catch (NoSuchAlgorithmException e) {
×
49
                    throw new RuntimeException(e);
×
50
                }
1✔
51
                break;
52
            case RANDOM_CURRENT_TIME_MILLIS_SEED:
53
                random = new Random(System.currentTimeMillis());
1✔
54
                break;
1✔
55
            case RANDOM_CUSTOM_SEED:
56
                random = new Random();
1✔
57
                if (cKeyProducerJavaRandom.customSeed != null) {
1✔
58
                    random.setSeed(cKeyProducerJavaRandom.customSeed); // only if explicitly configured
1✔
59
                }
60
                break;
61
            case SHA1_PRNG:
62
                try {
63
                    random = SecureRandom.getInstance("SHA1PRNG");
1✔
64
                    
65
                    // To simulate bug: do NOT set a seed at all
66
                    if (cKeyProducerJavaRandom.customSeed != null) {
1✔
67
                        random.setSeed(cKeyProducerJavaRandom.customSeed); // only if explicitly configured
1✔
68
                    }
NEW
69
                } catch (NoSuchAlgorithmException e) {
×
NEW
70
                    throw new RuntimeException(e);
×
71
                }
1✔
72
                break;
73
            case BIP39_SEED:
74
                random = new BIP39KeyProducer(
1✔
75
                    cKeyProducerJavaRandom.mnemonic,
76
                    cKeyProducerJavaRandom.passphrase,
77
                    cKeyProducerJavaRandom.bip32Path,
78
                    cKeyProducerJavaRandom.getCreationTimeInstant()
1✔
79
                );
80
                break;
1✔
81
            default:
82
                throw new RuntimeException("Unknown keyProducerJavaRandomInstance: " + cKeyProducerJavaRandom.keyProducerJavaRandomInstance);
×
83
        }
84
    }
1✔
85
    
86
    @Override
87
    public BigInteger[] createSecrets(int overallWorkSize, boolean returnStartSecretOnly) throws NoMoreSecretsAvailableException {
88
        int length = returnStartSecretOnly ? 1 : overallWorkSize;
1✔
89
        BigInteger[] secrets = new BigInteger[length];
1✔
90
        for (int i = 0; i < secrets.length; i++) {
1✔
91
            secrets[i] = keyUtility.createSecret(cKeyProducerJavaRandom.privateKeyMaxNumBits, random);
1✔
92
        }
93
        return secrets;
1✔
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc