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

The following examples show how to use org.spongycastle.crypto.digests.RIPEMD160Digest#update() . 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 bitshares_wallet with MIT License 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: 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 4
Source File: Tools.java    From thundernetwork with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Hash.java secret.
 *
 * @param secret the secret
 * @return the string
 * @throws UnsupportedEncodingException the unsupported encoding exception
 * @throws NoSuchAlgorithmException     the no such algorithm exception
 */
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 thunder 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: 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 7
Source File: EOSWalletTest.java    From token-core-android with Apache License 2.0 6 votes vote down vote up
@Test
public void generatePrvPubKey() {

  byte[] prvWIF = Base58.decode(WIF);
  // have omitted the checksum verification
  prvWIF = Arrays.copyOfRange(prvWIF, 1, prvWIF.length - 4);

  // use the privateKey to calculate the compressed public key directly
  ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, prvWIF));
  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);
  String eosPK = "EOS" + Base58.encode(pubKeyData);
  Assert.assertEquals(PUBLIC_KEY, eosPK);
}
 
Example 8
Source File: types.java    From guarda-android-wallets 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 9
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 10
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 11
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 12
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 13
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 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: DecentWalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public static String publicKeyFromPrivateKey(String privateKey) {
  byte[] pubKey = null;
  try {
    pubKey = DumpedPrivateKey.fromBase58(privateKey).getPubKeyPoint().getEncoded(true);
  } catch (Exception e) {
    return null;
  }
  byte[] checksum = new byte[160 / 8];
  RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
  ripemd160Digest.update(pubKey, 0, pubKey.length);
  ripemd160Digest.doFinal(checksum, 0);
  checksum =  Arrays.copyOfRange(checksum, 0, 4);
  byte[] pubKeyChecksummed = Bytes.concat(pubKey, checksum);
  return "DCT" + Base58.encode(pubKeyChecksummed);
}
 
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;
    }
}