Oihana PHP

readChecksumLine.php

Table of Contents

Functions

readChecksumLine()  : string|null
Reads the checksum a checksum file records for one file, or null.

Functions

readChecksumLine()

Reads the checksum a checksum file records for one file, or null.

readChecksumLine(string $content, string $filename[, string $algorithm = 'sha256' ]) : string|null

Read as sha256sum writes it, see checksumLine(): the hash, then two spaces — or a space and a star, its binary mode — then the file name. The hash must have the length of the algorithm's (64 characters for sha256, 32 for md5). Only a line naming this file counts: a checksum file copied beside the wrong file must not vouch for it. A file listing several files is read line by line, and the first line naming this one is taken.

Parameters
$content : string

What the checksum file holds.

$filename : string

The name of the file, as the line writes it.

$algorithm : string = 'sha256'

A hashing algorithm supported by hash_algos() (default: 'sha256').

Tags
throws
FileException

If the algorithm is unsupported.

example
use function oihana\files\fileChecksum;
use function oihana\files\readChecksumLine;

$recorded = readChecksumLine( file_get_contents( '/data/report.pdf.sha256' ) , 'report.pdf' ) ;

if ( $recorded === null || $recorded !== fileChecksum( '/data/report.pdf' ) )
{
    // no checksum for this file, or the file changed since it was written
}
author

Marc Alcaraz (ekameleon)

since
1.7.0
Return values
string|null —

The hash, lowercase; null when no line names the file with a well-formed one.

On this page

Search results