Which is preferable: sha1_file (f) or sha1 (file_get_contents (f))?

I want to create a hash of a file with a size of at least 5 MB and can expand to 1-2 GB. Now there is a tough choice between the two methods, although they work the same way.

Method 1: sha1_file($file)
Method 2: sha1(file_get_contents($file))

I tried with 10 MB, but there is not much difference in performance. But with a higher data scale. What is the best way to go?

+4
source share
1 answer

Use the highest level form if there is no good reason.

sha1_file. sha1_file - , . "" , / 1: - .

, 5 -2 ( ) / file_get_contents, . / .


1 sha1_file github. , , :

PHP_FUNCTION(sha1_file)
{       
    stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
    PHP_SHA1Init(&context);    
    while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
        PHP_SHA1Update(&context, buf, n);
    }    
    PHP_SHA1Final(digest, &context);    
    php_stream_close(stream);
}

, . .

+7