473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can you compute hash on large amount of data?

I tried to follow this advice, but I still run out of memory.

http://bytes.com/topic/visual-basic-net/answers/363011-md5-large-files

Basically, I am reading all the bytes of a 1 gb disk and I want to try and do a md5 hash on it. However, when following this example, my cryptostream gets too large a System out of memory exception.

Is there another way to do the md5 hash over some much data?

I am currently reading it into 10 million bytes arrays at a time. Can I hash the first array, and then use it as a seed for the next array, and so one, like you might do to a CRC32 calculation?

Any help would be appreciated.
Jan 31 '11 #1
2 2904
Rabbit
12,516 Expert Mod 8TB
Can you post your code?

You won't be able to feed the result of one hash into another because of the preprocessing that is done for MD5 hashes. However, you can do the hash append yourself. You will not get an officially correct MD5 hash but as long as you hash using the same function, the essence is the same.

Also, I found this on google.
Expand|Select|Wrap|Line Numbers
  1. Imports System.Security.Cryptography
  2. Imports System.Text
  3. Imports System.IO
  4.  
  5. Module RijndaelSample
  6.  
  7.     Sub Main()
  8.         Try
  9.             ' Create a new Rijndael object to generate a key
  10.             ' and initialization vector (IV).
  11.             Dim RijndaelAlg As Rijndael = Rijndael.Create
  12.  
  13.             ' Create a string to encrypt.
  14.             Dim sData As String = "Here is some data to encrypt."
  15.             Dim FileName As String = "CText.txt"
  16.  
  17.             ' Encrypt text to a file using the file name, key, and IV.
  18.             EncryptTextToFile(sData, FileName, RijndaelAlg.Key, RijndaelAlg.IV)
  19.  
  20.             ' Decrypt the text from a file using the file name, key, and IV.
  21.             Dim Final As String = DecryptTextFromFile(FileName, RijndaelAlg.Key, RijndaelAlg.IV)
  22.  
  23.             ' Display the decrypted string to the console.
  24.             Console.WriteLine(Final)
  25.         Catch e As Exception
  26.             Console.WriteLine(e.Message)
  27.         End Try
  28.  
  29.         Console.ReadLine()
  30.  
  31.     End Sub
  32.  
  33.  
  34.     Sub EncryptTextToFile(ByVal Data As String, ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte)
  35.         Try
  36.             ' Create or open the specified file.
  37.             Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
  38.  
  39.             ' Create a new Rijndael object.
  40.             Dim RijndaelAlg As Rijndael = Rijndael.Create
  41.  
  42.             ' Create a CryptoStream using the FileStream 
  43.             ' and the passed key and initialization vector (IV).
  44.             Dim cStream As New CryptoStream(fStream, _
  45.                                            RijndaelAlg.CreateEncryptor(Key, IV), _
  46.                                            CryptoStreamMode.Write)
  47.  
  48.             ' Create a StreamWriter using the CryptoStream.
  49.             Dim sWriter As New StreamWriter(cStream)
  50.  
  51.             Try
  52.  
  53.                 ' Write the data to the stream 
  54.                 ' to encrypt it.
  55.                 sWriter.WriteLine(Data)
  56.             Catch e As Exception
  57.  
  58.                 Console.WriteLine("An error occurred: {0}", e.Message)
  59.  
  60.             Finally
  61.  
  62.                 ' Close the streams and
  63.                 ' close the file.
  64.                 sWriter.Close()
  65.                 cStream.Close()
  66.                 fStream.Close()
  67.  
  68.             End Try
  69.         Catch e As CryptographicException
  70.             Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
  71.         Catch e As UnauthorizedAccessException
  72.             Console.WriteLine("A file error occurred: {0}", e.Message)
  73.         End Try
  74.     End Sub
  75.  
  76.  
  77.     Function DecryptTextFromFile(ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte) As String
  78.         Try
  79.             ' Create or open the specified file. 
  80.             Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
  81.  
  82.             ' Create a new Rijndael object.
  83.             Dim RijndaelAlg As Rijndael = Rijndael.Create
  84.  
  85.             ' Create a CryptoStream using the FileStream 
  86.             ' and the passed key and initialization vector (IV).
  87.             Dim cStream As New CryptoStream(fStream, _
  88.                                             RijndaelAlg.CreateDecryptor(Key, IV), _
  89.                                             CryptoStreamMode.Read)
  90.  
  91.             ' Create a StreamReader using the CryptoStream.
  92.             Dim sReader As New StreamReader(cStream)
  93.  
  94.             ' Read the data from the stream 
  95.             ' to decrypt it.
  96.             Dim val As String = Nothing
  97.  
  98.             Try
  99.  
  100.                 val = sReader.ReadLine()
  101.  
  102.             Catch e As Exception
  103.                 Console.WriteLine("An Cerror occurred: {0}", e.Message)
  104.             Finally
  105.                 ' Close the streams and
  106.                 ' close the file.
  107.                 sReader.Close()
  108.                 cStream.Close()
  109.                 fStream.Close()
  110.  
  111.  
  112.             End Try
  113.  
  114.             ' Return the string. 
  115.             Return val
  116.  
  117.         Catch e As CryptographicException
  118.             Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
  119.             Return Nothing
  120.         Catch e As UnauthorizedAccessException
  121.             Console.WriteLine("A file error occurred: {0}", e.Message)
  122.             Return Nothing
  123.         End Try
  124.     End Function
  125. End Module
If push comes to shove and nothing else works, and you must have an official MD5 hash, you can implement the algorithm yourself.
Jan 31 '11 #2
Thanks for your reply, but I think I found a better way of doing it. This is what I did if anyone is interested based on msdn.

This snippet assume 10 total large blocks of date to hash together. Readsize is number of elements in array, i.e. the length.

Using the following as a basis, I was able to computer a md5 hash over 1gb of data.


Dim md5Hash As MD5CryptoServiceProvider = New MD5CryptoServiceProvider

dim arrRead() as byte
dim arrHash() as byte

for i = 1 to 9
arrRead = ReadDataIntoDataBlock(i)
md5Hash.TransformBlock(arrRead, 0, ReadSize, arrRead, 0)
next i


'For the last, in this case, 10th block use
'TransformFinalBlock.

arrRead = ReadDataIntoDataBlock(10)
arrRead = md5Hash.TransformFinalBlock(arrRead, 0, ReadSize)
arrHash = md5Hash.Hash


'arrHash now contains 16 byte hash
Feb 2 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ice Man | last post by:
Hi All, I will need to send bewteen 2 asp pages very large amounts of data what is the best way to send it and to read it? for ex. I know this method: For i = 1 To Request.QueryString.Count...
2
by: steve | last post by:
Hi, I have researched but have not found a good solution to this problem. I am importing large amounts of data (over 50 Meg) into a new mysql db that I set up. I use >mysql dbname <...
5
by: Mike | last post by:
This is a general question on the best way to import a large amount of data to a MS-SQL DB. I can have the data in just about any format I need to, I just don't know how to import the data. I...
1
by: Bart | last post by:
Dear all, I would like to encrypt a large amount of data by using public/private keys, but I read on MSDN: "Symmetric encryption is performed on streams and is therefore useful to encrypt large...
4
by: loretta | last post by:
I have data within an xml tag that is being truncated when being read into a javascript variable. On Firefox, I am only getting up to 4096 characters. On IE, I am getting 31324 characters. I can...
4
by: eggie5 | last post by:
OK, so I'm trying to get a large amount of text back to my ASP script on the server. My large amount of text is the source to a web page, and I want to preserve the formatting on it, ie the...
2
by: Pja | last post by:
hi.. i need some help here.. how do i display large amount of data in vb6.0 in a table form that can be scrolled and to include print functions.. i am using vb6.0 as my front end and ms sql server...
16
by: Jack | last post by:
I need to process large amount of data. The data structure fits well in a dictionary but the amount is large - close to or more than the size of physical memory. I wonder what will happen if I try...
3
by: AnishAbs | last post by:
Hi , I have got a senario in which large amount of data should be copied from MS excel to MS SQL server. which is the best option to do so. Because when I use recordsets the process is very slow and...
3
by: Ale | last post by:
Hi all, how can I execute a large amount of DDL Queries on Access 2000? I've a DDl/SQL Script file which defines a data structure with CREATE TABLE, ALTER TABLE UPDATE TABLE, etc. etc. I know...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.