Java Code Examples for net.bither.bitherj.utils.Utils#doubleDigest()

The following examples show how to use net.bither.bitherj.utils.Utils#doubleDigest() . 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: ECKey.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public static ECKey signedMessageToKey(byte[] messageBytes, byte[] signatureEncoded) throws SignatureException {
    // Parse the signature bytes into r/s and the selector value.
    if (signatureEncoded.length < 65)
        throw new SignatureException("Signature truncated, expected 65 bytes and got " + signatureEncoded.length);
    int header = signatureEncoded[0] & 0xFF;
    // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
    //                  0x1D = second key with even y, 0x1E = second key with odd y
    if (header < 27 || header > 34)
        throw new SignatureException("Header byte out of range: " + header);
    BigInteger r = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 1, 33));
    BigInteger s = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 33, 65));
    ECDSASignature sig = new ECDSASignature(r, s);
    // Note that the C++ code doesn't actually seem to specify any character encoding. Presumably it's whatever
    // JSON-SPIRIT hands back. Assume UTF-8 for now.
    byte[] messageHash = Utils.doubleDigest(messageBytes);
    boolean compressed = false;
    if (header >= 31) {
        compressed = true;
        header -= 4;
    }
    int recId = header - 27;
    ECKey key = ECKey.recoverFromSignature(recId, sig, messageHash, compressed);
    if (key == null)
        throw new SignatureException("Could not recover public key from signature");
    return key;
}
 
Example 2
Source File: Block.java    From bitherj with Apache License 2.0 6 votes vote down vote up
private void parseHeader() throws ProtocolException {
    if (headerParsed)
        return;

    cursor = offset;
    blockVer = readUint32();
    blockPrev = readHash();
    blockRoot = readHash();
    blockTime = (int) readUint32();
    blockBits = readUint32();
    blockNonce = readUint32();

    blockHash = Utils.doubleDigest(bytes, offset, cursor);

    headerParsed = true;
    headerBytesValid = false;
}
 
Example 3
Source File: ECKey.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
     * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
     * encoded string.
     *
     * @throws IllegalStateException                         if this ECKey does not have the private part.
     * @throws net.bither.bitherj.crypto.KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
     */
    public String signMessage(String message, @Nullable KeyParameter aesKey) throws KeyCrypterException {
//        if (priv == null)
//            throw new IllegalStateException("This ECKey does not have the private key necessary for signing.");
        byte[] data = Utils.formatMessageForSigning(message);
        byte[] hash = Utils.doubleDigest(data);
        byte[] sigData = signHash(hash, aesKey);
        return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
    }
 
Example 4
Source File: DumpedPrivateKey.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public SecureCharSequence toSecureCharSequence() {
    byte[] addressBytes = new byte[1 + bytes.length + 4];
    addressBytes[0] = (byte) version;
    System.arraycopy(bytes, 0, addressBytes, 1, bytes.length);
    byte[] check = Utils.doubleDigest(addressBytes, 0, bytes.length + 1);
    System.arraycopy(check, 0, addressBytes, bytes.length + 1, 4);
    return Base58.encodeSecure(addressBytes);
}
 
Example 5
Source File: DeterministicKey.java    From bitherj with Apache License 2.0 5 votes vote down vote up
static byte[] addChecksum(byte[] input) {
    int inputLength = input.length;
    byte[] checksummed = new byte[inputLength + 4];
    System.arraycopy(input, 0, checksummed, 0, inputLength);
    byte[] checksum = Utils.doubleDigest(input);
    System.arraycopy(checksum, 0, checksummed, inputLength, 4);
    return checksummed;
}
 
Example 6
Source File: ScriptTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private static String toAddress(byte[] pubKeyHash, int version) {
    byte[] bytes = pubKeyHash;
    checkArgument(bytes.length == 20, "Addresses are 160-bit hashes, " +
            "so you must provide 20 bytes");

    checkArgument(version < 256 && version >= 0);

    byte[] addressBytes = new byte[1 + bytes.length + 4];
    addressBytes[0] = (byte) version;
    System.arraycopy(bytes, 0, addressBytes, 1, bytes.length);
    byte[] check = Utils.doubleDigest(addressBytes, 0, bytes.length + 1);
    System.arraycopy(check, 0, addressBytes, bytes.length + 1, 4);
    return Base58.encode(addressBytes);
}