Does anyone care to explain what is the idea behind the init-update-final functions of cartographic hash functions (e.g. SHA*_Init, SHA*_Update, SHA*_Final from OpenSSL)?
Asked
Active
Viewed 2,235 times
4
Mike Edward Moras
- 18,161
- 12
- 87
- 240
carobnodrvo
- 143
- 4
1 Answers
9
Many libraries, like Java and OpenSSL, provide an Init-Update-Final model for transformations. It's used to minimize memory requirements.
Let's suppose all you have is a single shot SHA-1 function which requires the input to be available.
Then, for example, with a 1GB RAM system, how would you hash a file of 2 GB ?
You simply cannot as the SHA-1 function will require to have the whole input in RAM and you simply can't do it.
What you can do, instead, is to call SHA_Init, read 1MB of the file into RAM, SHA_Update that 1MB, read the next 1MB of the file (overwriting the previous one), call SHA_Update on that 1MB, repeat 1998 more times and then call SHA_Final.
This requires only 1MB RAM to hash a 2GB file.
Mike Edward Moras
- 18,161
- 12
- 87
- 240
Ruggero
- 7,339
- 33
- 42