perl - 如何创建数据结构的可重复签名?

标签 perl data-structures signature

我有一种情况,我想创建一个数据结构的签名:

my $signature = ds_to_sig(
  { foo   => 'bar',
    baz   => 'bundy',
    boing => undef,
    number => 1_234_567,
  }
);

目标应该是,如果数据结构发生变化,那么签名也应该发生变化。

有没有一种既定的方法来做到这一点?

最佳答案

最好的方法是使用像 Storable 这样的深层结构序列化系统。 .具有相同数据的两个结构将产生相同的 Storable 输出 blob,因此可以比较它们。

#!/usr/bin/perl

use strict;
use warnings;

use Storable ('freeze');

$Storable::canonical = 1;

my $one = { foo => 42, bar => [ 1, 2, 3 ] };
my $two = { foo => 42, bar => [ 1, 2, 3 ] };

my $one_s = freeze $one;
my $two_s = freeze $two;

print "match\n" if $one_s eq $two_s;

...并证明逆:
$one = [ 4, 5, 6 ];
$one_s = freeze $one;

print "no match" if $one_s ne $two_s;

关于perl - 如何创建数据结构的可重复签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/218531/

相关文章:

c - SWIG typedef - 将数组返回给 Perl

Perl 的自动分割功能与就地编辑

performance - 更快地构建 trie

python - 优化嵌套 for 循环的运行时间

c++ - 在 C++ 中设置数据结构,每个类只包含一个外观

algorithm - 我应该如何格式化数字签名?

c++ - boost 函数签名

关于 "undefined subroutine"的 Perl 模块错误

Ruby - 币安 API : Signature is not signing correctly

java - 在不同语言语法之间转换正则表达式的工具?