comparison Chronosv2/source/Extensions/ArrayExtensions.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children 09d18d6e5f40
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 /*
2 Copyright (c) 2008 - 2009, Carlos Guzmán Álvarez
3
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9 * Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14 * Neither the name of the author nor the names of its contributors may be used to endorse or
15 promote products derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 using System;
31 using System.Security.Cryptography;
32 using System.Text;
33
34 namespace Chronos.Extensions
35 {
36 /// <summary>
37 /// Extension methods
38 /// </summary>
39 public static class ArrayExtensions
40 {
41 #region · Extensions ·
42
43 /// <summary>
44 /// Converts a given byte array to a base-64 string
45 /// </summary>
46 /// <param name="buffer"></param>
47 /// <returns></returns>
48 public static string ToBase64String(this byte[] buffer)
49 {
50 return Convert.ToBase64String(buffer);
51 }
52
53 /// <summary>
54 /// Computes the MD5 hash of a given byte array
55 /// </summary>
56 /// <param name="buffer"></param>
57 /// <returns></returns>
58 public static byte[] ComputeMD5Hash(this byte[] buffer)
59 {
60 MD5 md5 = MD5.Create();
61 md5.TransformFinalBlock(buffer, 0, buffer.Length);
62
63 return md5.Hash;
64 }
65
66 /// <summary>
67 /// Computes the SHA1 hash of a given byte array
68 /// </summary>
69 /// <param name="buffer"></param>
70 /// <returns></returns>
71 public static byte[] ComputeSHA1Hash(this byte[] buffer)
72 {
73 SHA1 hashAlgorithm = SHA1.Create();
74 hashAlgorithm.TransformFinalBlock(buffer, 0, buffer.Length);
75
76 return hashAlgorithm.Hash;
77 }
78
79 /// <summary>
80 /// Computes the MD5 hash of a given array of strings
81 /// </summary>
82 /// <param name="buffer"></param>
83 /// <returns></returns>
84 public static byte[] ComputeMD5Hash(this string[] values)
85 {
86 MD5 hashAlgorithm = MD5.Create();
87
88 foreach (string value in values)
89 {
90 if (value != null)
91 {
92 byte[] buffer = Encoding.UTF8.GetBytes(value);
93 byte[] output = new byte[buffer.Length];
94 int count = hashAlgorithm.TransformBlock(buffer, 0, buffer.Length, output, 0);
95 }
96 }
97
98 hashAlgorithm.TransformFinalBlock(new byte[0], 0, 0);
99
100 return hashAlgorithm.Hash;
101 }
102
103 /// <summary>
104 /// Computes the SHA1 hash of a given array of strings
105 /// </summary>
106 /// <param name="buffer"></param>
107 /// <returns></returns>
108 private static byte[] ComputeSHA1Hash(this string[] values)
109 {
110 SHA1 hashAlgorithm = SHA1.Create();
111
112 foreach (string value in values)
113 {
114 if (value != null)
115 {
116 byte[] buffer = Encoding.UTF8.GetBytes(value);
117 byte[] output = new byte[buffer.Length];
118 int count = hashAlgorithm.TransformBlock(buffer, 0, buffer.Length, output, 0);
119 }
120 }
121
122 hashAlgorithm.TransformFinalBlock(new byte[0], 0, 0);
123
124 return hashAlgorithm.Hash;
125 }
126
127 /// <summary>
128 /// Convert a byte array to an hex string
129 /// </summary>
130 /// <param name="buffer"></param>
131 /// <returns></returns>
132 public static string ToHexString(this byte[] buffer)
133 {
134 StringBuilder hex = new StringBuilder();
135
136 for (int i = 0; i < buffer.Length; i++)
137 {
138 hex.Append(buffer[i].ToString("x2"));
139 }
140
141 return hex.ToString();
142 }
143
144 #endregion
145 }
146 }