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