MessageDigest 的 update 方法有什么作用以及 BASE64Encoder 的用途是什么?
以下是加密用户字符串的代码:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
import java.io.*;
class Encrypter {
public synchronized String encrypt(String plainText) throws Exception {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA");
}catch(Exception exc) {
throw new Exception(exc.getMessage());
}
try {
md.update(plainText.getBytes("UTF-8"));
}catch(Exception exc) {
throw new Exception(exc.getMessage());
}
byte raw[] = md.digest();
String hash = (new BASE64Encoder()).encode(raw);
return hash;
}
public static void main(String args[]) {
try {
Encrypter encrypter = new Encrypter();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userInput = br.readLine();
String encryptedPassword = encrypter.encrypt(userInput);
System.out.println(encryptedPassword);
} catch(Exception exc) {
System.out.println(exc);
}
}
}
当我编译代码时,我收到这些警告:
Encrypter.java:4: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
import sun.misc.BASE64Encoder;
^
Encrypter.java:23: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
String hash = (new BASE64Encoder()).encode(raw);
^
2 warnings
是否有其他方法来加密 java 中的字符串?
该方法 的作用是什么更新
做什么?即语句 MessageDigest
类的md.update(plainText.getBytes("UTF-8"));
的作用是什么?
什么是 BASE64Encoder
类?我找不到它的 DOC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,您没有执行任何加密。您正在计算输入的单向哈希或摘要。该散列稍后可用于验证消息的完整性。请参阅哈希、SHA1 和 消息摘要。
Base64 编码 是一种用 ASCII 表示二进制数据的方法。这通常是可取的,因为并非所有数据存储和传输机制都支持原始二进制文件。例如,如果您想通过 http 查询字符串参数传输计算出的摘要,则需要将其编码为 Base64。此外,将原始二进制文件保存或打印到控制台将产生一串奇怪的字符,这些字符可能超出可打印范围,并且还可能从您的 PC 扬声器发出蜂鸣声!
您使用的
Base64Encoder
来自sun.misc
包,切勿使用。这是内部 Sun JVM 代码,将来可能可用,也可能不可用。这也解释了为什么您找不到任何 javadoc。幸运的是,存在一些免费且开放的 Base64 编码器和解码器。 Apache Commons Codec 是一个广泛使用且稳定的库,其中包含多个编解码器,包括 Base64。
md.update(plainText.getBytes("UTF-8"))
更新摘要的输入。调用digest
执行最终更新并计算输入的摘要。请参阅 的 javadocmd.digest
和md.update
First of all, you're not performing any encryption. You're computing a one-way hash or digest of your input. This hash can be later used to verify the integrity of the message. See Hashing, SHA1 and MessageDigest.
Base64 encoding is a method of representing binary data in ASCII. This is often desirable because not all data storage and transmission mechanisms support raw binary. For example, if you want to transfer your computed digest via an http query string parameter, you'll want to encode it as Base64. Also, saving or printing raw binary to the console will produce a stream of funky characters which may be outside of the printable range, and may also produce beeps from your PC speaker!
The
Base64Encoder
you're using comes from thesun.misc
package and should NEVER be used. This is internal Sun JVM code which may or may not be available in the future. That also explains why you're weren't able to find any javadoc.Fortunately, several free and open Base64 encoders and decoders exist. Apache Commons Codec is a widely used and stable library which contains several codecs include Base64.
md.update(plainText.getBytes("UTF-8"))
updates the input to the digest. Callingdigest
performs a final update and computes the digest of the input. See javadoc ofmd.digest
andmd.update
为了构建Sahil Muthoo 出色答案的第 5 条,下面是对源代码的更深入研究。
默认情况下,
update
方法只是将输入字节数组附加到MessageDigestSpi
抽象类的当前tempArray
中。MessageDigest
类扩展了MessageDigestSpi
类。然后调用MessageDigest.update
,调用MessageDigestSpi.engineUpdate
方法,查看源码可以发现:MessageDigest.java (源代码)
MessageDigestSpi.engineUpdate
是一个抽象方法必须通过扩展类来实现,如下所示:MessageDigestSpi.java (源代码)
To build off of bullet 5 from Sahil Muthoo's excellent answer, below is an deeper look into the source code.
By default, the
update
method simply appends the input byte array to the currenttempArray
of theMessageDigestSpi
abstract class.The
MessageDigest
class extends theMessageDigestSpi
class. ThenMessageDigest.update
is called the methodMessageDigestSpi.engineUpdate
is called, which can be found from investigating the source code:MessageDigest.java (source code)
MessageDigestSpi.engineUpdate
is an abstract method that must be implemented by extending classes as seen below:MessageDigestSpi.java (source code)
看看 Apache Commons 编解码器:
https://commons.apache.org/codec/
例如:
https://commons.apache.org /codec/api-release/org/apache/commons/codec/digest/DigestUtils.html
Have a look at Apache Commons Codec:
https://commons.apache.org/codec/
E.g.:
https://commons.apache.org/codec/api-release/org/apache/commons/codec/digest/DigestUtils.html
虽然这里的旧帖子是更新的答案。 Java 8 的 Base64。
Java 8 Base64 文档
While old post here is an updated answer. Java 8's Base64.
Java 8 Base64 Documents
对于Base64加密和解密,这个警告明确表示不鼓励使用Sun的Base64Encoder实现,并警告该实现可能会在未来版本中删除,我们能做的就是切换到Base64编码器的其他实现。我们可以使用 Commons Codec 库 进行 Base64 编码器。以下是一个示例:
使用 Commons 编解码器库后,您不应再看到上述警告。
For Base64 encryption and decryption this warning clearly says that it does not encourage the use of Sun implementation of Base64Encoder and gives a warning that the implementation may be removed in future releases, what we can do is to switch to other implementation of Base64 encoder. We can use Commons Codec library for Base64 Encoder. Following is an example:
After using Commons codec library, you should not see above warning again.