Skip to content

Instantly share code, notes, and snippets.

@TerrorJack
Last active May 13, 2021 10:18
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
import java.util.zip.CRC32;
public class AddressValidator {
public static byte[] base16Decode(String s) {
byte[] buf = new byte[s.length() / 2];
for (int i = 0; i < s.length() / 2; ++i) {
byte hi = Byte.parseByte(s.substring(i * 2, (i * 2) + 1), 16);
byte lo = Byte.parseByte(s.substring((i * 2) + 1, (i * 2) + 2), 16);
buf[i] = (byte)((hi << 4) | lo);
}
return buf;
}
public static boolean addressValidate(String addr) {
byte[] buf = AddressValidator.base16Decode(addr);
CRC32 hasher = new CRC32();
hasher.update(buf, 4, buf.length - 4);
long hash = hasher.getValue();
return hash == ((Byte.toUnsignedLong(buf[0]) << 24) | (Byte.toUnsignedLong(buf[1]) << 16) | (Byte.toUnsignedLong(buf[2]) << 8) | Byte.toUnsignedLong(buf[3]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment