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