SHA1 calculation for millions of strings having common prefix I have a function in which I need to calculate SHA1(common_prefix + random_string). I am calling this function many times so that calculation of SHA1 slows down the performance of the function. As for the all of the calls, I am using a common_prefix, is it possible to tweak SHA1 in a way that it won't calculate the hash for the (common_prefix+random_string), however it does something like a preprocessing for (common_prefix) and using this preprocess(calculated values) to calculate SHA1(common_prefix+random_string)?

Is it somehow possible to do it?
Yes, you can save effort, if the common prefix is at least one 512-bit block.

Speaking mathematically: For a padded message $m = m_1 \mathbin\| m_2 \mathbin\| \cdots \mathbin\| m_\ell$ of $\ell$ 512-bit blocks, $$\operatorname{SHA1}(m) = f(\cdots f(f(\mathit{iv}, m_1), m_2) \cdots, m_\ell),$$ where $f$ is SHA-1's internal compression function.  For example, suppose you have a pair of two-block messages $m_1 \mathbin\| m_2$ and $m_1 \mathbin\| m_2'$ with a common prefix $m_1$ and distinct suffixes $m_2$ and $m_2'$.  Then

\begin{align}
  \operatorname{SHA1}(m_1 \mathbin\| m_2) &amp;= f(f(\mathit{iv}, m_1), m_2), \\
  \operatorname{SHA1}(m_1 \mathbin\| m_2') &amp;= f(f(\mathit{iv}, m_1), m_2'),
\end{align}

which share the common subexpression $f(\mathit{iv}, m_1)$.  If you precompute $h_0 = f(\mathit{iv}, m_1)$, then you can more quickly compute $\operatorname{SHA1}(m_1 \mathbin\| m_2) = f(h_0, m_2)$ and $\operatorname{SHA1}(m_1 \mathbin\| m_2') = f(h_0, m_2')$.

Speaking programmatically: Most implementations of SHA-1 support an init/update/final API.  For example, to hash the string hello world with OpenSSL's API you might write:

SHA_CTX ctx;
unsigned char h[20];

SHA1_Init(&amp;ctx);
SHA1_Update(&amp;ctx, "hello", 5);
SHA1_Update(&amp;ctx, " ", 1);
SHA1_Update(&amp;ctx, "world", 5);
SHA1_Final(h, &amp;ctx);


The state of the SHA-1 computation, including buffering to split messages into blocks, is stored in the SHA_CTX object.  In some cases, like in OpenSSL, you can copy the context in order to save any effort that had already been done to hash what you've fed into it so far:

SHA_CTX ctx, ctx0, ctx1;
unsigned char h0[20], h1[20];

SHA1_Init(&amp;ctx);
SHA1_Update(&amp;ctx, "Once before a console dreary, while I programmed, weak and weary", 64);

ctx0 = ctx;
SHA1_Update(&amp;ctx0, "\nOver many a curious program which did TECO's buffer fill...", 60);
SHA1_Final(h0, &amp;ctx0);

ctx1 = ctx;
SHA1_Update(&amp;ctx1, "\nO vermin yak urea's pro-Grammy itched dt casbah fur fell...", 60);
SHA1_Final(h1, &amp;ctx1);


The resulting hashes are


h0 = b2b36d39d4431b3a57b7c30630bbbde9553b50ae
h1 = 2a0dae0548307b4ef33c0f1fedd7cbad2c32d5d8