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

发布时间 2023-07-27 18:21:45作者: 筱老邪

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); }