wasSharpNET – Blame information for rev 14

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;
10 office 8 using System.IO;
5 office 9 using System.Text;
10 office 10 using System.Threading.Tasks;
2 office 11  
12 namespace wasSharpNET.Cryptography
13 {
14 public static class SHA1
15 {
16 /// <summary>
17 /// Return a 40 character hex representation of a SHA1 hash.
18 /// </summary>
19 /// <param name="sha1">the SHA1 hash object</param>
13 office 20 /// <param name="data">the byte data to compute the hash from</param>
2 office 21 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, byte[] data)
22 {
23 return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
24 }
5 office 25  
13 office 26 /// <summary>
27 /// Return a 40 character hex representation of a SHA1 hash.
28 /// </summary>
29 /// <param name="sha1">the SHA1 hash object</param>
30 /// <param name="data">the string data to compute the hash from</param>
5 office 31 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data)
32 {
33 return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "");
34 }
10 office 35  
13 office 36 /// <summary>
37 /// Return a 40 character hex representation of a SHA1 hash.
38 /// </summary>
39 /// <param name="sha1">the SHA1 hash object</param>
40 /// <param name="data">the memorystream to compute the hash from</param>
14 office 41 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, Stream data)
13 office 42 {
43 return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
44 }
45  
14 office 46 /// <summary>
47 /// Return a 40 character hex representation of a SHA1 hash.
48 /// </summary>
49 /// <param name="sha1">the SHA1 hash object</param>
50 /// <param name="data">a filestream to read the file from</param>
51 public static string ToHex(this System.Security.Cryptography.SHA1 sha1, FileStream data)
52 {
53 return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
54 }
55  
10 office 56 ///////////////////////////////////////////////////////////////////////////
57 // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
58 ///////////////////////////////////////////////////////////////////////////
59 /// <summary>
60 /// Compute the SHA1 hash of an array of bytes and copy the result to the output stream as a hexadecimal string.
61 /// </summary>
62 /// <param name="sha1">the SHA1 object to use</param>
63 /// <param name="data">the data to hash</param>
64 /// <param name="outStream">the output stream</param>
65 public static async Task CopyToAsync(this System.Security.Cryptography.SHA1 sha1, byte[] data, Stream outStream)
66 {
67 var buffer = new char[1];
68 using (MemoryStream SHA1OutputStream = new MemoryStream())
69 {
70 using (StringReader SHA1InputStream = new StringReader(BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "")))
71 {
72 int count;
73 while ((count = await SHA1InputStream.ReadAsync(buffer, 0, 1)) != 0)
74 {
75 switch (buffer[0])
76 {
77 case '-':
78 continue;
79 default:
80 SHA1OutputStream.WriteByte((byte)buffer[0]);
81 break;
82 }
83 }
84 }
85 SHA1OutputStream.Position = 0L;
86 await SHA1OutputStream.CopyToAsync(outStream);
87 }
88 }
2 office 89 }
11 office 90 }