StrKey { protected static byte[] decodeCheck(VersionByte versionByte, char[] encoded) { byte[] bytes = new byte[encoded.length]; for (int i = 0; i < encoded.length; i++) { if (encoded[i] > 127) { throw new IllegalArgumentException("Illegal characters in encoded char array."); } bytes[i] = (byte) encoded[i]; } Base32 base32Codec = new Base32(); byte[] decoded = base32Codec.decode(bytes); byte decodedVersionByte = decoded[0]; byte[] payload = Arrays.copyOfRange(decoded, 0, decoded.length - 2); byte[] data = Arrays.copyOfRange(payload, 1, payload.length); byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 2, decoded.length); if (decodedVersionByte != versionByte.getValue()) { throw new FormatException("Version byte is invalid"); } byte[] expectedChecksum = StrKey.calculateChecksum(payload); if (!Arrays.equals(expectedChecksum, checksum)) { throw new FormatException("Checksum invalid"); } if (VersionByte.SEED.getValue() == decodedVersionByte) { Arrays.fill(bytes, (byte) 0); Arrays.fill(decoded, (byte) 0); Arrays.fill(payload, (byte) 0); } return data; }  static String encodeStellarAccountId(byte[] data); static byte[] decodeStellarAccountId(String data); static char[] encodeStellarSecretSeed(byte[] data); static byte[] decodeStellarSecretSeed(char[] data); static String encodePreAuthTx(byte[] data); static byte[] decodePreAuthTx(String data); static String encodeSha256Hash(byte[] data); static byte[] decodeSha256Hash(String data);  }
@Test() public void testDecodeInvalidVersionByte() { String address = "GCZHXL5HXQX5ABDM26LHYRCQZ5OJFHLOPLZX47WEBP3V2PF5AVFK2A5D"; try { StrKey.decodeCheck(StrKey.VersionByte.SEED, address.toCharArray()); fail(); } catch (FormatException e) { } }
@Test() public void testDecodeInvalidSeed() { String seed = "SAA6NXOBOXP3RXGAXBW6PGFI5BPK4ODVAWITS4VDOMN5C2M4B66ZML"; try { StrKey.decodeCheck(StrKey.VersionByte.SEED, seed.toCharArray()); fail(); } catch (FormatException e) { } }