Skip to content

Commit

Permalink
Fixed bug when Y value wasn't same size in bytes as the prime polynom…
Browse files Browse the repository at this point in the history
…ial.

This lead to an unfortunate situation where sometimes things couldn't be recovered because a "00" prefix was missing.
  • Loading branch information
moserware committed Nov 20, 2013
1 parent 1b54b72 commit 2c28cf9
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions SecretSplitter/Algebra/FiniteFieldPoint.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Moserware.Numerics;
using System;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using Moserware.Numerics;

namespace Moserware.Algebra {
/// <summary>
Expand All @@ -30,8 +30,16 @@ public string ToString(int totalPoints) {
string format = sb.ToString();

string shareNumber = ((long) X.PolynomialValue).ToString(format);


var expectedByteCount = Y.PrimePolynomial.SizeInBytes;
var pointBytes = Y.PolynomialValue.ToUnsignedBigEndianBytes();

// Occasionally, the value won't fill all bytes, so we need to prefix with 0's as needed
var prefixedPointBytes = Enumerable.Range(0, expectedByteCount - pointBytes.Length).Select(ix => (byte)0).Concat(pointBytes);

// To hex string on its own just wasn't working right
string shareValue = String.Join("", Y.PolynomialValue.ToUnsignedBigEndianBytes().Select(b => b.ToString("x2")));
string shareValue = String.Join("", prefixedPointBytes.Select(b => b.ToString("x2")));
return shareNumber + "-" + shareValue;
}

Expand Down

0 comments on commit 2c28cf9

Please sign in to comment.