Why Grapheme Array Is Ameliorate Than String For Storing Password Inward Java
Tuesday, February 5, 2019
Add Comment
Why grapheme array is amend than String for storing password inwards Java was recent inquiry asked to i of my friend inwards a coffee interview. he was interviewing for a Technical Pb position in addition to has over vi years of experience.Both Character array in addition to String tin locomote used to shop text information but choosing i over other is hard inquiry if y'all haven't faced the province of affairs already. But every bit my friend said whatsoever inquiry related to String must convey a clue on particular belongings of Strings similar immutability in addition to he used that to convince interviewer. hither nosotros volition explore about reasons on why should y'all used char[] for storing password than String.
Why String is immutable inwards Java or How Substring tin movement retention leak inwards Java, if y'all haven't read those y'all may notice them interesting.Here are few reasons which makes feel to believe that grapheme array is amend selection for storing password inwards Java than String:
Why String is immutable inwards Java or How Substring tin movement retention leak inwards Java, if y'all haven't read those y'all may notice them interesting.Here are few reasons which makes feel to believe that grapheme array is amend selection for storing password inwards Java than String:
1) Since Strings are immutable inwards Java if y'all shop password every bit manifestly text it volition locomote available inwards retention until Garbage collector clears it in addition to since String are used inwards String puddle for reusability at that topographic point is pretty high gamble that it volition locomote rest inwards retention for long duration, which pose a safety threat. Since whatsoever i who has access to retention dump tin notice the password inwards clear text in addition to that's about other argue y'all should ever used an encrypted password than manifestly text. Since Strings are immutable at that topographic point is no agency contents of Strings tin locomote changed because any modify volition create novel String, piece if y'all char[] y'all tin nonetheless laid all his chemical constituent every bit blank or zero. So Storing password inwards grapheme array clearly mitigates safety peril of stealing password.
2) Java itself recommends using getPassword() method of JPasswordField which returns a char[] in addition to deprecated getText() method which returns password inwards clear text stating safety reason. Its proficient to follow advice from Java squad in addition to adhering to criterion rather than going against it.
3) With String at that topographic point is ever a peril of printing manifestly text inwards log file or console but if role Array y'all won't impress contents of array instead its retention place larn printed. though non a existent argue but nonetheless brand sense.
String strPassword="Unknown";
char[] charPassword= new char[]{'U','n','k','w','o','n'};
System.out.println("String password: " + strPassword);
System.out.println("Character password: " + charPassword);
String password: Unknown
Character password: [C@110b053
char[] charPassword= new char[]{'U','n','k','w','o','n'};
System.out.println("String password: " + strPassword);
System.out.println("Character password: " + charPassword);
String password: Unknown
Character password: [C@110b053
That's all on why grapheme array is amend selection than String for storing passwords inwards Java. Though using char[] is non but plenty y'all demand to erase content to locomote to a greater extent than secure. I likewise propose working amongst hash'd or encrypted password instead of manifestly text in addition to clearing it from retention every bit before long every bit authentication is completed.
Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
Why multiple inheritance is non supported inwards Java
0 Response to "Why Grapheme Array Is Ameliorate Than String For Storing Password Inward Java"
Post a Comment