-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShare.cs
117 lines (102 loc) · 3.07 KB
/
Share.cs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Org.BouncyCastle.Crypto.Digests;
namespace shamirsSecretSharing
{
public class Share
{
/// <summary>
/// X-value of the share
/// </summary>
public byte[] X
{
get;
internal set;
}
/// <summary>
/// Y-value of the share
/// </summary>
public byte[] Y
{
get;
internal set;
}
/// <summary>
/// Create a share
/// </summary>
public Share(byte[] x, byte[] y)
{
Y = new byte[y.Length];
X = new byte[x.Length];
Array.Copy(y, Y, y.Length);
Array.Copy(x, X, x.Length);
}
/// <summary>
/// Calculate the SHA256-Hash of the share
/// </summary>
public byte[] GetHash()
{
Sha256Digest sha256 = new Sha256Digest();
byte[] hash = new byte[sha256.GetByteLength()];
sha256.BlockUpdate(X, 0, X.Length);
sha256.BlockUpdate(Y, 0, Y.Length);
sha256.GetByteLength();
sha256.DoFinal(hash, 0);
return hash;
}
public override bool Equals(object obj)
{
var share = obj as Share;
return this.GetHash().SequenceEqual(share.GetHash());
}
public byte[] ToBinary()
{
List<byte> retVal = new List<byte> { };
byte[] help, len;
// add X
retVal.Add(0x01);
help = PublicKey.getSubarry(X, 0, X.Length);
len = BitConverter.GetBytes(help.Length);
retVal.AddRange(len);
retVal.AddRange(help);
// add encryptedKey
retVal.Add(0x02);
help = PublicKey.getSubarry(Y, 0, Y.Length);
len = BitConverter.GetBytes(help.Length);
retVal.AddRange(len);
retVal.AddRange(help);
return retVal.ToArray();
}
public static Share FromBinary(byte[] array)
{
Share sh;
byte[] x, y;
int len;
x = y = new byte[0];
for (int i = 0; i < array.Length; i++)
{
switch (array[i])
{
case 0x01:
len = BitConverter.ToInt32(array, i + 1);
i = i + 4;
x = PublicKey.getSubarry(array, i + 1, i + 1 + len);
i = i + len;
break;
case 0x02:
len = BitConverter.ToInt32(array, i + 1);
i = i + 4;
y = PublicKey.getSubarry(array, i + 1, i + 1 + len);
i = i + len;
break;
default:
throw new FormatException("Share Format is incorrect");
}
}
sh = new Share(x,y);
return sh;
}
}
}