Why is char[] preferred over String for passwords?

Answer

Strings are immutable. That means once you\\\'ve created the string, if another process can dump memory, there\\\'s no way (aside from reflection) you can get rid of the data before GC kicks in.

With an array, you can explicitly wipe the data after you\\\'re done with it: you can overwrite the array with anything you like, and the password won\\\'t be present anywhere in the system, even before garbage collection.

So yes, this is a security concern - but even using char[] only reduces the window of opportunity for an attacker, and it\\\'s only for this specific type of attack.

EDIT: As noted in comments, it\\\'s possible that arrays being moved by the garbage collector will leave stray copies of the data in memory. I believe this is implementation-specific - the GC may clear all memory as it goes, to avoid this sort of thing. Even if it does, there\\\'s still the time during which the char[] contains the actual characters as an attack window.

All string Questions

Ask your interview questions on string

Write Your comment or Questions if you want the answers on string from string Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---