Example #1
0
 protected override void ValidatePassword(byte[] passwordHash)
 {
     if (passwordHash.BinaryCompareTo(_password) != 0)
     {
         throw LiteException.DatabaseWrongPassword();
     }
 }
Example #2
0
        public FileReaderV7(Stream stream, string password)
        {
            _stream = stream;

            // only userVersion was available in old file format versions
            _header = this.ReadPage(0);

            this.UserVersion = _header["userVersion"].AsInt32;

            if (password == null && _header["salt"].AsBinary.IsFullZero() == false)
            {
                throw new LiteException(0, "Current data file requires password");
            }
            else if (password != null)
            {
                if (_header["salt"].AsBinary.IsFullZero())
                {
                    throw new LiteException(0, "Current data file has no encryption - do not use password");
                }

                var hash = AesEncryption.HashSHA1(password);

                if (hash.SequenceEqual(_header["password"].AsBinary) == false)
                {
                    throw LiteException.DatabaseWrongPassword();
                }
            }

            _aes = password == null ? null : new AesEncryption(password, _header["salt"].AsBinary);
        }
Example #3
0
 /// <summary>
 /// To be override in Encripted disk
 /// </summary>
 protected virtual void ValidatePassword(byte[] passwordHash)
 {
     if (passwordHash.Any(b => b > 0))
     {
         throw LiteException.DatabaseWrongPassword();
     }
 }
Example #4
0
        /// <summary>
        /// Read page bytes from disk
        /// </summary>
        public virtual byte[] ReadPage(uint pageID)
        {
            var buffer   = new byte[BasePage.PAGE_SIZE];
            var position = BasePage.GetSizeOfPages(pageID);

            // position cursor
            if (_stream.Position != position)
            {
                _stream.Seek(position, SeekOrigin.Begin);
            }

            // read bytes from data file
            _stream.Read(buffer, 0, BasePage.PAGE_SIZE);

            // when reading the header, check the password
            if (pageID == 0 && _crypto != null)
            {
                // I know, header page will be double read (it's the price for isolated concerns)
                var header = (HeaderPage)BasePage.ReadPage(buffer);

                if (BinaryExtensions.BinaryCompareTo(_password, header.Password) != 0)
                {
                    throw LiteException.DatabaseWrongPassword();
                }
            }
            else if (_crypto != null)
            {
                buffer = _crypto.Decrypt(buffer);
            }

            return(buffer);
        }
        /// <summary>
        ///     Override read page decrypting data from disk
        /// </summary>
        public override byte[] ReadPage(uint pageID)
        {
            var buffer = base.ReadPage(pageID);

            // when read header, checks passoword
            if (pageID == 0)
            {
                // I know, header page will be double read (it's the price for isolated concerns)
                var header = (HeaderPage)BasePage.ReadPage(buffer);

                if (header.DbParams.Password.BinaryCompareTo(_password) != 0)
                {
                    throw LiteException.DatabaseWrongPassword();
                }

                return(buffer);
            }

            return(_crypto.Decrypt(buffer));
        }