utf8在dancer中打开,但在脚本中没有。[英] utf8 on in dancer but not script

本文是小编为大家收集整理的关于utf8在dancer中打开,但在脚本中没有。的处理方法,想解了utf8在dancer中打开,但在脚本中没有。的问题怎么解决?utf8在dancer中打开,但在脚本中没有。问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

这是对我之前关于显示 unicode 字符串差异的问题的跟进.事实证明,这些字符串看起来是相同的,但是其中一个字符串的 UTF8 标志是打开的.

SV = PVMG(0x4cca750) at 0x4b3fc90
 REFCNT = 1
 FLAGS = (PADMY,POK,pPOK,UTF8)
 IV = 0
 NV = 0
 PV = 0x1eda410 "flurbe"\0 [UTF8 "flurbe"]
 CUR = 6
 LEN = 16

SV = PV(0xf28090) at 0xf4b6a0
 REFCNT = 1
 FLAGS = (PADMY,POK,pPOK)
 PV = 0xf37b90 "flurbe"\0
 CUR = 6
 LEN = 16

当我加密字符串时,这似乎会对生成的 sha512 哈希值产生影响.据我所知,Dancer 是导致第一个结果具有 utf8 的原因,我的另一个脚本只是一个命令行脚本,没有使用 dancer,我怎么能强制它以相同的方式运行?

解决方案

(这更像是一个评论而不是一个答案,但它太大了.)

我刚刚运行了这个程序:

#!/usr/bin/perl -w

use warnings;
use strict;

use Devel::Peek ();
use Digest::SHA ();

my $x = 'flurbe';

Devel::Peek::Dump $x;

print Digest::SHA::sha512_hex($x), "\n\n";

utf8::upgrade $x;

Devel::Peek::Dump $x;

print Digest::SHA::sha512_hex($x), "\n";

__END__

它给出了这个输出:

SV = PV(0x10441040) at 0x10491638
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  PV = 0x10449ca0 "flurbe"\0
  CUR = 6
  LEN = 8
1cd2e71e55653caeb6c9bffa47a66ff1c9b526bbb732dcff28412090601e9b5e34d36be6a0267527347cd94039b383d4bc45653d786d1041debe7faa0716bdf1

SV = PV(0x10441040) at 0x10491638
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK,UTF8)
  PV = 0x10449ca0 "flurbe"\0 [UTF8 "flurbe"]
  CUR = 6
  LEN = 8
1cd2e71e55653caeb6c9bffa47a66ff1c9b526bbb732dcff28412090601e9b5e34d36be6a0267527347cd94039b383d4bc45653d786d1041debe7faa0716bdf1

如您所见,Devel::Peek::Dump 正确识别字符串已升级为 UTF-8,但这不会影响 Digest::SHA 计算的 SHA-512 哈希.

编辑添加:在上面的评论中,您提到您的"哈希是随机加盐的".这些盐可以包含 ASCII 范围之外的字节吗?如果是这样,与 UTF-8 升级的字符串连接会影响它们的内容.我刚刚运行了这个修改后的程序:

#!/usr/bin/perl -w

use warnings;
use strict;

use Devel::Peek ();
use Digest::SHA ();

my $x = 'flurbe';
my $y = "\xA0";      # a single byte, hex 00A0
my $z = "\xC2\xA0";  # UTF-8 representation of U+00A0, as a byte-string

Devel::Peek::Dump "$x$y";
print Digest::SHA::sha512_hex("$x$y"), "\n\n";

Devel::Peek::Dump "$x$z";
print Digest::SHA::sha512_hex("$x$z"), "\n\n";

utf8::upgrade $x;

Devel::Peek::Dump "$x$y";

print Digest::SHA::sha512_hex("$x$y"), "\n";

__END__

它给出了这个输出:

SV = PV(0x104410e8) at 0x104d68d8
  REFCNT = 1
  FLAGS = (PADTMP,POK,pPOK)
  PV = 0x10449ca0 "flurbe\240"\0
  CUR = 7
  LEN = 8
1901f989ed76143697ecc6683fd03ec793bc126d51cdbee0a72241933136c144f2e602828abddc7e4843df5542a099be92313fa5874d1d2dc54ecdd1ff308c5e

SV = PV(0x104d80b8) at 0x104ec098
  REFCNT = 1
  FLAGS = (PADTMP,POK,pPOK)
  PV = 0x10489170 "flurbe\302\240"\0
  CUR = 8
  LEN = 12
072f7b54c80fa8062ca1d17727a88c9ff4815f83c1166471331c6398b9140a06812eff341c98453f4c51356926dbe9694cbcbebfe4cda7e77cf68008ab838c6d

SV = PV(0x104d80a8) at 0x104f0f98
  REFCNT = 1
  FLAGS = (PADTMP,POK,pPOK,UTF8)
  PV = 0x104896c8 "flurbe\302\240"\0 [UTF8 "flurbe\x{a0}"]
  CUR = 8
  LEN = 12
072f7b54c80fa8062ca1d17727a88c9ff4815f83c1166471331c6398b9140a06812eff341c98453f4c51356926dbe9694cbcbebfe4cda7e77cf68008ab838c6d

如您所见,"$x$y" 的 SHA-512 哈希取决于 $x 是否经过 UTF-8 升级.带有 UTF-8 升级的 $x 的 "$x$y" 提供与带有 非-UTF-8 升级的 $x 的 "$x$z" 相同的 SHA-512 散列.这是因为 SHA-512 对字节而不是字符进行操作,并且 UTF-8 升级字符串与字节字符串的连接会导致字节字符串进行 UTF-8 升级.

本文地址:https://www.itbaoku.cn/post/1793881.html