(转)封装了MD5,SHA1等加密算法的类

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

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

70            return output;
71        }

72    }

73}
http://www.spdevelop.com/forum/Answer.aspx?QuestionId=47

转载于:https://www.cnblogs.com/wjsyby/archive/2007/10/08/917143.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值