简体   繁体   English

有没有办法创建可逆的不透明 UUID?

[英]Is there a way to create a reversible opaque UUID?

Request要求

We need a php-only system to map a filepath/filename.ext to a UUID in a way that can be reversible.我们需要一个 php-only 系统以可逆的方式将文件路径/文件名.ext 映射到 UUID。

Example例子

For example:例如:

maps/shop1.jpg ->  342abfec-31fdfg-23131

The user can access this map only from用户只能从

loader.php?token=342abfec-31fdfg-23131

The loader.php must be able to 'reverse' the UUID so the script can send maps/show1.jpg . loader.php必须能够“反​​转”UUID,以便脚本可以发送maps/show1.jpg

The goal目标

Main goal in this phase is not to secure content access.此阶段的主要目标不是保护内容访问。

Our goal for now is only to add an "opacity" level to avoid that an user can open for example shop2.jpg simply changing the URL.我们现在的目标只是添加一个“不透明度”级别,以避免用户可以打开例如shop2.jpg只需更改 URL。

The problem问题

We cannot, for now, save UUID<-->file mapping into a DB or similar.目前,我们无法将 UUID<--> 文件映射保存到数据库或类似文件中。

So I've the request to create 'on the fly' the UUID corresponding to a filepath/filename.txt, and this UUID must be reversible so a PHP script can 'decode' it and serve the corresponding file.因此,我请求“即时”创建与文件路径/文件名.txt 相对应的 UUID,并且此 UUID必须是可逆的,以便 PHP 脚本可以“解码”它并提供相应的文件。

Question

Does a reversible string to UUID algorithm already exist? UUID 算法的可逆字符串是否已经存在?

It sounds like you want it to be hard to guess URLs or spot patterns in them.听起来您希望很难猜测 URL 或发现其中的模式。 The suggestion of base64 encoding in another answer is a good one, but similar inputs will still lead to similar outputs, so it may not be ideal:另一个答案中base64编码的建议是一个很好的建议,但是类似的输入仍然会导致类似的输出,因此它可能并不理想:

maps/shop1.jpg: bWFwcy9zaG9wMS5qcGc
maps/shop2.jpg: bWFwcy9zaG9wMi5qcGc
maps/shop3.jpg: bWFwcy9zaG9wMy5qcGc
maps/shop4.jpg: bWFwcy9zaG9wNC5qcGc
maps/shop5.jpg: bWFwcy9zaG9wNS5qcGc

To avoid this, you can XOR the string with a random salt (or "nonce"), and include the salt in the URL to decode it, eg:为避免这种情况,您可以使用随机盐(或“nonce”)对字符串进行异或,并将盐包含在 URL 中以对其进行解码,例如:

function base64_url_encode($input)
{
    return strtr(base64_encode($input), '+/=', '._-');
}

function base64_url_decode($input)
{
    return base64_decode(strtr($input, '._-', '+/='));
}

function obfuscate($input)
{
    $salt = random_bytes(strlen($input));
    // Make sure our separator doesn't appear in the salt, or explode() will split at the wrong place
    str_replace('|', '-', $salt);
    $xor = $input ^ $salt;
    return base64_url_encode("$salt|$xor");
}

function deobfuscate($input)
{
    [$salt, $xor] = explode('|', base64_url_decode($input), 2);
    return $salt ^ $xor;
}

Which gives strings like this:这给出了这样的字符串:

maps/shop1.jpg: NubGQVCitxcciGC8wht8W4e2Mn_R33hsuU7Wsnw-
maps/shop2.jpg: ePr0T12ft29NwpNMslN8FZuEPHLs3wA98L0mwjQ-
maps/shop3.jpg: 7stRD6lwniLozbmY_fJ8g6ohfIYD9k2Y_pfyjZU-
maps/shop4.jpg: 1ArPov5EmGqyikHyN2l8uWu_0dE38AXCvm.YRw4-
maps/shop5.jpg: tQN2RHTzN.LCEHu1URJ82GIGN1uAX42yJVXfIXU-

(A variation of this would be to use the MD5 or SHA1 hash of the string as the salt, so that each string will always have the same obfuscated form, but still be hard to guess.) (一种变体是使用字符串的 MD5 或 SHA1 哈希值作为盐,这样每个字符串将始终具有相同的混淆形式,但仍然难以猜测。)

You could even XOR a second fixed string that's not transmitted (a "key"), making this into a very simple encryption scheme.您甚至可以对未传输的第二个固定字符串(“密钥”)进行异或,使其成为一个非常简单的加密方案。 But if you want security, you're better off just using a real encryption function like sodium_crypto_secretbox .但是如果你想要安全,你最好只使用一个真正的加密函数,比如sodium_crypto_secretbox

Well what you're looking for is a an encode/decode algorithm.那么你正在寻找的是一种编码/解码算法。 There's hex and base64 encodings which are usually used for the above but if you want really something unique you have to create your own.有十六进制和 base64 编码通常用于上述情况,但如果你真的想要一些独特的东西,你必须创建自己的。

Here's a url safe base 64 encoding/decoding example:这是一个 url 安全的 base 64 编码/解码示例:

/**
 * Encode a string to URL Safe base 64
 */
function base64_url_encode($input)
{
    $result = strtr(base64_encode($input), '+/=', '._-');
    //remove padding
    $start = strlen($result) - 2;
    $pos = strpos($result, '-', $start);
    if ($pos !== false) {
        $result = substr_replace($result, "", $pos);
    }
    return $result;
}

/**
 * Decode a string from URL Safe base 64
 */

function base64_url_decode($input)
{
    //add back padding
    $input = str_pad($input, ceil(strlen($input)/4)*4, '-');
    return base64_decode(strtr($input, '._-', '+/='));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM