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

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Security.Cryptography;
 5namespace 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
posted @ 2007-10-08 15:33  VincentYinBo  阅读(493)  评论(0编辑  收藏  举报