Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Created April 16, 2020 08:57
Show Gist options
  • Save saswata-dutta/327519ba029c633e4c04c4fa179fe044 to your computer and use it in GitHub Desktop.
Save saswata-dutta/327519ba029c633e4c04c4fa179fe044 to your computer and use it in GitHub Desktop.
static void clear(String source) {
try {
Field charField = String.class.getDeclaredField("value");
charField.setAccessible(true);
SecureRandom random = SecureRandom.getInstanceStrong();
char[] noise = new char[source.length()];
for (int i = 0; i < source.length(); i++) {
noise[i] = (char) random.nextInt(Character.MAX_VALUE + 1);
}
charField.set(source, noise);
} catch (NoSuchAlgorithmException | NoSuchFieldException | IllegalAccessException ex) {
LOGGER.error("Failed to clear String", ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment