Created
April 16, 2020 08:57
-
-
Save saswata-dutta/327519ba029c633e4c04c4fa179fe044 to your computer and use it in GitHub Desktop.
secure data in java https://security.stackexchange.com/questions/74718/is-it-more-secure-to-overwrite-the-value-char-in-a-string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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