コード例 #1
0
        private static string HexMD5(MD5 md5, string value, UTF8Encoding encoding)
        {
            var bytes = encoding.GetBytes(value);
            var hash  = md5.ComputeHash(bytes);

            return(BsonUtils.ToHexString(hash));
        }
コード例 #2
0
        /// <summary>
        /// Writes a BSON ObjectId to the writer.
        /// </summary>
        /// <param name="objectId">The ObjectId.</param>
        public override void WriteObjectId(ObjectId objectId)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
            {
                ThrowInvalidState("WriteObjectId", BsonWriterState.Value, BsonWriterState.Initial);
            }

            var bytes = ObjectId.Pack(objectId.Timestamp, objectId.Machine, objectId.Pid, objectId.Increment);

            WriteNameHelper(Name);
            switch (_jsonWriterSettings.OutputMode)
            {
            case JsonOutputMode.Strict:
                _textWriter.Write("{{ \"$oid\" : \"{0}\" }}", BsonUtils.ToHexString(bytes));
                break;

            case JsonOutputMode.Shell:
            default:
                _textWriter.Write("ObjectId(\"{0}\")", BsonUtils.ToHexString(bytes));
                break;
            }

            State = GetNextState();
        }
コード例 #3
0
        /// <summary>
        /// Writes a BSON ObjectId to the writer.
        /// </summary>
        /// <param name="timestamp">The timestamp.</param>
        /// <param name="machine">The machine hash.</param>
        /// <param name="pid">The PID.</param>
        /// <param name="increment">The increment.</param>
        public override void WriteObjectId(int timestamp, int machine, short pid, int increment)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
            {
                ThrowInvalidState("WriteObjectId", BsonWriterState.Value, BsonWriterState.Initial);
            }

            var bytes = ObjectId.Pack(timestamp, machine, pid, increment);

            switch (_jsonWriterSettings.OutputMode)
            {
            case JsonOutputMode.Strict:
            case JsonOutputMode.JavaScript:
                WriteStartDocument();
                WriteString("$oid", BsonUtils.ToHexString(bytes));
                WriteEndDocument();
                break;

            case JsonOutputMode.TenGen:
            case JsonOutputMode.Shell:
                WriteNameHelper(Name);
                _textWriter.Write(string.Format("ObjectId(\"{0}\")", BsonUtils.ToHexString(bytes)));
                break;
            }

            State = GetNextState();
        }
コード例 #4
0
        /// <summary>
        /// Checks whether a given database name is valid on this server.
        /// </summary>
        /// <param name="databaseName">The database name.</param>
        /// <param name="message">An error message if the database name is not valid.</param>
        /// <returns>True if the database name is valid; otherwise, false.</returns>
        public virtual bool IsDatabaseNameValid(string databaseName, out string message)
        {
            if (databaseName == null)
            {
                throw new ArgumentNullException("databaseName");
            }

            if (databaseName == "")
            {
                message = "Database name is empty.";
                return(false);
            }

            foreach (var c in databaseName)
            {
                if (__invalidDatabaseNameChars.Contains(c))
                {
                    var bytes = new byte[] { (byte)((int)c >> 8), (byte)((int)c & 255) };
                    var hex   = BsonUtils.ToHexString(bytes);
                    message = string.Format("Database name '{0}' is not valid. The character 0x{1} '{2}' is not allowed in database names.", databaseName, hex, c);
                    return(false);
                }
            }

            if (Encoding.UTF8.GetBytes(databaseName).Length > 64)
            {
                message = string.Format("Database name '{0}' exceeds 64 bytes (after encoding to UTF8).", databaseName);
                return(false);
            }

            message = null;
            return(true);
        }
コード例 #5
0
 // private methods
 private void ValidateDatabaseName(string name)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (name == "")
     {
         throw new ArgumentException("Database name is empty.");
     }
     foreach (var c in name)
     {
         if (__invalidDatabaseNameChars.Contains(c))
         {
             var bytes   = new byte[] { (byte)((int)c >> 8), (byte)((int)c & 255) };
             var hex     = BsonUtils.ToHexString(bytes);
             var message = string.Format("Database name '{0}' is not valid. The character 0x{1} '{2}' is not allowed in database names.", name, hex, c);
             throw new ArgumentException(message);
         }
     }
     if (Encoding.UTF8.GetBytes(name).Length > 64)
     {
         var message = string.Format("Database name '{0}' exceeds 64 bytes (after encoding to UTF8).", name);
         throw new ArgumentException(message);
     }
 }
コード例 #6
0
        public static string MongoPasswordDigest(string username, SecureString password)
        {
            using (var md5 = MD5.Create())
            {
                var bytes = Utf8Encodings.Strict.GetBytes(username + ":mongo:");

                IntPtr unmanagedPassword = IntPtr.Zero;
                try
                {
                    unmanagedPassword = Marshal.SecureStringToBSTR(password);
                    var      passwordChars       = new char[password.Length];
                    GCHandle passwordCharsHandle = new GCHandle();
                    try
                    {
                        passwordCharsHandle = GCHandle.Alloc(passwordChars, GCHandleType.Pinned);
                        Marshal.Copy(unmanagedPassword, passwordChars, 0, passwordChars.Length);

                        var      byteCount           = Utf8Encodings.Strict.GetByteCount(passwordChars);
                        var      passwordBytes       = new byte[byteCount];
                        GCHandle passwordBytesHandle = new GCHandle();
                        try
                        {
                            passwordBytesHandle = GCHandle.Alloc(passwordBytesHandle, GCHandleType.Pinned);
                            Utf8Encodings.Strict.GetBytes(passwordChars, 0, passwordChars.Length, passwordBytes, 0);

                            var buffer = new byte[bytes.Length + passwordBytes.Length];
                            Buffer.BlockCopy(bytes, 0, buffer, 0, bytes.Length);
                            Buffer.BlockCopy(passwordBytes, 0, buffer, bytes.Length, passwordBytes.Length);

                            return(BsonUtils.ToHexString(md5.ComputeHash(buffer)));
                        }
                        finally
                        {
                            Array.Clear(passwordBytes, 0, passwordBytes.Length);

                            if (passwordBytesHandle.IsAllocated)
                            {
                                passwordBytesHandle.Free();
                            }
                        }
                    }
                    finally
                    {
                        Array.Clear(passwordChars, 0, passwordChars.Length);

                        if (passwordCharsHandle.IsAllocated)
                        {
                            passwordCharsHandle.Free();
                        }
                    }
                }
                finally
                {
                    if (unmanagedPassword != IntPtr.Zero)
                    {
                        Marshal.ZeroFreeBSTR(unmanagedPassword);
                    }
                }
            }
        }
コード例 #7
0
 private void WriteExtendedJson(Newtonsoft.Json.JsonWriter writer, ObjectId value)
 {
     writer.WriteStartObject();
     writer.WritePropertyName("$oid");
     writer.WriteValue(BsonUtils.ToHexString(value.ToByteArray()));
     writer.WriteEndObject();
 }
コード例 #8
0
        /// <summary>
        /// Writes a BSON ObjectId to the writer.
        /// </summary>
        /// <param name="objectId">The ObjectId.</param>
        public override void WriteObjectId(ObjectId objectId)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
            {
                ThrowInvalidState("WriteObjectId", BsonWriterState.Value, BsonWriterState.Initial);
            }

            var bytes = objectId.ToByteArray();

            WriteNameHelper(Name);
            switch (_jsonWriterSettings.OutputMode)
            {
            case JsonOutputMode.CanonicalExtendedJson:
                //  _textWriter.Write("{{ \"$oid\" : \"{0}\" }}", BsonUtils.ToHexString(bytes));
                _textWriter.Write("\"{0}\"", BsonUtils.ToHexString(bytes));
                break;

            case JsonOutputMode.Shell:
            default:
                _textWriter.Write("ObjectId(\"{0}\")", BsonUtils.ToHexString(bytes));
                break;
            }

            State = GetNextState();
        }
コード例 #9
0
        private static string MongoPasswordDigest(string username, byte[] passwordBytes)
        {
            var prefixString = username + ":mongo:";
            var prefixBytes  = Utf8Encodings.Strict.GetBytes(prefixString);

            var buffer       = new byte[prefixBytes.Length + passwordBytes.Length];
            var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try
            {
                Buffer.BlockCopy(prefixBytes, 0, buffer, 0, prefixBytes.Length);
                Buffer.BlockCopy(passwordBytes, 0, buffer, prefixBytes.Length, passwordBytes.Length);

                using (var md5 = MD5.Create())
                {
                    var hash = md5.ComputeHash(buffer);
                    return(BsonUtils.ToHexString(hash));
                }
            }
            finally
            {
                Array.Clear(buffer, 0, buffer.Length);
                bufferHandle.Free();
            }
        }
コード例 #10
0
        private string GuidToString(BsonBinarySubType subType, byte[] bytes, GuidRepresentation guidRepresentation)
        {
            if (bytes.Length != 16)
            {
                var message = string.Format("Length of binary subtype {0} must be 16, not {1}.", subType, bytes.Length);
                throw new ArgumentException(message);
            }
            if (subType == BsonBinarySubType.UuidLegacy)
            {
                if (guidRepresentation == GuidRepresentation.Standard)
                {
                    throw new ArgumentException("GuidRepresentation for binary subtype UuidLegacy must not be Standard.");
                }
            }
            if (subType == BsonBinarySubType.UuidStandard)
            {
                if (guidRepresentation == GuidRepresentation.Unspecified)
                {
                    guidRepresentation = GuidRepresentation.Standard;
                }
                if (guidRepresentation != GuidRepresentation.Standard)
                {
                    var message = string.Format("GuidRepresentation for binary subtype UuidStandard must be Standard, not {0}.", guidRepresentation);
                    throw new ArgumentException(message);
                }
            }

            if (guidRepresentation == GuidRepresentation.Unspecified)
            {
                var s     = BsonUtils.ToHexString(bytes);
                var parts = new string[]
                {
                    s.Substring(0, 8),
                    s.Substring(8, 4),
                    s.Substring(12, 4),
                    s.Substring(16, 4),
                    s.Substring(20, 12)
                };
                return(string.Format("HexData({0}, \"{1}\")", (int)subType, string.Join("-", parts)));
            }
            else
            {
                string uuidConstructorName;
                switch (guidRepresentation)
                {
                case GuidRepresentation.CSharpLegacy: uuidConstructorName = "CSUUID"; break;

                case GuidRepresentation.JavaLegacy: uuidConstructorName = "JUUID"; break;

                case GuidRepresentation.PythonLegacy: uuidConstructorName = "PYUUID"; break;

                case GuidRepresentation.Standard: uuidConstructorName = "UUID"; break;

                default: throw new BsonInternalException("Unexpected GuidRepresentation");
                }
                var guid = GuidConverter.FromBytes(bytes, guidRepresentation);
                return(string.Format("{0}(\"{1}\")", uuidConstructorName, guid.ToString()));
            }
        }
コード例 #11
0
        public void TestToHexString()
        {
            var value    = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 255 };
            var expected = "000102030405060708090a0b0c0d0e0f10ff";
            var actual   = BsonUtils.ToHexString(value);

            Assert.AreEqual(expected, actual);
        }
コード例 #12
0
        public void ToHexString_should_throw_when_bytes_is_null()
        {
            var exception = Record.Exception(() => BsonUtils.ToHexString(null));

            var argumentNullException = exception.Should().BeOfType <ArgumentNullException>().Subject;

            argumentNullException.ParamName.Should().Be("bytes");
        }
コード例 #13
0
 /// <summary>
 /// Computes the hash value of the secured string
 /// </summary>
 private static string GenerateDigest(SecureString password)
 {
     using (var sha256 = SHA256.Create())
     {
         var hash = ComputeHash(sha256, new byte[0], password);
         return(BsonUtils.ToHexString(hash));
     }
 }
コード例 #14
0
 /// <summary>
 /// Computes the hash value of the secured string
 /// </summary>
 private static string GenerateDigest(SecureString secureString)
 {
     using (var sha256 = new SHA256Managed())
     {
         TransformFinalBlock(sha256, secureString);
         return(BsonUtils.ToHexString(sha256.Hash));
     }
 }
コード例 #15
0
 // public static methods
 /// <summary>
 /// Gets the MD5 hash of a string.
 /// </summary>
 /// <param name="text">The string to get the MD5 hash of.</param>
 /// <returns>The MD5 hash.</returns>
 public static string Hash(string text)
 {
     using (var md5 = MD5.Create())
     {
         var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
         return(BsonUtils.ToHexString(hash));
     }
 }
コード例 #16
0
 /// <summary>
 /// Computes the hash value of the secured string
 /// </summary>
 private static string GenerateDigest(SecureString secureString)
 {
     using (var sha256 = new SHA256CryptoServiceProvider())
     {
         var hash = ComputeHash(sha256, new byte[0], secureString);
         return(BsonUtils.ToHexString(hash));
     }
 }
コード例 #17
0
        public static string MongoPasswordDigest(string username, SecureString password)
        {
            using (var md5 = MD5.Create())
            {
                var bytes = __encoding.GetBytes(username + ":mongo:");
                md5.TransformBlock(bytes, 0, bytes.Length, null, 0);

                IntPtr unmanagedPassword = IntPtr.Zero;
                try
                {
                    unmanagedPassword = Marshal.SecureStringToBSTR(password);
                    var      passwordChars       = new char[password.Length];
                    GCHandle passwordCharsHandle = new GCHandle();
                    try
                    {
                        passwordCharsHandle = GCHandle.Alloc(passwordChars, GCHandleType.Pinned);
                        Marshal.Copy(unmanagedPassword, passwordChars, 0, passwordChars.Length);

                        var      byteCount           = __encoding.GetByteCount(passwordChars);
                        var      passwordBytes       = new byte[byteCount];
                        GCHandle passwordBytesHandle = new GCHandle();
                        try
                        {
                            passwordBytesHandle = GCHandle.Alloc(passwordBytesHandle, GCHandleType.Pinned);
                            __encoding.GetBytes(passwordChars, 0, passwordChars.Length, passwordBytes, 0);
                            md5.TransformFinalBlock(passwordBytes, 0, passwordBytes.Length);
                            return(BsonUtils.ToHexString(md5.Hash));
                        }
                        finally
                        {
                            Array.Clear(passwordBytes, 0, passwordBytes.Length);

                            if (passwordBytesHandle.IsAllocated)
                            {
                                passwordBytesHandle.Free();
                            }
                        }
                    }
                    finally
                    {
                        Array.Clear(passwordChars, 0, passwordChars.Length);

                        if (passwordCharsHandle.IsAllocated)
                        {
                            passwordCharsHandle.Free();
                        }
                    }
                }
                finally
                {
                    if (unmanagedPassword != IntPtr.Zero)
                    {
                        Marshal.ZeroFreeBSTR(unmanagedPassword);
                    }
                }
            }
        }
コード例 #18
0
        /// <summary>
        /// Downloads a GridFS file.
        /// </summary>
        /// <param name="stream">The destination stream.</param>
        /// <param name="fileInfo">The GridFS file.</param>
        public void Download(Stream stream, MongoGridFSFileInfo fileInfo)
        {
            using (_server.RequestStart(_settings.ReadPreference))
            {
                var connectionId = _server.RequestConnectionId;

                if (_settings.VerifyMD5 && fileInfo.MD5 == null)
                {
                    throw new MongoGridFSException(connectionId, "VerifyMD5 is true and file being downloaded has no MD5 hash.");
                }

                var database         = GetDatabase();
                var chunksCollection = GetChunksCollection(database);

                string md5Client = null;
                using (var md5Algorithm = _settings.VerifyMD5 ? MD5.Create() : null)
                {
                    var numberOfChunks = (fileInfo.Length + fileInfo.ChunkSize - 1) / fileInfo.ChunkSize;
                    for (var n = 0L; n < numberOfChunks; n++)
                    {
                        var query = Query.And(Query.EQ("files_id", fileInfo.Id), Query.EQ("n", n));
                        var chunk = chunksCollection.FindOne(query);
                        if (chunk == null)
                        {
                            string errorMessage = string.Format("Chunk {0} missing for GridFS file '{1}'.", n, fileInfo.Name);
                            throw new MongoGridFSException(connectionId, errorMessage);
                        }
                        var data = chunk["data"].AsBsonBinaryData;
                        if (data.Bytes.Length != fileInfo.ChunkSize)
                        {
                            // the last chunk only has as many bytes as needed to complete the file
                            if (n < numberOfChunks - 1 || data.Bytes.Length != fileInfo.Length % fileInfo.ChunkSize)
                            {
                                string errorMessage = string.Format("Chunk {0} for GridFS file '{1}' is the wrong size.", n, fileInfo.Name);
                                throw new MongoGridFSException(connectionId, errorMessage);
                            }
                        }
                        stream.Write(data.Bytes, 0, data.Bytes.Length);
                        if (_settings.VerifyMD5)
                        {
                            md5Algorithm.TransformBlock(data.Bytes, 0, data.Bytes.Length, null, 0);
                        }
                    }

                    if (_settings.VerifyMD5)
                    {
                        md5Algorithm.TransformFinalBlock(new byte[0], 0, 0);
                        md5Client = BsonUtils.ToHexString(md5Algorithm.Hash);
                    }
                }

                if (_settings.VerifyMD5 && !md5Client.Equals(fileInfo.MD5, StringComparison.OrdinalIgnoreCase))
                {
                    throw new MongoGridFSException(connectionId, "Download client and server MD5 hashes are not equal.");
                }
            }
        }
コード例 #19
0
        private static string Hash(string str)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(str);
            using (SHA256 algorithm = SHA256.Create())
            {
                var hash = algorithm.ComputeHash(bytes);

                return(BsonUtils.ToHexString(hash));
            }
        }
コード例 #20
0
 // internal methods
 /// <summary>
 /// Computes the MONGODB-CR password digest.
 /// </summary>
 /// <param name="username">The username.</param>
 /// <returns></returns>
 internal string ComputeMongoCRPasswordDigest(string username)
 {
     using (var md5 = MD5.Create())
     {
         var encoding    = Utf8Encodings.Strict;
         var prefixBytes = encoding.GetBytes(username + ":mongo:");
         var hash        = ComputeHash(md5, prefixBytes, _securePassword);
         return(BsonUtils.ToHexString(hash));
     }
 }
コード例 #21
0
        private string CreateKey(string username, SecureString password, string nonce)
        {
            var passwordDigest = AuthenticationHelper.MongoPasswordDigest(username, password);

            using (var md5 = MD5.Create())
            {
                var bytes = Utf8Encodings.Strict.GetBytes(nonce + username + passwordDigest);
                var hash  = md5.ComputeHash(bytes);
                return(BsonUtils.ToHexString(hash));
            }
        }
コード例 #22
0
 // internal methods
 /// <summary>
 /// Computes the MONGODB-CR password digest.
 /// </summary>
 /// <param name="username">The username.</param>
 /// <returns></returns>
 internal string ComputeMongoCRPasswordDigest(string username)
 {
     using (var md5 = MD5.Create())
     {
         var encoding    = new UTF8Encoding(false, true);
         var prefixBytes = encoding.GetBytes(username + ":mongo:");
         md5.TransformBlock(prefixBytes, 0, prefixBytes.Length, null, 0);
         TransformFinalBlock(md5, _securePassword);
         return(BsonUtils.ToHexString(md5.Hash));
     }
 }
コード例 #23
0
        private string CreateKey(string username, SecureString password, string nonce)
        {
            var passwordDigest = AuthenticationHelper.MongoPasswordDigest(username, password);

            using (var md5 = MD5.Create())
            {
                var bytes = new UTF8Encoding(false, true).GetBytes(nonce + username + passwordDigest);
                bytes = md5.ComputeHash(bytes);
                return(BsonUtils.ToHexString(bytes));
            }
        }
コード例 #24
0
        /// <summary>
        /// Computes the hash value of the secured string
        /// </summary>
        private static string GenerateDigest(SecureString secureString)
        {
#if NETCORE50 || NETSTANDARD1_5 || NETSTANDARD1_6
            using (var sha256 = SHA256.Create())
#else
            using (var sha256 = new SHA256CryptoServiceProvider())
#endif
            {
                var hash = ComputeHash(sha256, new byte[0], secureString);
                return(BsonUtils.ToHexString(hash));
            }
        }
コード例 #25
0
        public OperationResult Execute(CancellationToken cancellationToken)
        {
            try
            {
                var result = _bucket.DownloadAsBytes(_id, cancellationToken: cancellationToken);

                return(OperationResult.FromResult(BsonUtils.ToHexString(result)));
            }
            catch (Exception exception)
            {
                return(OperationResult.FromException(exception));
            }
        }
コード例 #26
0
        // protected methods
        protected override void CloseImplementation(CancellationToken cancellationToken)
        {
            if (_checkMD5 && _position == FileInfo.Length)
            {
                var md5 = BsonUtils.ToHexString(_md5.GetHashAndReset());
                if (!md5.Equals(FileInfo.MD5, StringComparison.OrdinalIgnoreCase))
                {
#pragma warning disable 618
                    throw new GridFSMD5Exception(_idAsBsonValue);
#pragma warning restore
                }
            }
        }
コード例 #27
0
        public async Task <OperationResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            try
            {
                var result = await _bucket.DownloadAsBytesAsync(_id, cancellationToken : cancellationToken);

                return(OperationResult.FromResult(BsonUtils.ToHexString(result)));
            }
            catch (Exception exception)
            {
                return(OperationResult.FromException(exception));
            }
        }
コード例 #28
0
        private static string GetSignature(string stringToSign, SecureString secretAccessKey, string date, string region, string service)
        {
            using (var decryptedSecureString = new DecryptedSecureString(secretAccessKey))
            {
                var aws4SecretAccessKeyChars = "AWS4".Concat(decryptedSecureString.GetChars()).ToArray();
                var aws4SecretAccessKeyBytes = Encoding.ASCII.GetBytes(aws4SecretAccessKeyChars);
                var kDateBlock = Hmac256(aws4SecretAccessKeyBytes, Encoding.ASCII.GetBytes(date));
                Array.Clear(aws4SecretAccessKeyChars, 0, aws4SecretAccessKeyChars.Length);
                Array.Clear(aws4SecretAccessKeyBytes, 0, aws4SecretAccessKeyBytes.Length);
                var kRegionBlock  = Hmac256(kDateBlock, Encoding.ASCII.GetBytes(region));
                var kServiceBlock = Hmac256(kRegionBlock, Encoding.ASCII.GetBytes(service));
                var kSigningBlock = Hmac256(kServiceBlock, Encoding.ASCII.GetBytes("aws4_request"));

                return(BsonUtils.ToHexString(Hmac256(kSigningBlock, Encoding.ASCII.GetBytes(stringToSign))));
            }
        }
コード例 #29
0
        /// <summary>
        /// Downloads a GridFS file.
        /// </summary>
        /// <param name="stream">The destination stream.</param>
        /// <param name="fileInfo">The GridFS file.</param>
        public void Download(
            Stream stream,
            MongoGridFSFileInfo fileInfo
            )
        {
            using (database.RequestStart()) {
                EnsureIndexes();

                string md5Client;
                using (var md5Algorithm = MD5.Create()) {
                    var numberOfChunks = (fileInfo.Length + fileInfo.ChunkSize - 1) / fileInfo.ChunkSize;
                    for (int n = 0; n < numberOfChunks; n++)
                    {
                        var query = Query.And(
                            Query.EQ("files_id", fileInfo.Id),
                            Query.EQ("n", n)
                            );
                        var chunk = chunks.FindOne(query);
                        if (chunk == null)
                        {
                            string errorMessage = string.Format("Chunk {0} missing for GridFS file '{1}'.", n, fileInfo.Name);
                            throw new MongoGridFSException(errorMessage);
                        }
                        var data = chunk["data"].AsBsonBinaryData;
                        if (data.Bytes.Length != fileInfo.ChunkSize)
                        {
                            // the last chunk only has as many bytes as needed to complete the file
                            if (n < numberOfChunks - 1 || data.Bytes.Length != fileInfo.Length % fileInfo.ChunkSize)
                            {
                                string errorMessage = string.Format("Chunk {0} for GridFS file '{1}' is the wrong size.", n, fileInfo.Name);
                                throw new MongoGridFSException(errorMessage);
                            }
                        }
                        stream.Write(data.Bytes, 0, data.Bytes.Length);
                        md5Algorithm.TransformBlock(data.Bytes, 0, data.Bytes.Length, null, 0);
                    }

                    md5Algorithm.TransformFinalBlock(new byte[0], 0, 0);
                    md5Client = BsonUtils.ToHexString(md5Algorithm.Hash);
                }

                if (!md5Client.Equals(fileInfo.MD5, StringComparison.OrdinalIgnoreCase))
                {
                    throw new MongoGridFSException("Download client and server MD5 hashes are not equal.");
                }
            }
        }
コード例 #30
0
        private BsonDocument CreateFilesCollectionDocument()
        {
            var uploadDateTime = DateTime.UtcNow;

            return(new BsonDocument
            {
                { "_id", _idAsBsonValue },
                { "length", _length },
                { "chunkSize", _chunkSizeBytes },
                { "uploadDate", uploadDateTime },
                { "md5", () => BsonUtils.ToHexString(_md5.GetHashAndReset()), !_disableMD5 },
                { "filename", _filename },
                { "contentType", _contentType, _contentType != null },
                { "aliases", () => new BsonArray(_aliases.Select(a => new BsonString(a))), _aliases != null },
                { "metadata", _metadata, _metadata != null }
            });
        }