MyRapid MyRapidMyRapid

加密函数之Hash(MD5/SHA1/SHA256/SHA384/SHA512)

/*------------------------------NBest 快速开发框架----------------------------
	创建时间:2019-01-24 15:34:10
	作者:陈恩点
	邮箱:dvmu@163.com
	变更记录:
	时间               		变更作者    变更内容
	2019-01-24 15:34:10		陈恩点      初次创建文件
----------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NBest.Code
{
    public class Hash
    {
        /// <summary>
        /// 获取文件的Hash值()
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static string Encrypt(byte[] data)
        {
            try
            {
                HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
                //HashAlgorithm hashAlgorithm = new SHA1CryptoServiceProvider();
                //HashAlgorithm hashAlgorithm = new SHA256CryptoServiceProvider();
                //HashAlgorithm hashAlgorithm = new SHA384CryptoServiceProvider();
                //HashAlgorithm hashAlgorithm = new SHA512CryptoServiceProvider();
                //计算Hash
                var result = hashAlgorithm.ComputeHash(data);
                string hash = BitConverter.ToString(result);
                return hash;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "";
            }
        }
    }
}


创作不易,转载请保留原文连接:加密函数之Hash(MD5/SHA1/SHA256/SHA384/SHA512)

点赞(212) 打赏

发表评论

热门评论

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏