0
私はMD5とMessageDigestでSHAを勉強しています。ここでは、MD5とMessageDigestを実装するクラスのコードを示します。私はそれを理解するのに困っている。誰かがこのMD5 MessageDigestコードのしくみを説明できますか?
したがって、MD5の「インスタンス」を取得します。私はそれがMD5アルゴリズムだと思いますか?次に、バイトを更新します。それはなぜこれをするのですか?次に、md.digest()を使って変数bytes bを作成しますが、なぜこれもどちらかとは分かりません。それで、私はそれがアルゴリズムを実行するためにfor文を使用し、おそらくそれを埋め込む(0を付加する)と思います。誰かが少し良く説明できるなら、私は感謝します!
try {
MessageDigest md = MessageDigest.getInstance("MD5"); // get the
// instance
// of md5
md.update(bytes); // get the digest updated
byte[] b = md.digest(); // calculate the final value
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
message = buf.toString(); // output as strings
} catch (NoSuchAlgorithmException e) {
e.printStackTrace(); // when certain algorithm is down, output the
// abnormal condition
}
return message;
}
バイトを16進数に変換するより良い方法を説明できますか? – RockOn
http://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java –
私はGuavaの 'BaseEncoding.base16( ).lowerCase()。encode(bytes) '。 –