wasSharpNET – Diff between revs 10 and 11
?pathlinks?
Rev 10 | Rev 11 | |||
---|---|---|---|---|
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 | public static string ToHex(this System.Security.Cryptography.SHA1 sha1, byte[] data) |
20 | public static string ToHex(this System.Security.Cryptography.SHA1 sha1, byte[] data) |
|
21 | { |
21 | { |
|
22 | return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""); |
22 | return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""); |
|
23 | } |
23 | } |
|
24 | |
- | ||
25 | |
24 | |
|
26 | public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data) |
25 | public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data) |
|
27 | { |
26 | { |
|
28 | return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", ""); |
27 | return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", ""); |
|
29 | } |
28 | } |
|
30 | |
29 | |
|
31 | /////////////////////////////////////////////////////////////////////////// |
30 | /////////////////////////////////////////////////////////////////////////// |
|
32 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
31 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
33 | /////////////////////////////////////////////////////////////////////////// |
32 | /////////////////////////////////////////////////////////////////////////// |
|
34 | /// <summary> |
33 | /// <summary> |
|
35 | /// Compute the SHA1 hash of an array of bytes and copy the result to the output stream as a hexadecimal string. |
34 | /// Compute the SHA1 hash of an array of bytes and copy the result to the output stream as a hexadecimal string. |
|
36 | /// </summary> |
35 | /// </summary> |
|
37 | /// <param name="sha1">the SHA1 object to use</param> |
36 | /// <param name="sha1">the SHA1 object to use</param> |
|
38 | /// <param name="data">the data to hash</param> |
37 | /// <param name="data">the data to hash</param> |
|
39 | /// <param name="outStream">the output stream</param> |
38 | /// <param name="outStream">the output stream</param> |
|
40 | public static async Task CopyToAsync(this System.Security.Cryptography.SHA1 sha1, byte[] data, Stream outStream) |
39 | public static async Task CopyToAsync(this System.Security.Cryptography.SHA1 sha1, byte[] data, Stream outStream) |
|
41 | { |
40 | { |
|
42 | var buffer = new char[1]; |
41 | var buffer = new char[1]; |
|
43 | using (MemoryStream SHA1OutputStream = new MemoryStream()) |
42 | using (MemoryStream SHA1OutputStream = new MemoryStream()) |
|
44 | { |
43 | { |
|
45 | using (StringReader SHA1InputStream = new StringReader(BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""))) |
44 | using (StringReader SHA1InputStream = new StringReader(BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""))) |
|
46 | { |
45 | { |
|
47 | int count; |
46 | int count; |
|
48 | while ((count = await SHA1InputStream.ReadAsync(buffer, 0, 1)) != 0) |
47 | while ((count = await SHA1InputStream.ReadAsync(buffer, 0, 1)) != 0) |
|
49 | { |
48 | { |
|
50 | switch (buffer[0]) |
49 | switch (buffer[0]) |
|
51 | { |
50 | { |
|
52 | case '-': |
51 | case '-': |
|
53 | continue; |
52 | continue; |
|
54 | default: |
53 | default: |
|
55 | SHA1OutputStream.WriteByte((byte)buffer[0]); |
54 | SHA1OutputStream.WriteByte((byte)buffer[0]); |
|
56 | break; |
55 | break; |
|
57 | } |
56 | } |
|
58 | } |
57 | } |
|
59 | } |
58 | } |
|
60 | SHA1OutputStream.Position = 0L; |
59 | SHA1OutputStream.Position = 0L; |
|
61 | await SHA1OutputStream.CopyToAsync(outStream); |
60 | await SHA1OutputStream.CopyToAsync(outStream); |
|
62 | } |
61 | } |
|
63 | } |
62 | } |
|
64 | } |
63 | } |
|
65 | } |
64 | } |
|
66 | |
65 | |
|
67 |
|
- | ||
68 | |
- | ||
69 | |
- | ||
70 | |
- |