wasSharpNET – Blame information for rev 27

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
27 office 8 using System.Collections.Generic;
10 office 9 using System.IO;
27 office 10 using System.Linq;
5 office 11 using System.Text;
10 office 12 using System.Threading.Tasks;
2 office 13  
14 namespace wasSharpNET.Cryptography
15 {
16 public static class SHA1
17 {
18 /// <summary>
19 /// Return a 40 character hex representation of a SHA1 hash.
20 /// </summary>
21 /// <param name="sha1">the SHA1 hash object</param>
13 office 22 /// <param name="data">the byte data to compute the hash from</param>
2 office 23 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, byte[] data)
24 {
25 return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
26 }
5 office 27  
13 office 28 /// <summary>
27 office 29 /// Return a 40 character hex representation of a SHA1 hash from multiple sequential byte arrays.
30 /// </summary>
31 /// <param name="sha1">the SHA1 hash object</param>
32 /// <param name="data">the byte data to compute the hash from</param>
33 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, IEnumerable<byte[]> data)
34 {
35 return sha1.ToHex(data.Where(i => i != null).SelectMany(i => i).ToArray());
36 }
37  
38 /// <summary>
13 office 39 /// Return a 40 character hex representation of a SHA1 hash.
40 /// </summary>
41 /// <param name="sha1">the SHA1 hash object</param>
42 /// <param name="data">the string data to compute the hash from</param>
5 office 43 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data)
44 {
45 return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "");
46 }
10 office 47  
13 office 48 /// <summary>
49 /// Return a 40 character hex representation of a SHA1 hash.
50 /// </summary>
51 /// <param name="sha1">the SHA1 hash object</param>
52 /// <param name="data">the memorystream to compute the hash from</param>
14 office 53 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, Stream data)
13 office 54 {
55 return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
56 }
57  
14 office 58 /// <summary>
59 /// Return a 40 character hex representation of a SHA1 hash.
60 /// </summary>
61 /// <param name="sha1">the SHA1 hash object</param>
62 /// <param name="data">a filestream to read the file from</param>
63 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, FileStream data)
64 {
65 return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
66 }
67  
10 office 68 ///////////////////////////////////////////////////////////////////////////
69 // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
70 ///////////////////////////////////////////////////////////////////////////
71 /// <summary>
72 /// Compute the SHA1 hash of an array of bytes and copy the result to the output stream as a hexadecimal string.
73 /// </summary>
74 /// <param name="sha1">the SHA1 object to use</param>
75 /// <param name="data">the data to hash</param>
76 /// <param name="outStream">the output stream</param>
77 public static async Task CopyToAsync(this System.Security.Cryptography.SHA1 sha1, byte[] data, Stream outStream)
78 {
79 var buffer = new char[1];
27 office 80 using (var SHA1OutputStream = new MemoryStream())
10 office 81 {
27 office 82 using (var SHA1InputStream =
83 new StringReader(BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "")))
10 office 84 {
85 int count;
86 while ((count = await SHA1InputStream.ReadAsync(buffer, 0, 1)) != 0)
87 switch (buffer[0])
88 {
89 case '-':
90 continue;
91 default:
27 office 92 SHA1OutputStream.WriteByte((byte) buffer[0]);
10 office 93 break;
94 }
95 }
96 SHA1OutputStream.Position = 0L;
97 await SHA1OutputStream.CopyToAsync(outStream);
98 }
99 }
2 office 100 }
27 office 101 }