The MessageDigest
class can provide you with an instance of the MD5 digest.
(MessageDigest
类可以为您提供MD5摘要的实例。)
When working with strings and the crypto classes be sure to always specify the encoding you want the byte representation in. If you just use string.getBytes()
it will use the platform default.
(使用字符串和crypto类时,请务必始终指定要使用字节表示形式的编码。如果仅使用string.getBytes()
,它将使用平台默认值。)
(Not all platforms use the same defaults) ((并非所有平台都使用相同的默认值))
import java.security.*;
..
byte[] bytesOfMessage = yourString.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
If you have a lot of data take a look at the .update(byte[])
method which can be called repeatedly.
(如果您有大量数据,请查看.update(byte[])
方法,该方法可以重复调用。)
Then call .digest()
to obtain the resulting hash. (然后调用.digest()
以获取结果哈希。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…