示例#1
0
        public bool GetSectorData(long sector, out byte[] data, out byte[] hash, out byte[] hashCodeResult)
        {
            if (sector < 0)
            {
                throw new ArgumentOutOfRangeException("sector");
            }
            if (sector > m_Sectors)
            {
                throw new ArgumentOutOfRangeException("sector");
            }

            if (!File.Exists(m_FilePath) || File.GetLastWriteTime(m_FilePath) != m_LastWriteTime)
            {
                data           = null;
                hash           = null;
                hashCodeResult = null;
                return(false);
            }
            try
            {
                byte[]     readData   = new byte[32768];
                FileStream fileStream = new FileStream(m_FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                fileStream.Position = sector * 32768;
                int count;
                if (fileStream.Position + 32768 <= fileStream.Length)
                {
                    count = 32768;
                }
                else
                {
                    count = (int)(fileStream.Length - fileStream.Position);
                }
                fileStream.Read(readData, 0, count);
                fileStream.Close();
                data = readData;
                hash = ComputeHashes.SHA512Compute(data);
                // Sicherer Hash ANFANG
                hashCodeResult = new byte[64];
                for (int n = 0; n < 64; n++)
                {
                    hashCodeResult[n] = (byte)(hash[n] ^ m_FileHash[n]);
                }
                hashCodeResult = ComputeHashes.SHA512Compute(hashCodeResult);
                hashCodeResult = ComputeHashes.SHA512Compute(hashCodeResult);
                hashCodeResult = ComputeHashes.SHA512Compute(hashCodeResult);
                hashCodeResult = ComputeHashes.SHA512Compute(hashCodeResult);
                // Sicherer Hash ENDE
                return(true);
            }
            catch (Exception ex)
            {
                m_Logger.Log(ex, "An exception was thrown while reading from shared file '{0}'!", m_FileName);
                data           = null;
                hash           = null;
                hashCodeResult = null;
                return(false);
            }
        }
示例#2
0
        public SharedFile(string filePath, byte[] fileHash, RIndexedHashtable <string, string> metaData, string comment, byte rating, DateTime?lastRequest)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (fileHash == null)
            {
                throw new ArgumentNullException("fileHash");
            }
            if (fileHash.Length != 64)
            {
                throw new ArgumentException();
            }
            if (rating > 3)
            {
                throw new ArgumentOutOfRangeException("rating");
            }
            if (comment == null)
            {
                throw new ArgumentNullException("comment");
            }

            m_FilePath                   = filePath;
            m_DirectoryPath              = Path.GetDirectoryName(m_FilePath);
            m_FileName                   = Path.GetFileName(m_FilePath);
            m_FileHash                   = fileHash;
            m_FileHashString             = Core.ByteArrayToString(m_FileHash);
            m_OnceHashedFileHash         = ComputeHashes.SHA512Compute(m_FileHash);
            m_OnceHashedFileHashString   = Core.ByteArrayToString(m_OnceHashedFileHash);
            m_TwiceHashedFileHash        = ComputeHashes.SHA512Compute(m_OnceHashedFileHash);
            m_TwiceHashedFileHashString  = Core.ByteArrayToString(m_TwiceHashedFileHash);
            m_ThriceHashedFileHash       = ComputeHashes.SHA512Compute(m_TwiceHashedFileHash);
            m_ThriceHashedFileHashString = Core.ByteArrayToString(m_ThriceHashedFileHash);
            m_FileSize                   = new FileInfo(m_FilePath).Length;
            m_FileSizeString             = Core.LengthToString(m_FileSize);
            m_Sectors = m_FileSize / 32768;
            if (metaData.IsEmpty)
            {
                try
                {
                    switch (Path.GetExtension(m_FilePath))
                    {
                    case ".mp3":
                        FileStream     fileStream = new FileStream(m_FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                        ID3v1TagReader id3v1      = new ID3v1TagReader(fileStream);
                        if (id3v1.HasTag)
                        {
                            if (id3v1.HasAlbum && id3v1.Album.Trim() != string.Empty)
                            {
                                m_MetaData.Add("Album", id3v1.Album.Trim());
                            }
                            if (id3v1.HasArtist && id3v1.Artist.Trim() != string.Empty)
                            {
                                m_MetaData.Add("Artist", id3v1.Artist.Trim());
                            }
                            if (id3v1.HasTitle && id3v1.Title.Trim() != string.Empty)
                            {
                                m_MetaData.Add("Title", id3v1.Title.Trim());
                            }
                        }
                        ID3v2TagTextFrameReader id3v2 = new ID3v2TagTextFrameReader(fileStream);
                        if (id3v2.HasTag)
                        {
                            if (id3v2.Frames.ContainsKey("TALB") && id3v2.Frames["TALB"].Trim() != string.Empty)
                            {
                                m_MetaData.Add("TALB", id3v2.Frames["TALB"].Trim());
                            }
                            if (id3v2.Frames.ContainsKey("TPE1") && id3v2.Frames["TPE1"].Trim() != string.Empty)
                            {
                                m_MetaData.Add("TPE1", id3v2.Frames["TPE1"].Trim());
                            }
                            if (id3v2.Frames.ContainsKey("TIT2") && id3v2.Frames["TIT2"].Trim() != string.Empty)
                            {
                                m_MetaData.Add("TIT2", id3v2.Frames["TIT2"].Trim());
                            }
                        }
                        fileStream.Close();
                        break;

                    default:
                        FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(m_FilePath);
                        if (fileVersionInfo.CompanyName != null && fileVersionInfo.CompanyName.Trim() != string.Empty)
                        {
                            m_MetaData.Add("CompanyName", fileVersionInfo.CompanyName.Trim());
                        }
                        if (fileVersionInfo.ProductName != null && fileVersionInfo.ProductName.Trim() != string.Empty)
                        {
                            m_MetaData.Add("ProductName", fileVersionInfo.ProductName.Trim());
                        }
                        break;
                    }
                }
                catch (Exception ex)
                {
                    m_Logger.Log(ex, "An Exception was thrown while gathering meta data!");
                }
            }
            else
            {
                m_MetaData = metaData;
            }
            Core.ParseMetaData(m_MetaData, out m_Album, out m_Artist, out m_Title);
            m_Rating        = rating;
            m_Comment       = comment;
            m_LastRequest   = lastRequest;
            m_LastWriteTime = File.GetLastWriteTime(m_FilePath);
        }