亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在java中嘗試MD5哈希

在java中嘗試MD5哈希

暮色呼如 2024-01-25 21:34:51
您好,我編寫了一個類來為字符串輸入創建哈希,但我的程序有時會為兩個不同的輸入提供相同的哈希。import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class Test {public byte[] Hash(String input) throws NoSuchAlgorithmException{    MessageDigest messageDigest = MessageDigest.getInstance("MD5");    byte b[] = messageDigest.digest(input.getBytes());    return b;}public static void main(String args[]) throws NoSuchAlgorithmException{   Test t = new Test();   byte[] hashValue = t.Hash("viud");   String hashString = hashValue.toString();   while(hashString.length()<32)   {       hashString = "0" + hashString;   }   System.out.println(hashString);}}當我對函數 Hash() 的輸入為“viud”時,我得到的結果為 --> 0000000000000000000000[B@13e8c1c 當我的輸入字符串為“Hello”時,我得到的結果為 --> 0000000000000000000000[B@ 13e8c1c但這種情況在程序執行時只發生幾次。每次運行該程序時,我都會為相同的輸入值生成不同的哈希值,有時還會為兩個不同的輸入獲取相同的哈希值。究竟發生了什么?
查看完整描述

1 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

  byte[] hashValue = t.Hash("viud");   
  String hashString = hashValue.toString();

byte[] 上的 toString 將為您提供 byte[] 的內存(堆)地址。這不是你想要的。你要

String hashString = new String(t.Hash("viud"));


查看完整回答
反對 回復 2024-01-25
  • 1 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號