.NET有丰富的加密解密API库供我们使用,本文章总结了.NET下的Hash散列算法,并制作成简单的DEMO,希望能对大家有所帮助。
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace EncryptAndDecrypt { public class MD5 { public byte[] Hash(byte[] data) { System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); return md5.ComputeHash(data); } } }
SHA1
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace EncryptAndDecrypt { public class SHA1 { public byte[] Hash(byte[] data) { System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); return sha1.ComputeHash(data); } } }
SHA256
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace EncryptAndDecrypt { public class SHA256 { public byte[] Hash(byte[] data) { System.Security.Cryptography.SHA256 sha256= System.Security.Cryptography.SHA256.Create(); return sha256.ComputeHash(data); } } }SHA384
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace EncryptAndDecrypt { public class SHA384 { public byte[] Hash(byte[] data) { System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384.Create(); return sha384.ComputeHash(data); } } }
SHA512
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace EncryptAndDecrypt { public class SHA512 { public byte[] Hash(byte[] data) { System.Security.Cryptography.SHA512 sha512 = System.Security.Cryptography.SHA512.Create(); return sha512.ComputeHash(data); } } }