Menus

Nov 23, 2018

Why char array is preferred to store password than String in Java?





1) Strings are immutable: Strings are immutable in Java and therefore if a password is stored as plain text it will be available in memory until Garbage collector clears it and as Strings are used in String pool for re-usability there are high chances that it will remain in memory for long duration, which is a security threat. Strings are immutable and there is no way that the content of Strings can be changed because any change will produce new String.

With an array, the data can be wiped explicitly after its work is complete. The array can be overwritten and and the password won’t be present anywhere in the system, even before garbage collection.

2) Security: Any one who has access to memory dump can find the password in clear text. So Storing password in character array clearly mitigates security risk of stealing password.

3) Log file safety: With an array, one can explicitly wipe the data , overwrite the array and the password won’t be present anywhere in the system.
With plain String, there are much higher chances of accidentally printing the password to logs, monitors or some other insecure place. So char[] is less vulnerable.

No comments:

Post a Comment

Contact Form

Name

Email *

Message *