-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashedNetworkStack.cs
More file actions
56 lines (45 loc) · 1.49 KB
/
Copy pathHashedNetworkStack.cs
File metadata and controls
56 lines (45 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace NetworkingStack.Core
{
public class HashedNetworkStack : NetworkStack
{
public override byte[] Read()
{
return ReadDataHash(base.Read());
}
public override void Write(byte[] data)
{
base.Write(WriteDataHash(data));
}
private static byte[] WriteDataHash(byte[] data)
{
List<byte> buffer = new List<byte>(data);
using (SHA1CryptoServiceProvider hash = new SHA1CryptoServiceProvider())
{
buffer.AddRange(hash.ComputeHash(data));
}
return buffer.ToArray();
}
private static byte[] ReadDataHash(byte[] data)
{
byte[] hashData;
using (SHA1CryptoServiceProvider hash = new SHA1CryptoServiceProvider())
{
hashData = hash.ComputeHash(data);
hashData = hash.ComputeHash(data, 0, data.Length - hashData.Length);
for (int i = 0; i < hashData.Length; i++)
{
if (hashData[i] != data[i + data.Length - hashData.Length])
throw new Exception();
}
}
byte[] buffer = new byte[data.Length - hashData.Length];
Buffer.BlockCopy(data, 0, buffer, 0, buffer.Length);
return buffer;
}
}
}