一个封装了MD5,SHA1等加密算法的类

本文介绍了一个使用C#实现的哈希算法类,该类支持多种哈希算法,包括SHA1、SHA256、SHA384、SHA512、MD5和RIPEMD160。通过简单的API调用,可以方便地生成指定类型的哈希值。



//C# Code

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Security.Cryptography;
 5
 6namespace trips
 7ExpandedBlockStart.gifContractedBlock.gif{
 8    static class CryptClass
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    {
10        public enum HashType : int
11ExpandedSubBlockStart.gifContractedSubBlock.gif        {
12            SHA1,
13            SHA256,
14            SHA384,
15            SHA512,
16            MD5,
17            RIPEMD160
18        }

19
20
21        public static string FromString(string input, HashType hashtype)
22ExpandedSubBlockStart.gifContractedSubBlock.gif        {
23            Byte[] clearBytes;
24            Byte[] hashedBytes;
25            string output = String.Empty;
26
27            switch (hashtype)
28ExpandedSubBlockStart.gifContractedSubBlock.gif            {
29                case HashType.RIPEMD160:
30                    clearBytes = new UTF8Encoding().GetBytes(input);
31                    RIPEMD160 myRIPEMD160 = RIPEMD160Managed.Create();
32                    hashedBytes = myRIPEMD160.ComputeHash(clearBytes);
33                    output = BitConverter.ToString(hashedBytes).Replace("-""").ToLower();
34                    break;
35                case HashType.MD5:
36                    clearBytes = new UTF8Encoding().GetBytes(input);
37                    hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
38                    output = BitConverter.ToString(hashedBytes).Replace("-""").ToLower();
39                    break;
40                case HashType.SHA1:
41                    clearBytes = Encoding.UTF8.GetBytes(input);
42                    SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
43                    sha1.ComputeHash(clearBytes);
44                    hashedBytes = sha1.Hash;
45                    sha1.Clear();
46                    output = BitConverter.ToString(hashedBytes).Replace("-""").ToLower();
47                    break;
48                case HashType.SHA256:
49                    clearBytes = Encoding.UTF8.GetBytes(input);
50                    SHA256 sha256 = new SHA256Managed();
51                    sha256.ComputeHash(clearBytes);
52                    hashedBytes = sha256.Hash;
53                    sha256.Clear();
54                    output = BitConverter.ToString(hashedBytes).Replace("-""").ToLower();
55                    break;
56                case HashType.SHA384:
57                    clearBytes = Encoding.UTF8.GetBytes(input);
58                    SHA384 sha384 = new SHA384Managed();
59                    sha384.ComputeHash(clearBytes);
60                    hashedBytes = sha384.Hash;
61                    sha384.Clear();
62                    output = BitConverter.ToString(hashedBytes).Replace("-""").ToLower();
63                    break;
64                case HashType.SHA512:
65                    clearBytes = Encoding.UTF8.GetBytes(input);
66                    SHA512 sha512 = new SHA512Managed();
67                    sha512.ComputeHash(clearBytes);
68                    hashedBytes = sha512.Hash;
69                    sha512.Clear();
70                    output = BitConverter.ToString(hashedBytes).Replace("-""").ToLower();
71                    break;
72            }

73            return output;
74        }

75
76    }

77}

78


//End

转载于:https://www.cnblogs.com/yean/archive/2009/07/23/1529782.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值