Skip to content

Instantly share code, notes, and snippets.

@vpereira
Created September 26, 2013 21:04
Show Gist options
  • Save vpereira/6720549 to your computer and use it in GitHub Desktop.
Save vpereira/6720549 to your computer and use it in GitHub Desktop.
endianess from scrypt
//data from here https://litecoin.info/Scrypt
import java.io.*;
import java.util.*;
import java.net.*;
import java.security.*;
import java.text.*;
import java.util.zip.*;
public class A {
public static String data = "0000000105e9a54b7f65b46864bc90f55d67cccd8b6404a02f5e064a6df69282adf6e2e5f7f953b0632b25b099858b717bb7b24084148cfa841a89f106bc6b655b18d2ed4ebb191a1d018ea700000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000";
public static String target = "0000000000000000000000000000000000000000000000000000a78e01000000";
private static byte[] targetBuf = new byte[128];
private static byte[] dataBuf = new byte[128];
private static byte[] targetBufLE = new byte[128];
private static byte[] dataBufLE = new byte[128];
public static byte[] endianSwitch(byte[] bytes) {
//Method to switch the endianess of a byte array
byte[] bytes2 = new byte[bytes.length];
for(int i = 0; i < bytes.length; i++){
bytes2[i] = bytes[bytes.length-i-1];
}
return bytes2;
}
public static String prettyPrintByteArray(byte[] bites){
//Method to convert a byte array to hex literal separated by bites.
String str = "";
int n = 0;
for(byte bite:bites){
n += 1;
str = str + (Integer.toString( ( bite & 0xff ) + 0x100, 16 /* radix */ ).substring( 1 ));
}
return str;
}
public static byte[] hexStrToData( String str ) throws NumberFormatException {
if ( str.length() % 2 != 0 )
throw new NumberFormatException("Invalid length of string");
byte[] buf = new byte[str.length() >> 1];
for ( int i=0; i<buf.length; i++) {
buf[i] = (byte) Integer.parseInt( str.substring(i*2,i*2+2), 16);
}
return buf;
}
public static byte[] chunkEndianSwitch(byte[] bytes) {
//Method to properly switch the endianness of the header -- numbers must be treated as 32 bit chunks. Thanks to ali1234 for this.
byte[] bytes2 = new byte[bytes.length];
for(int i = 0; i < bytes.length; i+=4){
bytes2[i] = bytes[i+3];
bytes2[i+1] = bytes[i+2];
bytes2[i+2] = bytes[i+1];
bytes2[i+3] = bytes[i];
}
return bytes2;
}
public static void main(String[] args) {
//System.out.println(prettyPrintByteArray(chunkEndianSwitch(hexStrToData(h1))));
System.out.println("Big Endian data " + data);
System.out.println("Big Endian target " + target);
targetBuf = hexStrToData(target);
dataBuf = hexStrToData(data);
targetBufLE = endianSwitch(targetBuf);
dataBufLE = chunkEndianSwitch(dataBuf);
System.out.print("Little Endian data ");
System.out.println(prettyPrintByteArray(dataBufLE));
System.out.print("Little Endian target ");
System.out.println(prettyPrintByteArray(targetBufLE));
}
}
/* TO COMPILE IT AND RUN:
* javac A.java
* jar cvf A.jar A.class
* java -cp A.jar A
*
*/
/* RESULTS
*
* Big Endian data 0000000105e9a54b7f65b46864bc90f55d67cccd8b6404a02f5e064a6df69282adf6e2e5f7f953b0632b25b099858b717bb7b24084148cfa841a89f106bc6b655b18d2ed4ebb191a1d018ea700000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000
* Big Endian target 0000000000000000000000000000000000000000000000000000a78e01000000
* Little Endian data 010000004ba5e90568b4657ff590bc64cdcc675da004648b4a065e2f8292f66de5e2f6adb053f9f7b0252b63718b859940b2b77bfa8c1484f1891a84656bbc06edd2185b1a19bb4ea78e011d00000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280
* Little Endian target 000000018ea70000000000000000000000000000000000000000000000000000
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment