本文介紹了SHA 256 不同的結(jié)果的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
如果我從 Mac 調(diào)用命令
If I invoke the command from Mac
echo hello | shasum -a 256
或來自 ubuntu
echo hello | sha256sum
然后我得到以下結(jié)果
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 -
我注意到末尾有破折號.
I notice there is dash at the end.
但是當(dāng)我使用 Python hashlib
或 Java java.security.MessageDigest
時(shí),它們給我的結(jié)果相同,如下所示:
But when I use Python hashlib
or Java java.security.MessageDigest
, they give me the same result as follows:
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
那么,誰能指出我哪里弄錯(cuò)了?
So, could anyone point out where I got it wrong please?
謝謝.
Python:
>>> import hashlib
>>> hashlib.sha256("hello").hexdigest()
Java:
MessageDigest md = MessageDigest.getInstance("SHA-256");
String text = "hello";
md.update(text.getBytes("UTF-8"));
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
sb.append(String.format("%02x", digest[i] & 0xFF))
}
System.out.println(sb.toString());
推薦答案
echo
命令正在為您的字符串添加尾隨換行符.試試:
The echo
commands are adding a trailing newline to your string. Try:
hashlib.sha256("hello
").hexdigest()
這篇關(guān)于SHA 256 不同的結(jié)果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!