-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCrcAlgorithm.cs
22 lines (20 loc) · 941 Bytes
/
CrcAlgorithm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace ComputerNetwork.Polynomials
{
public static class CrcAlgorithm
{
private static BinaryPolynomial GeneratorPolynomial { get; } = new BinaryPolynomial("11010");
private static int Padding { get; } = GeneratorPolynomial.Degree - 1;
public static BinaryPolynomial GenerateCrc(BinaryPolynomial polynomial)
{
var polynomialWithPaddingShift = polynomial.Copy().RiseDegree(Padding);
var divisionResult = BinaryPolynomial.Division(polynomialWithPaddingShift, GeneratorPolynomial);
return polynomialWithPaddingShift + divisionResult.Item2;
}
public static bool IsCrcValid(BinaryPolynomial polynomialForCheck)
{
var polynomial = polynomialForCheck.Copy();
var divisionResult = BinaryPolynomial.Division(polynomial, GeneratorPolynomial);
return divisionResult.Item2.IsNullPolynomial;
}
}
}