innmind/hash

文件增量哈希

1.6.0 2024-07-25 11:05 UTC

This package is auto-updated.

Last update: 2024-08-25 11:21:26 UTC


README

Build Status codecov Type Coverage

此组件允许增量计算文件(或任何字符串序列)的哈希值。

安装

composer require innmind/hash

用法

use Innmind\OperatingSystem\Factory;
use Innmind\Url\Path;
use Innmind\Hash\{
    Hash,
    Value,
};
use Innmind\Immutable\Set;

$hashes = Factory::build()
    ->filesystem()
    ->mount(Path::of('some-folder/'))
    ->all()
    ->map(Hash::sha512->ofFile(...));
$hashes; // Set<Value>

由于计算不依赖于文件系统,因此可以在不在文件系统中的内容上调用,或者无法放入内存中的内容上调用。

示例

use Innmind\OperatingSystem\Factory;
use Innmind\Http\{
    Message\Request\Request,
    Message\Method,
    ProtocolVersion,
};
use Innmind\Url\Url;
use Innmind\Server\Control\Server\Command;
use Innmind\Hash\{
    Hash,
    Value,
};

$os = Factory::build();
$os
    ->remote()
    ->http()(new Request(
        Url::of('https://github.com'),
        Method::get,
        ProtocolVersion::v20,
    ))
    ->map(static fn($success) => $success->response()->content())
    ->map(Hash::sha512->ofContent(...))
    ->match(
        static fn($value) => $value, // Value
        static fn() => null, // http call failed
    );

$output = $os
    ->control()
    ->processes()
    ->execute(
        Command::foreground('git')
            ->withOption('version'),
    )
    ->output()
    ->chunks()
    ->map(static fn($chunk) => $chunk[0]); // to only keep the chunk data

Hash::sha512->ofSequence($output); // Value