【问题标题】:Generate hash using PHASH with PHP使用 PHASH 和 PHP 生成哈希
【发布时间】:2018-06-11 10:53:42
【问题描述】:

如何从PHP 中的字符串生成PHASH 值?

我继承了一个 ASP 代码库,它使用 PHASHstrings不是图像的路径)。根据研究,PHASH 用于图像。

我目前正在使用 PHP 重写这部分代码库,并且有几个库看起来很有用:

然而,它们都需要图像的路径。我试过jenssegers/imagehash,当我传递一个随机字符串时会引发异常。

下面的代码说明了PHASH 当前在旧代码库中的使用方式:

sLoginPassword = RequestValue("Password")
SQLVal(PHASH(sLoginPassword))

更新

PHASH 是代码库中的自定义函数,由于大小写混合(PHash vs PHASH),我最初未能找到它。

幸运的是,我找到了以下SO answer,它是用C# 编写的。感谢@Lathejockey81 提供答案,我已将其转换为下面的PHP(作为答案)。

【问题讨论】:

  • 幸运的是我发现了这个 - stackoverflow.com/questions/18582667/… - 并且,在深入挖掘之后,它看起来像是一个自定义函数,尽管该函数被引用为 PHash,而不是 PHASH
  • 您想对密码进行哈希处理?为什么不使用像 sha1() 这样的内置哈希
  • 不,我想知道如何在 PHP 中对上面链接的 PHash 函数进行逆向工程 :) - 转换代码后我会尽快提供答案。

标签: php asp-classic phash


【解决方案1】:

自定义PHASH函数由SO answer转换而来:

function PHASH($string)
{
    $value = trim(strtoupper($string));

    $dAccumulator = 0;
    $asciiBytes = [];

    for($i = 0; $i < strlen($value); $i++) {
        $asciiBytes[] = ord($value[$i]);
    }

    for($i = 0; $i < count($asciiBytes); $i++) {
        if(($i & 1) == 1) {
            $dAccumulator = cos($dAccumulator + (float) $asciiBytes[$i]);
        } else {
            $dAccumulator = sin($dAccumulator + (float) $asciiBytes[$i]);
        }
    }
    $dAccumulator = $dAccumulator * pow(10, 9);

    return round($dAccumulator);
}

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多