Java Code Examples for org.bitcoinj.core.Sha256Hash#hash()

The following examples show how to use org.bitcoinj.core.Sha256Hash#hash() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyChainGroupTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private MarriedKeyChain createMarriedKeyChain() {
    byte[] entropy = Sha256Hash.hash("don't use a seed like this in real life".getBytes());
    DeterministicSeed seed = new DeterministicSeed(entropy, "", MnemonicCode.BIP39_STANDARDISATION_TIME_SECS);
    MarriedKeyChain chain = MarriedKeyChain.builder()
            .seed(seed)
            .followingKeys(watchingAccountKey)
            .threshold(2).build();
    return chain;
}
 
Example 2
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void encryptDecryptWalletWithArbitraryPath() throws Exception {
    final byte[] ENTROPY = Sha256Hash.hash("don't use a string seed like this in real life".getBytes());
    KeyChainGroup keyChainGroup = new KeyChainGroup(UNITTEST,
            new DeterministicSeed(ENTROPY, "", 1389353062L),
            DeterministicKeyChain.BIP44_ACCOUNT_ZERO_PATH);
    Wallet encryptedWallet = new Wallet(UNITTEST, keyChainGroup);
    encryptedWallet.encrypt(PASSWORD1);
    encryptedWallet.decrypt(PASSWORD1);
}
 
Example 3
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private String seedToWif(String seed) {
    try {
        byte[] bytes = Sha256Hash.hash(seed.getBytes());
        bytes[0] &= 248;
        bytes[31] &= 127;
        bytes[31] |= 64;
        BigInteger bi = new BigInteger(Hex.toHexString(bytes), 16);
        ECKey testKey = ECKey.fromPrivate(bi);
        return testKey.getPrivateKeyAsWiF(params);
    } catch (Exception e) {
        Log.e("flintd", "seedToWif... exception: " + e.toString());
    }
    return null;
}
 
Example 4
Source File: PassportTransactionFormatter.java    From polling-station-app with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Signs the raw bytes using a travel document.
 * Follows the steps in this answer: https://bitcoin.stackexchange.com/a/5241
 * @return signedRawTransaction
 */
public byte[] signRawTransaction(PublicKey pubkey, byte[][] parts, PassportConnection pcon) throws Exception {
    byte[] rawTransaction = Bytes.concat(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5],
            parts[6], parts[7], parts[8], parts[9], parts[10], parts[11], parts[12]);

    // Double hash transaction
    byte[] step14 = Sha256Hash.hash(Sha256Hash.hash(rawTransaction));

    // Generate signature and get publickey
    byte[] multiSignature = new byte[320];
    byte[] hashPart;

    for (int i = 0; i < 4; i++) {
        hashPart = Arrays.copyOfRange(step14, i * 8, i * 8 + 8);
        System.arraycopy(pcon.signData(hashPart), 0, multiSignature, i * 80, 80);
    }

    byte[] signatureLength = Util.hexStringToByteArray("fd97014d4101");
    byte[] hashCodeType = Util.hexStringToByteArray("01");
    byte[] publicKeyASN = pubkey.getEncoded();

    byte[] publicKey = new byte[81];
    System.arraycopy(publicKeyASN, publicKeyASN.length-81, publicKey, 0, 81);

    byte[] publickeyLength = Util.hexStringToByteArray("4c51");

    // Set signature and pubkey in format
    byte[] step16 = Bytes.concat(signatureLength, multiSignature, hashCodeType, publickeyLength, publicKey);

    // Update transaction with signature and remove hash code type
    byte[] step19 = Bytes.concat(parts[0], parts[1], parts[2], parts[3], step16, parts[6],
            parts[7], parts[8], parts[9], parts[10], parts[11], parts[12]);

    return step19;
}
 
Example 5
Source File: PassportTransactionFormatterTest.java    From polling-station-app with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testBuildRawTransaction() {
    byte[][] transaction = ptf.buildRawTransaction();

    assertEquals(1, value(transaction[0]));
    assertEquals(1, value(transaction[1]));

    //check for number of outputs
    assertEquals(1, value(transaction[7]));

    //check for zero coins
    assertEquals(0, value(transaction[8]));

    //check length of script
    assertEquals(25, value(transaction[9]));


    // Create the raw transaction and hash it
    byte[] raw = Bytes.concat(transaction[0], transaction[1], transaction[2], transaction[3], transaction[4], transaction[5],
            transaction[6], transaction[7], transaction[8], transaction[9], transaction[10], transaction[11], transaction[12]);
    byte[] hashRaw = Sha256Hash.hash(Sha256Hash.hash(raw));


    //check for the correct hash
    byte[] correctHash = Util.hexStringToByteArray("09AB317A17BBEB4F46EFA2BDA80F137059608AA6696FF5155F0E2A72DC6C249E");
    for(int i=0; i< correctHash.length; i++) {
        assertEquals(correctHash[i], hashRaw[i]);
    }
}
 
Example 6
Source File: MnemonicCode.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convert mnemonic word list to original entropy value.
 */
public byte[] toEntropy(List<String> words) throws MnemonicException.MnemonicLengthException, MnemonicException.MnemonicWordException, MnemonicException.MnemonicChecksumException {
    if (words.size() % 3 > 0)
        throw new MnemonicException.MnemonicLengthException("Word list size must be multiple of three words.");

    if (words.size() == 0)
        throw new MnemonicException.MnemonicLengthException("Word list is empty.");

    // Look up all the words in the list and construct the
    // concatenation of the original entropy and the checksum.
    //
    int concatLenBits = words.size() * 11;
    boolean[] concatBits = new boolean[concatLenBits];
    int wordindex = 0;
    for (String word : words) {
        // Find the words index in the wordlist.
        int ndx = Collections.binarySearch(this.wordList, word);
        if (ndx < 0)
            throw new MnemonicException.MnemonicWordException(word);

        // Set the next 11 bits to the value of the index.
        for (int ii = 0; ii < 11; ++ii)
            concatBits[(wordindex * 11) + ii] = (ndx & (1 << (10 - ii))) != 0;
        ++wordindex;
    }

    int checksumLengthBits = concatLenBits / 33;
    int entropyLengthBits = concatLenBits - checksumLengthBits;

    // Extract original entropy as bytes.
    byte[] entropy = new byte[entropyLengthBits / 8];
    for (int ii = 0; ii < entropy.length; ++ii)
        for (int jj = 0; jj < 8; ++jj)
            if (concatBits[(ii * 8) + jj])
                entropy[ii] |= 1 << (7 - jj);

    // Take the digest of the entropy.
    byte[] hash = Sha256Hash.hash(entropy);
    boolean[] hashBits = bytesToBits(hash);

    // Check all the checksum bits.
    for (int i = 0; i < checksumLengthBits; ++i)
        if (concatBits[entropyLengthBits + i] != hashBits[i])
            throw new MnemonicException.MnemonicChecksumException();

    return entropy;
}
 
Example 7
Source File: MnemonicCode.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convert entropy data to mnemonic word list.
 */
public List<String> toMnemonic(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;

    // 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<String> words = new ArrayList<>();
    int nwords = concatBits.length / 11;
    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(this.wordList.get(index));
    }

    return words;
}
 
Example 8
Source File: PrivateKey.java    From evt4j with MIT License 4 votes vote down vote up
@NotNull
public static PrivateKey seedPrivateKey(@NotNull String seed) {
    byte[] hash = Sha256Hash.hash(seed.getBytes());
    ECKey key = ECKey.fromPrivate(hash);
    return new PrivateKey(key);
}
 
Example 9
Source File: Utils.java    From evt4j with MIT License 4 votes vote down vote up
public static byte[] hash(byte[] data) {
    return Sha256Hash.hash(data);
}
 
Example 10
Source File: MnemonicCode.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convert mnemonic word list to original entropy value.
 */
public byte[] toEntropy(List<String> words) throws MnemonicException.MnemonicLengthException, MnemonicException.MnemonicWordException, MnemonicException.MnemonicChecksumException {
    if (words.size() % 3 > 0)
        throw new MnemonicException.MnemonicLengthException("Word list size must be multiple of three words.");

    if (words.size() == 0)
        throw new MnemonicException.MnemonicLengthException("Word list is empty.");

    // Look up all the words in the list and construct the
    // concatenation of the original entropy and the checksum.
    //
    int concatLenBits = words.size() * 11;
    boolean[] concatBits = new boolean[concatLenBits];
    int wordindex = 0;
    for (String word : words) {
        // Find the words index in the wordlist.
        int ndx = Collections.binarySearch(this.wordList, word);
        if (ndx < 0)
            throw new MnemonicException.MnemonicWordException(word);

        // Set the next 11 bits to the value of the index.
        for (int ii = 0; ii < 11; ++ii)
            concatBits[(wordindex * 11) + ii] = (ndx & (1 << (10 - ii))) != 0;
        ++wordindex;
    }        

    int checksumLengthBits = concatLenBits / 33;
    int entropyLengthBits = concatLenBits - checksumLengthBits;

    // Extract original entropy as bytes.
    byte[] entropy = new byte[entropyLengthBits / 8];
    for (int ii = 0; ii < entropy.length; ++ii)
        for (int jj = 0; jj < 8; ++jj)
            if (concatBits[(ii * 8) + jj])
                entropy[ii] |= 1 << (7 - jj);

    // Take the digest of the entropy.
    byte[] hash = Sha256Hash.hash(entropy);
    boolean[] hashBits = bytesToBits(hash);

    // Check all the checksum bits.
    for (int i = 0; i < checksumLengthBits; ++i)
        if (concatBits[entropyLengthBits + i] != hashBits[i])
            throw new MnemonicException.MnemonicChecksumException();

    return entropy;
}
 
Example 11
Source File: MnemonicCode.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convert entropy data to mnemonic word list.
 */
public List<String> toMnemonic(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;

    // 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<String> words = new ArrayList<>();
    int nwords = concatBits.length / 11;
    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(this.wordList.get(index));
    }
        
    return words;        
}
 
Example 12
Source File: Hash.java    From balzac with Apache License 2.0 4 votes vote down vote up
public static Hash sha256(byte[] bytes) {
    return new Hash(Sha256Hash.hash(bytes));
}
 
Example 13
Source File: MnemonicCode.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convert mnemonic word list to original entropy value.
 */
public byte[] toEntropy(List<String> words) throws MnemonicException.MnemonicLengthException, MnemonicException.MnemonicWordException, MnemonicException.MnemonicChecksumException {
    if (words.size() % 3 > 0)
        throw new MnemonicException.MnemonicLengthException("Word list size must be multiple of three words.");

    if (words.size() == 0)
        throw new MnemonicException.MnemonicLengthException("Word list is empty.");

    // Look up all the words in the list and construct the
    // concatenation of the original entropy and the checksum.
    //
    int concatLenBits = words.size() * 11;
    boolean[] concatBits = new boolean[concatLenBits];
    int wordindex = 0;
    for (String word : words) {
        // Find the words index in the wordlist.
        int ndx = Collections.binarySearch(this.wordList, word);
        if (ndx < 0)
            throw new MnemonicException.MnemonicWordException(word);

        // Set the next 11 bits to the value of the index.
        for (int ii = 0; ii < 11; ++ii)
            concatBits[(wordindex * 11) + ii] = (ndx & (1 << (10 - ii))) != 0;
        ++wordindex;
    }        

    int checksumLengthBits = concatLenBits / 33;
    int entropyLengthBits = concatLenBits - checksumLengthBits;

    // Extract original entropy as bytes.
    byte[] entropy = new byte[entropyLengthBits / 8];
    for (int ii = 0; ii < entropy.length; ++ii)
        for (int jj = 0; jj < 8; ++jj)
            if (concatBits[(ii * 8) + jj])
                entropy[ii] |= 1 << (7 - jj);

    // Take the digest of the entropy.
    byte[] hash = Sha256Hash.hash(entropy);
    boolean[] hashBits = bytesToBits(hash);

    // Check all the checksum bits.
    for (int i = 0; i < checksumLengthBits; ++i)
        if (concatBits[entropyLengthBits + i] != hashBits[i])
            throw new MnemonicException.MnemonicChecksumException();

    return entropy;
}
 
Example 14
Source File: MnemonicCode.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convert entropy data to mnemonic word list.
 */
public List<String> toMnemonic(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;

    // 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<String> words = new ArrayList<>();
    int nwords = concatBits.length / 11;
    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(this.wordList.get(index));
    }
        
    return words;        
}
 
Example 15
Source File: ByteFormatter.java    From eosio-java with MIT License 2 votes vote down vote up
/**
 * Calculate the sha256 hash of the current ByteFormatter context and return it as a new
 * ByteFormatter.
 *
 * @return - New ByteFormatter containing the sha256 hash of the current one.
 */
public ByteFormatter sha256() {
    return new ByteFormatter(Sha256Hash.hash(this.context));
}