view bin/ed2k @ 719:bcdf320dabf4

Rewrite the ed2k again in php Getting openssl to hash md4 is a pain.
author nanaya <me@nanaya.net>
date Sun, 10 Dec 2023 00:35:22 +0900
parents 94ee419ad047
children
line wrap: on
line source

#!/usr/bin/env php
<?php

function ed2k_hash(string $path): string
{
    // ed2k hash is an md4 hash of concatenated binary md4 hashes of 9500 KiB file chunks.
    static $bs = 9500 * 1024;
    $hash = '';
    $fp = fopen($path, 'rb');

    while (!feof($fp)) {
        $hash .= hash('md4', fread($fp, $bs), true);
    }
    fclose($fp);

    return hash('md4', $hash);
}

for ($i = 1; $i < $argc; $i++) {
    $path = $argv[$i];
    $hash = ed2k_hash($path);
    $filename = basename($path);
    $filesize = filesize($path);

    echo "ed2k://|file|{$filename}|{$filesize}|{$hash}|";
    echo PHP_EOL;
}