1. 概述
本文解释Java中为什么用char[]来表示密码而不是String。
注意本文重点在内存中操作密码的方法而不是存储密码的实际方法,存储密码我们通常放在持久层。
假设我们不能够控制密码的格式(例如密码来自于三方库API的字符串)。尽管看起来用字符串操作密码很合理,但是Java团队还是建议用char[]。

例如,javax.swing中的JPasswordField中,我们可以看到getText()自Java2以来就已被弃用,取而代之的是返回char[]的getPassword()方法。
因此,我们来深入了解一下为什么推荐用char[]来操作密码。
2. 字符串不可变
Java中字符串是不可变的,这意味着我们无法用任何高级API更改它们。对于字符串中的任何一个变更都会产生一个新的字符串,旧的字符串将还保留在内存中。
因此,存储密码的字符串一直在内存中可用直到垃圾收集器清除它。我们无法控制这个回收的过程,而且字符串生存的时间可能比常规对象要长得多,因为为了可重用字符串是保存在字符串池中的。
因为,任何有访问内存权限的人都可以从内存中查看密码。使用char[]我们可以在完成预期工作后显式擦除数据,这样即使在垃圾收集发生之前,我们也可以确保从内存中删除密码。
使用String的场景:- String stringPassword = "password";
- System.out.println(String.format("Original String password value: %s", stringPassword));
- System.out.println(String.format("Original String password hashCode: %s",
- Integer.toHexString(stringPassword.hashCode())));
- String newString = "*********";
- stringPassword.replace(stringPassword, newString);
- System.out.println(String.format("String password value after trying to replace it: %s", stringPassword));
- System.out.println(String.format("hashCode after trying to replace the original String: %s",
- Integer.toHexString(stringPassword.hashCode())));
复制代码 输出:- Original String password value: password
- Original String password hashCode: 4889ba9b
- String password value after trying to replace it: password
- hashCode after trying to replace the original String: 4889ba9b
复制代码 使用char[]的场景:- char[] charPassword = new char[]{'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
- System.out.println(String.format("Original char password value: %s", charPassword));
- System.out.println(String.format("Original char password hashCode: %s",
- Integer.toHexString(charPassword.hashCode())));
- Arrays.fill(charPassword, '*');
- System.out.println(String.format("Changed char password value: %s", charPassword));
- System.out.println(String.format("Changed char password hashCode: %s",
- Integer.toHexString(charPassword.hashCode())));
复制代码 输出:- Original char password value: [C@51cdd8a
- Original char password hashCode: 51cdd8a
- Changed char password value: [C@51cdd8a
- Changed char password hashCode: 51cdd8a
复制代码 可以看到,使用String时,替换原始字符串后,值和hashCode都没有变,这意味着String保持不变。
对于char[]数组,值变了hashCode没变说明我们是更改的同一个对象数据。
3. 可以不经意间打印密码
使用char[]中操作密码的另一个好处是可以防止在控制台、监视器或其他不安全的地方记录密码。
下面是代码:- String stringPassword = "password";
- char[] charPassword = new char[]{'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
- System.out.println(String.format("Printing String password -> %s", stringPassword));
- System.out.println(String.format("Printing char[] password -> %s", charPassword));
复制代码 输出:- Printing String password -> password
- Printing char[] password -> [C@51cdd8a
复制代码 使用String打印了密码本身,使用char[]输出的[C@51cdd8a 这样密码就不那么容易泄露了。
4. 结论
本文探索了为什么不用字符串来操作密码而使用char[]来操作密码。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |