Пример #1
0
        public static FileDescriptor SaveToDisk(string path, byte[] contents, bool overwrite = false)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            if (overwrite && File.Exists(realPath))
            {
                File.Delete(realPath);
            }
            using (Stream outputStream = new FileStream(realPath, FileMode.CreateNew))
            {
                outputStream.Write(contents, 0, contents.Length);
            }
            FileGetFileDescriptorArgument fileGetFileDecriptorArgument = new FileGetFileDescriptorArgument
            {
                Path             = realPath,
                IncludePathInfo  = true,
                IncludeAttribute = true,
                IncludeMd5       = true,
                IncludeSha1      = true,
                IncludeSha256    = true,
                IncludeSha512    = true
            };
            FileDescriptor fileDescriptor = GetFileDescriptor(fileGetFileDecriptorArgument);

            return(fileDescriptor);
        }
Пример #2
0
        public static FileDescriptor SaveToDisk(string path, Stream stream, bool overwrite = false)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            if (overwrite && File.Exists(realPath))
            {
                File.Delete(realPath);
            }
            using (Stream outputStream = new FileStream(realPath, FileMode.CreateNew))
            {
                if (stream.CanSeek)
                {
                    stream.Position = 0;
                }
                stream.CopyTo(outputStream, TfxCentral.BufferLength);
            }
            FileGetFileDescriptorArgument fileGetFileDecriptorArgument = new FileGetFileDescriptorArgument
            {
                Path             = realPath,
                IncludePathInfo  = true,
                IncludeAttribute = true,
                IncludeMd5       = true,
                IncludeSha1      = true,
                IncludeSha256    = true,
                IncludeSha512    = true
            };
            FileDescriptor fileDescriptor = GetFileDescriptor(fileGetFileDecriptorArgument);

            return(fileDescriptor);
        }
Пример #3
0
        public static FileDescriptor GetFileDescriptor(string path, bool includeHash = false)
        {
            FileGetFileDescriptorArgument fileGetFileDecriptorArgument = new FileGetFileDescriptorArgument
            {
                Path             = path,
                IncludePathInfo  = true,
                IncludeAttribute = true
            };

            if (includeHash)
            {
                fileGetFileDecriptorArgument.IncludeMd5    = true;
                fileGetFileDecriptorArgument.IncludeSha1   = true;
                fileGetFileDecriptorArgument.IncludeSha256 = true;
                fileGetFileDecriptorArgument.IncludeSha512 = true;
            }
            FileDescriptor result = GetFileDescriptor(fileGetFileDecriptorArgument);

            return(result);
        }
Пример #4
0
        internal static FileDescriptor GetFileDescriptor(FileGetFileDescriptorArgument args)
        {
            string       realPath   = PathExpress.ResolvePath(args.Path);
            string       parent     = Path.GetDirectoryName(realPath);
            string       fullName   = Path.GetFileName(realPath);
            string       name       = Path.GetFileNameWithoutExtension(fullName);
            string       extension  = Path.GetExtension(fullName);
            FileMetadata metadata   = null;
            BytesEx      md5Hash    = null;
            BytesEx      sha1Hash   = null;
            BytesEx      sha256Hash = null;
            BytesEx      sha512Hash = null;

            if (args.IncludeAttribute)
            {
                FileInfo fileInfo     = new FileInfo(realPath);
                bool     isArchive    = fileInfo.Attributes.HasFlag(FileAttributes.Archive);
                bool     isReadOnly   = fileInfo.Attributes.HasFlag(FileAttributes.ReadOnly);
                bool     isHidden     = fileInfo.Attributes.HasFlag(FileAttributes.Hidden);
                bool     isSystem     = fileInfo.Attributes.HasFlag(FileAttributes.System);
                long     length       = fileInfo.Length;
                DateTime createdTime  = fileInfo.CreationTimeUtc;
                DateTime modifiedTime = fileInfo.LastWriteTimeUtc;
                DateTime accessTime   = fileInfo.LastAccessTimeUtc;
                metadata = new FileMetadata(isArchive, isHidden, isReadOnly, isSystem, length, createdTime, accessTime, modifiedTime);
            }
            if (args.IncludeMd5 || args.IncludeSha1 || args.IncludeSha256 || args.IncludeSha512)
            {
                string md5    = "md5";
                string sha1   = "sha1";
                string sha256 = "sha256";
                string sha512 = "sha512";
                Dictionary <string, System.Security.Cryptography.HashAlgorithm> cryptDictionary = new Dictionary <string, System.Security.Cryptography.HashAlgorithm>();
                if (args.IncludeMd5)
                {
                    cryptDictionary[md5] = HashExpress.GetAlgorithm(HashAlgorithm.Md5);
                }
                if (args.IncludeSha1)
                {
                    cryptDictionary[sha1] = HashExpress.GetAlgorithm(HashAlgorithm.Sha1);
                }
                if (args.IncludeSha256)
                {
                    cryptDictionary[sha256] = HashExpress.GetAlgorithm(HashAlgorithm.Sha256);
                }
                if (args.IncludeSha512)
                {
                    cryptDictionary[sha512] = HashExpress.GetAlgorithm(HashAlgorithm.Sha512);
                }
                System.Security.Cryptography.HashAlgorithm[] crypts = cryptDictionary.Values.ToArray();
                using (Stream fileStream = new FileStream(realPath, FileMode.Open))
                {
                    byte[] buffer = new byte[HashExpress.StreamBufferSize];
                    int    read;
                    long   currentPosition = 0L;
                    long   streamLength    = fileStream.Length;
                    while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        currentPosition += read;
                        foreach (System.Security.Cryptography.HashAlgorithm crypt in crypts)
                        {
                            if (currentPosition < streamLength)
                            {
                                int _ = crypt.TransformBlock(buffer, 0, read, buffer, 0);
                            }
                            else
                            {
                                byte[] _ = crypt.TransformFinalBlock(buffer, 0, read);
                            }
                        }
                    }
                    if (args.IncludeMd5)
                    {
                        md5Hash = BytesEx.FromBytes(cryptDictionary[md5].Hash);
                    }
                    if (args.IncludeSha1)
                    {
                        sha1Hash = BytesEx.FromBytes(cryptDictionary[sha1].Hash);
                    }
                    if (args.IncludeSha256)
                    {
                        sha256Hash = BytesEx.FromBytes(cryptDictionary[sha256].Hash);
                    }
                    if (args.IncludeSha512)
                    {
                        sha512Hash = BytesEx.FromBytes(cryptDictionary[sha512].Hash);
                    }
                }
            }
            FileDescriptor result = new FileDescriptor(name, extension, parent, metadata, md5Hash, sha1Hash, sha256Hash, sha512Hash);

            return(result);
        }