Ejemplo n.º 1
0
        public void Test7()
        {
            var decoder = new Rs256DecoderLittleEndian(64, 60, new GaloisField256(0x11D));
            var encoder = new Rs256EncoderLittleEndian(64, 60, new GaloisField256(0x11D));

            ReadOnlySpan <byte> largeEncodedData = LargeEncodedData;

            var rand = new Random(1);

            for (var i = 1; i < 64; ++i)
            {
                Span <byte> testData = stackalloc byte[largeEncodedData.Length + i];

                var randBytes = new byte[i];

                rand.NextBytes(randBytes);

                largeEncodedData.CopyTo(testData);

                new Span <byte>(randBytes).CopyTo(testData.Slice(largeEncodedData.Length));

                Rs256Algorithms.RemoveAndCorrectFecInPlace(ref testData, encoder, decoder, largeEncodedData.Length - 4, 4, out _);                 // There are 4 junk bytes at then end. The other 4 is for possible MIC32

                Assert.That(testData.Length, Is.EqualTo(LargeDecodedData.Length));

                //Assert.That(testData.SequenceEqual(LargeDecodedData));

                Assert.That(testData.ToArray(), Is.EqualTo(LargeDecodedData));
            }
        }
Ejemplo n.º 2
0
        public void Test1()
        {
            var data = new byte[52]
            {
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x20,
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x20,
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x20,
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x20,
                0xE4, 0x81, 0x13, 0xE0
            };

            var encoder = new Rs256EncoderLittleEndian(64, 60, new GaloisField256(0x11D));

            // Verify that encoder doesn't keep a state
            var checksum = new byte[4];

            encoder.CalculateParity(data, checksum);

            encoder.CalculateParity(data, checksum);

            Assert.That(checksum, Is.EqualTo(new byte[] { 0x79, 0x9b, 0x9a, 0xee }));

            Span <byte> toBeCorrected = stackalloc byte[data.Length + 4];

            data.CopyTo(toBeCorrected);
            checksum.CopyTo(toBeCorrected.Slice(data.Length));
            toBeCorrected[0] = 255;

            Rs256Algorithms.RemoveAndCorrectFecInPlace(ref toBeCorrected, encoder, new Rs256DecoderLittleEndian(64, 60, new GaloisField256(0x11D)), data.Length, 0, out _);

            Assert.That(toBeCorrected.ToArray(), Is.EqualTo(data.ToArray()));
        }
Ejemplo n.º 3
0
        public void Test6()
        {
            var decoder = new Rs256DecoderLittleEndian(64, 60, new GaloisField256(0x11D));
            var encoder = new Rs256EncoderLittleEndian(64, 60, new GaloisField256(0x11D));

            ReadOnlySpan <byte> data = LargeEncodedData;

            Span <byte> testData = data.ToArray();

            Rs256Algorithms.RemoveAndCorrectFecInPlace(ref testData, encoder, decoder, data.Length - 4, 4, out _);             // There are 4 junk bytes at then end. The other 4 is for possible MIC32

            Assert.That(testData.Length, Is.EqualTo(2590));

            Assert.That(testData.SequenceEqual(LargeDecodedData));
        }
Ejemplo n.º 4
0
        public void Test5()
        {
            var data = new byte[52 + 4]
            {
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x20,
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x20,
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x20,
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x20,
                0xE4, 0x81, 0x13, 0xE0,
                0x79, 0x9b, 0x9a, 0xee
            };

            var decoder = new Rs256DecoderLittleEndian(64, 60, new GaloisField256(0x11D));
            var encoder = new Rs256EncoderLittleEndian(64, 60, new GaloisField256(0x11D));

            int MaxExtraBytes = 64 - data.Length;

            var rand = new Random(1);

            var randbytes = new byte[MaxExtraBytes];

            int searchCount = 100;

            var results = new Rs256Algorithms.FixedList <int>(stackalloc int[64]);

            for (var i = 0; i < searchCount; ++i)
            {
                rand.NextBytes(randbytes);

                var tooLongData = new byte[data.Length + MaxExtraBytes];

                Buffer.BlockCopy(data, 0, tooLongData, 0, data.Length);
                Buffer.BlockCopy(randbytes, 0, tooLongData, data.Length, MaxExtraBytes);

                var testData = new Span <byte>(tooLongData, 0, tooLongData.Length);


                Rs256Algorithms.SearchLengths(testData, encoder, decoder, ref results);
                Assert.That(results.ToArray(), Does.Contain(data.Length));

                if (results.Length > 1)
                {
                    Console.WriteLine($"Found {results.Length} possibilities: {string.Join(", ", results.ToArray())}");
                }

                results.Clear();
            }
        }
Ejemplo n.º 5
0
        public void Test_NothingToBeFound()
        {
            var data = new byte[52];

            var rand = new Random(1);

            rand.NextBytes(data);

            Span <byte> toBeCorrected = stackalloc byte[data.Length];

            data.CopyTo(toBeCorrected);

            Rs256Algorithms.RemoveAndCorrectFecInPlace(ref toBeCorrected, new Rs256EncoderLittleEndian(64, 60, new GaloisField256(0x11D)), new Rs256DecoderLittleEndian(64, 60, new GaloisField256(0x11D)), data.Length, 0, out _);

            Assert.That(toBeCorrected.ToArray(), Is.EqualTo(new ReadOnlySpan <byte>(data).Slice(0, data.Length - 4).ToArray()));
        }