Android Open Source - bitcoin-wallet Hash Tools






From Project

Back to project page bitcoin-wallet.

License

The source code is released under:

Copyright (C) 2011 by Caleb Anderson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the ...

If you think the Android project bitcoin-wallet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.dirtyfilthy.bitcoin.util;
// ww w.ja va  2 s . c om
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import net.dirtyfilthy.bouncycastle.crypto.digests.RIPEMD160Digest;
import net.dirtyfilthy.bouncycastle.jce.provider.JDKMessageDigest;

public class HashTools {
  public static MessageDigest sha256digester;
  public static MessageDigest sha1digester;
  public static MessageDigest ripemd160digester;
  

  public static byte[] sha256(byte[] toHash){
    try {
      if(sha256digester==null){
        sha256digester = MessageDigest.getInstance("SHA256");
      }
    } catch (NoSuchAlgorithmException e) {

      try {
        if(sha256digester==null){
          sha256digester = MessageDigest.getInstance("SHA-256");
        }
      } catch (NoSuchAlgorithmException e1) {
        throw new RuntimeException("Can't find SHA-256 message digest algorithm",e1);
      }
    }
    return sha256digester.digest(toHash);
  }

  public static byte[] sha1(byte[] toHash){
    try {
      if(sha1digester==null){
        sha1digester = MessageDigest.getInstance("SHA1");
      }
    } catch (NoSuchAlgorithmException e) {

      try {
        if(sha1digester==null){
          sha1digester = MessageDigest.getInstance("SHA-1");
        }
      } catch (NoSuchAlgorithmException e1) {
        throw new RuntimeException("Can't find SHA-1 message digest algorithm",e1);
      }
    }
    return sha1digester.digest(toHash);
  }

  public static byte[] doubleSha256(byte[] toHash){
    return sha256(sha256(toHash));
  }

  public static byte[] hash160(byte[] toHash){
    return ripemd160(sha256(toHash));
  }

  public static byte[] ripemd160(byte[] toHash){
    if(ripemd160digester==null){  
      ripemd160digester = new  JDKMessageDigest.RIPEMD160();
    }
    return ripemd160digester.digest(toHash);
  }

  public static byte[] reverseByteArray(byte[] a) {
    byte[] b=a.clone();
    int left  = 0;          // index of leftmost element
    int right = b.length-1; // index of rightmost element

    while (left < right) {
      // exchange the left and right elements, the old xor byte swap
      b[left]  = (byte) (b[left] ^ b[right]); 
      b[right] = (byte) (b[left] ^ b[right]); 
      b[left]  = (byte) (b[left] ^ b[right]); 

      // move the bounds toward the center
      left++;
      right--;
    }
    return b;
  }

}




Java Source Code List

net.dirtyfilthy.bitcoin.core.Address.java
net.dirtyfilthy.bitcoin.core.Base58Hash160.java
net.dirtyfilthy.bitcoin.core.BlockChain.java
net.dirtyfilthy.bitcoin.core.BlockExistsException.java
net.dirtyfilthy.bitcoin.core.BlockStore.java
net.dirtyfilthy.bitcoin.core.Block.java
net.dirtyfilthy.bitcoin.core.BtcValue.java
net.dirtyfilthy.bitcoin.core.ByteArrayable.java
net.dirtyfilthy.bitcoin.core.InvalidBlockException.java
net.dirtyfilthy.bitcoin.core.OpCode.java
net.dirtyfilthy.bitcoin.core.OpData.java
net.dirtyfilthy.bitcoin.core.OrphanBlockException.java
net.dirtyfilthy.bitcoin.core.Script.java
net.dirtyfilthy.bitcoin.core.TxIn.java
net.dirtyfilthy.bitcoin.core.TxOut.java
net.dirtyfilthy.bitcoin.core.Tx.java
net.dirtyfilthy.bitcoin.protocol.AddressBook.java
net.dirtyfilthy.bitcoin.protocol.AddressPacket.java
net.dirtyfilthy.bitcoin.protocol.BlockPacket.java
net.dirtyfilthy.bitcoin.protocol.ConnectionHandler.java
net.dirtyfilthy.bitcoin.protocol.Connection.java
net.dirtyfilthy.bitcoin.protocol.GetAddressPacket.java
net.dirtyfilthy.bitcoin.protocol.GetBlocksPacket.java
net.dirtyfilthy.bitcoin.protocol.GetDataPacket.java
net.dirtyfilthy.bitcoin.protocol.GetHeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.HeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryVector.java
net.dirtyfilthy.bitcoin.protocol.IrcBootStrap.java
net.dirtyfilthy.bitcoin.protocol.MalformedPacketException.java
net.dirtyfilthy.bitcoin.protocol.PacketFactory.java
net.dirtyfilthy.bitcoin.protocol.PacketType.java
net.dirtyfilthy.bitcoin.protocol.Packet.java
net.dirtyfilthy.bitcoin.protocol.PingPacket.java
net.dirtyfilthy.bitcoin.protocol.ProtocolVersion.java
net.dirtyfilthy.bitcoin.protocol.ReplyPacket.java
net.dirtyfilthy.bitcoin.protocol.TxPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionAckPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionPacket.java
net.dirtyfilthy.bitcoin.util.Base58.java
net.dirtyfilthy.bitcoin.util.BigIntegerTools.java
net.dirtyfilthy.bitcoin.util.HashTools.java
net.dirtyfilthy.bitcoin.util.KeyTools.java
net.dirtyfilthy.bitcoin.util.MyHex.java
net.dirtyfilthy.bitcoin.wallet.ExposedSQLiteCursor.java
net.dirtyfilthy.bitcoin.wallet.InvalidPasswordException.java
net.dirtyfilthy.bitcoin.wallet.KeyRing.java
net.dirtyfilthy.bitcoin.wallet.SqlBlockStore.java
net.dirtyfilthy.bitcoin.wallet.Wallet.java