Beispiel #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);
        }