Skip to content

Instantly share code, notes, and snippets.

@TerrorJack
Last active May 13, 2021 10:18
Show Gist options
  • Save TerrorJack/d6c79b33e5b5d0f5d52f3a2c5cdacc60 to your computer and use it in GitHub Desktop.
Save TerrorJack/d6c79b33e5b5d0f5d52f3a2c5cdacc60 to your computer and use it in GitHub Desktop.
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