Exemplo n.º 1
0
        /// <summary>
        /// Checks whether a byte array starts with a known header, containing the app name and an
        /// optional revision number.
        /// </summary>
        /// <param name="packedCipher">Byte array to examine.</param>
        /// <param name="expectedAppName">The header must start with this app name.</param>
        /// <param name="revision">reveives the revision number.</param>
        /// <returns>Returns true if the array starts with a valid header, otherwise false.</returns>
        public static bool HasMatchingHeader(byte[] packedCipher, string expectedAppName, out int revision)
        {
            if (packedCipher != null)
            {
                // test for app name
                if (ByteArrayExtensions.ContainsAt(packedCipher, CryptoUtils.StringToBytes(expectedAppName), 0))
                {
                    int position = expectedAppName.Length;

                    // Followed by separator?
                    if (ByteArrayExtensions.ContainsAt(packedCipher, (byte)Separator, position))
                    {
                        revision = 1;
                        return(true);
                    }

                    if (ByteArrayExtensions.ContainsAt(packedCipher, CryptoUtils.StringToBytes(RevisionSeparator), position))
                    {
                        int digitsStart = position + RevisionSeparator.Length;
                        int digitsEnd   = digitsStart;

                        // Skip digits
                        while (ByteArrayExtensions.ContainsDigitCharAt(packedCipher, digitsEnd))
                        {
                            digitsEnd++;
                        }

                        // Followed by separator?
                        if ((digitsEnd > digitsStart) &&
                            (ByteArrayExtensions.ContainsAt(packedCipher, (byte)Separator, digitsEnd)))
                        {
                            byte[] digitsPart = new byte[digitsEnd - digitsStart];
                            Array.Copy(packedCipher, digitsStart, digitsPart, 0, digitsPart.Length);
                            revision = int.Parse(CryptoUtils.BytesToString(digitsPart));
                            return(true);
                        }
                    }
                }
            }

            revision = 0;
            return(false);
        }
Exemplo n.º 2
0
        public void ByteArrayStartsWithHandlesNullAndEmpty()
        {
            byte[] haystack = null;
            byte[] needle   = null;
            Assert.Throws <ArgumentNullException>(delegate { ByteArrayExtensions.ContainsAt(haystack, needle, 0); });

            // Works the same way as string.StartsWith
            bool expectedResult = string.Empty.StartsWith(string.Empty);

            haystack = new byte[0];
            needle   = new byte[0];
            Assert.AreEqual(expectedResult, haystack.ContainsAt(needle, 0));
            Assert.IsTrue(expectedResult);

            expectedResult = "test".StartsWith(string.Empty);
            haystack       = new byte[] { 1, 2, 3 };
            needle         = new byte[0];
            Assert.AreEqual(expectedResult, haystack.ContainsAt(needle, 0));
            Assert.IsTrue(expectedResult);
        }