Exemple #1
0
        private async Task CalculateHash(
            IDispatcher d,
            HashProgress notifier)
        {
            if (notifier != null)
            {
                Action action = () => notifier(Path, 0);
                if (d == null)
                {
                    action();
                }
                else
                {
                    d.Execute(action);
                }
            }

            //TODO - exception handling
            StorageFile file = await GetFromRoot(m_path, m_root);
            int num_chunks = (int) (GetSize()/Chunker.chunk_size) + 1;
            int hash_size = num_chunks*32;
            float current_chunk = 0;
            var hash_builder = new StringBuilder(hash_size);
            m_hash = "";

            var chunker = new Chunker(GetSize());

            foreach (Chunk chunk in chunker.GetChunks())
            {
                using (IRandomAccessStream inputStream = await file.OpenAsync(FileAccessMode.Read))
                {
                    using (var dataReader = new DataReader(inputStream.GetInputStreamAt(chunk.Start)))
                    {
                        await dataReader.LoadAsync(chunk.Length);
                        IBuffer buf = dataReader.ReadBuffer(chunk.Length);
                        IBuffer hashed = m_alg.HashData(buf);
                        hash_builder.Append(CryptographicBuffer.EncodeToHexString(hashed));
                    }
                }
                current_chunk++;


                if (notifier != null)
                {
                    float percent_done = current_chunk/num_chunks;
                    Action action = () => notifier(Path, percent_done*100);
                    if (d == null)
                    {
                        action();
                    }
                    else
                    {
                        d.Execute(action);
                    }
                }
            }

            m_hash = hash_builder.ToString();

            if (hash_size > 32) //hash the hash 
            {
                // Convert the string to UTF8 binary data.
                IBuffer hashbuf = CryptographicBuffer.ConvertStringToBinary(m_hash, BinaryStringEncoding.Utf8);
                IBuffer hashed = m_alg.HashData(hashbuf);
                m_hash = CryptographicBuffer.EncodeToHexString(hashed);
            }

            if (notifier != null)
            {
                Action action = () => notifier(Path, 100);
                if (d == null)
                {
                    action();
                }
                else
                {
                    d.Execute(action);
                }
            }
        }
Exemple #2
0
        private async Task CalculateHash(
            IDispatcher d,
            HashProgress notifier)
        {
            if (notifier != null)
            {
                Action action = () => notifier(Path, 0);
                if (d == null)
                {
                    action();
                }
                else
                {
                    d.Execute(action);
                }
            }

            //TODO - exception handling
            StorageFile file = await GetFromRoot(m_path, m_root);

            int   num_chunks    = (int)(GetSize() / Chunker.chunk_size) + 1;
            int   hash_size     = num_chunks * 32;
            float current_chunk = 0;
            var   hash_builder  = new StringBuilder(hash_size);

            m_hash = "";

            var chunker = new Chunker(GetSize());

            foreach (Chunk chunk in chunker.GetChunks())
            {
                using (IRandomAccessStream inputStream = await file.OpenAsync(FileAccessMode.Read))
                {
                    using (var dataReader = new DataReader(inputStream.GetInputStreamAt(chunk.Start)))
                    {
                        await dataReader.LoadAsync(chunk.Length);

                        IBuffer buf    = dataReader.ReadBuffer(chunk.Length);
                        IBuffer hashed = m_alg.HashData(buf);
                        hash_builder.Append(CryptographicBuffer.EncodeToHexString(hashed));
                    }
                }
                current_chunk++;


                if (notifier != null)
                {
                    float  percent_done = current_chunk / num_chunks;
                    Action action       = () => notifier(Path, percent_done * 100);
                    if (d == null)
                    {
                        action();
                    }
                    else
                    {
                        d.Execute(action);
                    }
                }
            }

            m_hash = hash_builder.ToString();

            if (hash_size > 32) //hash the hash
            {
                // Convert the string to UTF8 binary data.
                IBuffer hashbuf = CryptographicBuffer.ConvertStringToBinary(m_hash, BinaryStringEncoding.Utf8);
                IBuffer hashed  = m_alg.HashData(hashbuf);
                m_hash = CryptographicBuffer.EncodeToHexString(hashed);
            }

            if (notifier != null)
            {
                Action action = () => notifier(Path, 100);
                if (d == null)
                {
                    action();
                }
                else
                {
                    d.Execute(action);
                }
            }
        }