/// <summary>
        ///     Gets the UTC from unix time.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns>The date and time as UTC</returns>
        private static DateTime GetUtcFromUnixTime(
            byte[] data)
        {
            if (data.Length != 4)
            {
                throw new InvalidDataException("Unix time must be a 32-bit integer");
            }

            var unixTime = BitConverter.ToInt32(data, 0);
            var netTime  = PasswordSafeUtility.GetUtcFromUnixTime(unixTime);

            return(netTime);
        }
        /// <summary>
        ///     Parses a password history from the database serialized format which is:
        ///     smmnnddddddddllll[passsword] ...
        ///     's' is a decimal status.
        ///     'mm' is a hex max number of password history entries
        ///     'nn' is a hex number of passwords in this list
        ///     The following is repeated 'nn' times:
        ///     'dddddddd' is a hex representation of 32-bit unix time when this password was changed.
        ///     'llll' is a hex representation of 16-bit length of the password that follows.
        ///     [password] is the string of the password.
        /// </summary>
        /// <param name="serialized">The serialized string</param>
        /// <returns>The decoded password history</returns>
        public static PasswordSafePasswordHistory Parse(
            string serialized)
        {
            if (serialized == null)
            {
                throw new ArgumentNullException("serialized");
            }

            var history = new PasswordSafePasswordHistory();

            if (serialized.Length < 5)
            {
                return(history);
            }

            history.Status     = byte.Parse(serialized.Substring(0, 1), NumberStyles.Integer, CultureInfo.InvariantCulture);
            history.MaxHistory = byte.Parse(serialized.Substring(1, 2), NumberStyles.HexNumber,
                                            CultureInfo.InvariantCulture);
            var numberOfPasswords = int.Parse(serialized.Substring(3, 2), NumberStyles.HexNumber,
                                              CultureInfo.InvariantCulture);
            var currentIndex = 5;

            for (var i = 0; i < numberOfPasswords; ++i)
            {
                var unixTimeChanged = int.Parse(serialized.Substring(currentIndex, 8), NumberStyles.HexNumber,
                                                CultureInfo.InvariantCulture);
                var passwordChangedUtc = PasswordSafeUtility.GetUtcFromUnixTime(unixTimeChanged);
                currentIndex += 8;

                var passwordLength = int.Parse(serialized.Substring(currentIndex, 4), NumberStyles.HexNumber,
                                               CultureInfo.InvariantCulture);
                currentIndex += 4;

                var password = serialized.Substring(currentIndex, passwordLength);
                currentIndex += passwordLength;

                history.PasswordHistoryAdd(new PasswordSafePassword(password, passwordChangedUtc));
            }

            return(history);
        }