Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelliao committed May 1, 2017
1 parent 002db1d commit 0272397
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

public class BytesUtils {

/**
* Is byte array ALL zeros?
*/
public static boolean isZeros(byte[] bs) {
for (byte b : bs) {
if (b != 0) {
Expand All @@ -13,6 +16,9 @@ public static boolean isZeros(byte[] bs) {
return true;
}

/**
* Join two byte arrays to a new byte array.
*/
public static byte[] concat(byte[] buf1, byte[] buf2) {
byte[] buffer = new byte[buf1.length + buf2.length];
int offset = 0;
Expand All @@ -22,6 +28,9 @@ public static byte[] concat(byte[] buf1, byte[] buf2) {
return buffer;
}

/**
* Join three byte arrays to a new byte array.
*/
public static byte[] concat(byte[] buf1, byte[] buf2, byte[] buf3) {
byte[] buffer = new byte[buf1.length + buf2.length + buf3.length];
int offset = 0;
Expand All @@ -33,6 +42,9 @@ public static byte[] concat(byte[] buf1, byte[] buf2, byte[] buf3) {
return buffer;
}

/**
* Is array equals? (length equals and every byte equals)
*/
public static boolean equals(byte[] b1, byte[] b2) {
if (b1 == null || b2 == null) {
throw new IllegalArgumentException("one of the arguments is null");
Expand All @@ -48,6 +60,9 @@ public static boolean equals(byte[] b1, byte[] b2) {
return true;
}

/**
* Reverse the byte array. Return new reversed array.
*/
public static byte[] reverse(byte[] msgHash) {
return Arrays.reverse(msgHash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

public class ClasspathUtils {

/**
* Load classpath resource as byte array.
*/
public static byte[] loadAsBytes(String classpath) throws IOException {
try (InputStream input = ClasspathUtils.class.getResourceAsStream(classpath)) {
if (input == null) {
Expand All @@ -27,8 +30,6 @@ public static byte[] loadAsBytes(String classpath) throws IOException {

/**
* Get classes under package.
*
* @throws IOException
*/
public static List<Class<?>> getClasses(String packageName) throws IOException {
return ClassPath.from(ClasspathUtils.class.getClassLoader()).getTopLevelClasses(packageName).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@

public class HashUtils {

/**
* Get RipeMD160 hash.
*/
public static byte[] ripeMd160(byte[] input) {
MessageDigest digest = new RIPEMD160.Digest();
digest.update(input);
return digest.digest();
}

/**
* Get SHA-256 hash.
*/
public static byte[] sha256(byte[] input) {
Digest d = new SHA256Digest();
d.update(input, 0, input.length);
Expand All @@ -30,13 +36,19 @@ public static byte[] sha256(byte[] input) {
return out;
}

/**
* Get double SHA-256 hash.
*/
public static byte[] doubleSha256(byte[] input) {
byte[] round1 = sha256(input);
return sha256(round1);
}

static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

/**
* Convert byte array to hex string.
*/
public static String toHexString(byte[] b) {
return toHexString(b, false);
}
Expand All @@ -55,6 +67,9 @@ public static String toHexString(byte[] b, boolean sep) {
return sb.toString().trim();
}

/**
* Convert byte array (little endian) to hex string.
*/
public static String toHexStringAsLittleEndian(byte[] b) {
StringBuilder sb = new StringBuilder(b.length << 2);
for (int i = b.length - 1; i >= 0; i--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import com.itranswarp.bitcoin.util.BytesUtils;
import com.itranswarp.bitcoin.util.HashUtils;

/**
* Bitcoin block data.
*
* @author Michael Liao
*/
public class Block {

public Header header;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.itranswarp.bitcoin.io.BitcoinInput;

/**
* inventory array.
* Inventory array.
*
* @author Michael Liao
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import com.itranswarp.bitcoin.io.BitcoinInput;
import com.itranswarp.bitcoin.serializer.TimestampSerializer;

/**
* Timestamp and network address.
*
* @author Michael Liao
*/
public class TimestampNetworkAddress {

/**
Expand Down

0 comments on commit 0272397

Please sign in to comment.