Geesu
Geesu

Reputation: 6008

Calculating SHA1 in PHP similar to CC_SHA1_Update in C

So I have an application in C that is transmitting data over the internet to a PHP page. The C implementation for my hash is:

    CC_SHA1_CTX ctx;
    CC_SHA1_Init(&ctx);
    CC_SHA1_Update(&ctx, ciph_right,20);
    CC_SHA1_Update(&ctx, _keyRight, 20);
    CC_SHA1_Final(digest, &ctx);

Where ciph_right and _keyRight are simply 2 byte arrays of length 20. How can I achieve this similar implementation for PHP so I'm getting the same result?

I don't hav the CC_SHA1_Update function in PHP, I simply have sha1(). And unfortunately I'm not 100% certain what CC_SHA1_Update actually does. I thought it simply combined the two byte arrays + took a hash of them together, but this appears to not be the case.

Anyone have any ideas?

Thanks!

Upvotes: 2

Views: 390

Answers (1)

Charles
Charles

Reputation: 51411

The sha1 function takes a complete string and derives a SHA1 hash from that complete string.

A bit of Googling suggests that the C calls to CC_SHA1_Update are just adding more data to be hashed.

You therefore have two options.

  1. Just concatenate all the data together and then call sha1 on it.
  2. Use the (standard) hash extension in streaming mode:

.

$h = hash_init('sha1');
hash_update($h, 'some data');
hash_update($h, 'some more data');
$final_hash = hash_final($h);

$final_hash should then contain fa978155004254c23f9cf42918ad372038afcaf5, which happens to be the same hash as the string 'some datasome more data'.

Upvotes: 2

Related Questions