在Java和C#中計算SHA-1哈希

Java版本:

public void testHash() { String password = "Test"; byte[] key = password.getBytes(); MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] hash = md.digest(key); String result = ""; for ( byte b : hash ) { result += Integer.toHexString(b + 256) + " "; } System.out.println(result); } 

C#版本:

 public void testHash() { String password = "Test"; byte[] key = System.Text.Encoding.Default.GetBytes(password); SHA1 sha1 = SHA1Managed.Create(); byte[] hash = sha1.ComputeHash(key); String result; foreach ( byte b in hash ) { result += Convert.ToInt32(b).ToString("x2") + " "; } Console.WriteLine(result); } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章