Java Code Examples for org.spongycastle.crypto.digests.RIPEMD160Digest#doFinal()

The following examples show how to use org.spongycastle.crypto.digests.RIPEMD160Digest#doFinal() . 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: EOSSign.java    From token-core-android with Apache License 2.0 7 votes vote down vote up
private static String serialEOSSignature(byte[] data) {
  byte[] toHash = ByteUtil.concat(data, "K1".getBytes());
  RIPEMD160Digest digest = new RIPEMD160Digest();
  digest.update(toHash, 0, toHash.length);
  byte[] out = new byte[20];
  digest.doFinal(out, 0);
  byte[] checksumBytes = Arrays.copyOfRange(out, 0, 4);
  data = ByteUtil.concat(data, checksumBytes);
  return "SIG_K1_" + Base58.encode(data);
}
 
Example 2
Source File: types.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
@Override
public String toString() {
    RIPEMD160Digest dig = new RIPEMD160Digest();
    dig.update(key_data, 0, key_data.length);
    byte[] out = new byte[20];
    dig.doFinal(out, 0);

    byte[] byteKeyData = new byte[37];
    System.arraycopy(key_data, 0, byteKeyData, 0, key_data.length);
    System.arraycopy(out, 0, byteKeyData, key_data.length, byteKeyData.length - key_data.length);

    String strResult = GRAPHENE_ADDRESS_PREFIX;
    strResult += Base58.encode(byteKeyData);

    return strResult;
}
 
Example 3
Source File: types.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
public public_key_type(String strBase58) throws NoSuchAlgorithmException {
    String strPrefix = GRAPHENE_ADDRESS_PREFIX;
    byte[] byteKeyData = Base58.decode(strBase58.substring(strPrefix.length()));
    binary_key binaryKey = new binary_key(byteKeyData);

    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(binaryKey.data, 0, binaryKey.data.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);

    byte[] byteOut = new byte[4];
    System.arraycopy(out, 0, byteOut, 0, byteOut.length);
    int nByteOut = ByteBuffer.wrap(byteOut).getInt();

    if (nByteOut != binaryKey.check) {
        throw new RuntimeException("Public key is not valid");
    }
    key_data = binaryKey.data;
}
 
Example 4
Source File: Tools.java    From thunder with GNU Affero General Public License v3.0 6 votes vote down vote up
public static String hashSecretToString (byte[] secret) {

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");

            md.update(secret);
            byte[] digest = md.digest();

            RIPEMD160Digest dig = new RIPEMD160Digest();
            dig.update(digest, 0, digest.length);

            byte[] out = new byte[20];
            dig.doFinal(out, 0);

            return Tools.byteToString(out);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
 
Example 5
Source File: Tools.java    From thundernetwork with GNU Affero General Public License v3.0 6 votes vote down vote up
public static byte[] hashSecret (byte[] secret) {

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");

            md.update(secret);
            byte[] digest = md.digest();

            RIPEMD160Digest dig = new RIPEMD160Digest();
            dig.update(digest, 0, digest.length);

            byte[] out = new byte[20];
            dig.doFinal(out, 0);

            return out;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
 
Example 6
Source File: types.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
public public_key_type(String strBase58) throws NoSuchAlgorithmException {
    String strPrefix = GRAPHENE_ADDRESS_PREFIX;
    byte[] byteKeyData = Base58.decode(strBase58.substring(strPrefix.length()));
    binary_key binaryKey = new binary_key(byteKeyData);

    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(binaryKey.data, 0, binaryKey.data.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);

    byte[] byteOut = new byte[4];
    System.arraycopy(out, 0, byteOut, 0, byteOut.length);
    int nByteOut = ByteBuffer.wrap(byteOut).getInt();

    if (nByteOut != binaryKey.check) {
        throw new RuntimeException("Public key is not valid");
    }
    key_data = binaryKey.data;
}
 
Example 7
Source File: types.java    From bitshares_wallet with MIT License 6 votes vote down vote up
public public_key_type(String strBase58) throws NoSuchAlgorithmException {
    String strPrefix = GRAPHENE_ADDRESS_PREFIX;
    byte[] byteKeyData = Base58.decode(strBase58.substring(strPrefix.length()));
    binary_key binaryKey = new binary_key(byteKeyData);

    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(binaryKey.data, 0, binaryKey.data.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);

    byte[] byteOut = new byte[4];
    System.arraycopy(out, 0, byteOut, 0, byteOut.length);
    int nByteOut = ByteBuffer.wrap(byteOut).getInt();

    if (nByteOut != binaryKey.check) {
        throw new RuntimeException("Public key is not valid");
    }
    key_data = binaryKey.data;
}
 
Example 8
Source File: Utils.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
 */
public static byte[] sha256hash160(byte[] input) {
    byte[] sha256 = Sha256Hash.hash(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 9
Source File: SerializeUtils.java    From nuls with MIT License 5 votes vote down vote up
public static byte[] sha256hash160(byte[] input) {
    byte[] sha256 = Sha256Hash.hash(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 10
Source File: HashUtils.java    From gerbera with MIT License 5 votes vote down vote up
public static byte[] ripemd160(byte[] bytes) {
    RIPEMD160Digest d = new RIPEMD160Digest();
    d.update (bytes, 0, bytes.length);

    byte[] result = new byte[d.getDigestSize()];
    d.doFinal(result, 0);

    return result;
}
 
Example 11
Source File: Utils.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
 */
public static byte[] sha256hash160(byte[] input) {
    byte[] sha256 = Sha256Hash.hash(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 12
Source File: RIPEMD160.java    From BlockchainWallet-Crypto with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] ripemd160(byte[] bytes) {
    RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
    ripemd160Digest.update(bytes, 0, bytes.length);
    byte[] hash160 = new byte[RIPEMD160_DIGEST_LENGTH];
    ripemd160Digest.doFinal(hash160, 0);
    return hash160;
}
 
Example 13
Source File: Utils.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
 */
public static byte[] sha256hash160(byte[] input) {
    byte[] sha256 = Sha256Hash.hash(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 14
Source File: ZCashWalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public static String publicKeyFromPrivateKey_taddr(String privKey) throws ZCashException {
    String methodName = "publicKeyFromPrivateKey_taddr";
    checkArgumentNonNull(privKey, "privKey", methodName);
    try {
      Base58.decodeChecked(privKey);
    } catch (IllegalArgumentException e) {
      throw new ZCashException("Invalid private key.");
    }

    ECKey key = DumpedPrivateKey.fromBase58(privKey);
    byte[] pubKey = key.getPubKeyPoint().getEncoded(key.isCompressed());

    pubKey = Sha256Hash.hash(pubKey);

    byte[] pubKeyHash = new byte[20];
    RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
    ripemd160Digest.update(pubKey, 0, pubKey.length);
    ripemd160Digest.doFinal(pubKeyHash, 0);

    pubKey = Bytes.concat(ZEC_MAINNET_ADDR_PREFIX, pubKeyHash);
//    pubKey = Bytes.concat(ZEC_TESTNET_ADDR_PREFIX, pubKeyHash);
    //                               ^~~~~~~~~~~~~~~~~~~~~~~~ mainnet prefix

    byte[] checksum = Sha256Hash.hashTwice(pubKey);
    byte[] summed = Bytes.concat(pubKey, new byte[]{checksum[0], checksum[1], checksum[2], checksum[3]});

    return Base58.encode(summed);
  }
 
Example 15
Source File: Utils.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] calculateChecksum(byte[] data) {
  byte[] checksum = new byte[160 / 8];
  RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
  ripemd160Digest.update(data, 0, data.length);
  ripemd160Digest.doFinal(checksum, 0);
  return Arrays.copyOfRange(checksum, 0, 4);
}
 
Example 16
Source File: ZCashWalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public static String publicKeyFromPrivateKey_taddr(String privKey) throws ZCashException {
    String methodName = "publicKeyFromPrivateKey_taddr";
    checkArgumentNonNull(privKey, "privKey", methodName);
    try {
      Base58.decodeChecked(privKey);
    } catch (IllegalArgumentException e) {
      throw new ZCashException("Invalid private key.");
    }

    ECKey key = DumpedPrivateKey.fromBase58(privKey);
    byte[] pubKey = key.getPubKeyPoint().getEncoded(key.isCompressed());

    pubKey = Sha256Hash.hash(pubKey);

    byte[] pubKeyHash = new byte[20];
    RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
    ripemd160Digest.update(pubKey, 0, pubKey.length);
    ripemd160Digest.doFinal(pubKeyHash, 0);

    pubKey = Bytes.concat(ZEC_MAINNET_ADDR_PREFIX, pubKeyHash);
//    pubKey = Bytes.concat(ZEC_TESTNET_ADDR_PREFIX, pubKeyHash);
    //                               ^~~~~~~~~~~~~~~~~~~~~~~~ mainnet prefix

    byte[] checksum = Sha256Hash.hashTwice(pubKey);
    byte[] summed = Bytes.concat(pubKey, new byte[]{checksum[0], checksum[1], checksum[2], checksum[3]});

    return Base58.encode(summed);
  }
 
Example 17
Source File: Hash.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Calculates RIPEMD160(data).
 */
public static byte[] getRipemd160hash(byte[] data) {
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(data, 0, data.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 18
Source File: EOSKey.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
public String getPublicKeyAsHex() {
  ECKey ecKey = ECKey.fromPrivate(bytes);
  byte[] pubKeyData = ecKey.getPubKey();
  RIPEMD160Digest digest = new RIPEMD160Digest();
  digest.update(pubKeyData, 0, pubKeyData.length);
  byte[] out = new byte[20];
  digest.doFinal(out, 0);
  byte[] checksumBytes = Arrays.copyOfRange(out, 0, 4);

  pubKeyData = ByteUtil.concat(pubKeyData, checksumBytes);
  return "EOS" + Base58.encode(pubKeyData);
}
 
Example 19
Source File: EncryptUtils.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
 */
public static byte[] sha256hash160(byte[] input) {

    byte[] sha256 = computeSHA256(input);
    RIPEMD160Digest digest = new RIPEMD160Digest();
    digest.update(sha256, 0, sha256.length);
    byte[] out = new byte[20];
    digest.doFinal(out, 0);
    return out;
}
 
Example 20
Source File: MultiChainAddressGenerator.java    From polling-station-app with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Converts a given public key to a valid MultiChain address.
 * See {@link <a href="http://www.multichain.com/developers/address-key-format/">MultiChain Documentation</a>}
 * @param pubKey byte array containing the public key
 * @return String representing the corresponding address.
 */
public static String getPublicAddress(String[] version, String addressChecksum, byte[] pubKey) {
    //Step 3
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-256");
        digest.reset();
        byte[] hash = digest.digest(pubKey);

        //Step 4
        RIPEMD160Digest ripemd = new RIPEMD160Digest();
        ripemd.update(hash, 0, hash.length);
        byte[] out = new byte[20];
        ripemd.doFinal(out, 0);
        String hashStr = Util.byteArrayToHexString(out);

        //Step 5
        String step5 = "";
        if (BuildConfig.DEBUG && version.length != 4) throw new AssertionError("Version length != 4");
        for (int i = 0; i < 4; i++) { //Assumes version.length == 4
            step5 += version[i] + hashStr.substring((i*10),(i*10)+10);
        }
        digest.reset();

        //Step 6
        byte[] step6 = digest.digest(Util.hexStringToByteArray(step5));
        digest.reset();

        //Step 7
        byte[] step7 = digest.digest(step6);
        digest.reset();

        //Step 8
        byte[] checksum = new byte[]{ step7[0],step7[1],step7[2],step7[3] };

        //Step 9
        byte[] byteAddressChecksum = Util.hexStringToByteArray(addressChecksum);
        byte[] xor = new byte[4];
        for (int i = 0; i < 4; i++) {
            int xorvalue = (int)checksum[i] ^ (int)byteAddressChecksum[i];
            xor[i] = (byte)(0xff & xorvalue);
        }

        //Step 10
        String addressbytes = step5 + Util.byteArrayToHexString(xor);

        //Step 11
        String address = Base58.encode(Util.hexStringToByteArray(addressbytes));
        return address;
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
        return null;
    }
}