1 package MogileFS
::Checksum
;
4 use overload
'""' => \
&as_string
;
7 "MD5" => { type
=> 1, bytelen
=> 128 / 8 },
8 "SHA-1" => { type
=> 2, bytelen
=> 160 / 8 },
9 "SHA-224" => { type
=> 3, bytelen
=> 224 / 8 },
10 "SHA-256" => { type
=> 4, bytelen
=> 256 / 8 },
11 "SHA-384" => { type
=> 5, bytelen
=> 384 / 8 },
12 "SHA-512" => { type
=> 6, bytelen
=> 512 / 8 },
15 our %NAME2TYPE = map { $_ => $TYPE{$_}->{type
} } keys(%TYPE);
16 our %TYPE2NAME = map { $NAME2TYPE{$_} => $_ } keys(%NAME2TYPE);
19 my ($class, $row) = @_;
22 checksum
=> $row->{checksum
},
23 checksumtype
=> $row->{checksumtype
}
29 # $string = "MD5:d41d8cd98f00b204e9800998ecf8427e"
31 my ($class, $fidid, $string) = @_;
32 $string =~ /\A([\w-]+):([a-fA-F0-9]{32,128})\z/ or
33 die "invalid checksum string";
34 my $checksumname = $1;
36 my $ref = $TYPE{$checksumname} or
37 die "invalid checksum name ($checksumname) from $string";
38 my $checksum = pack("H*", $hexdigest);
39 my $len = length($checksum);
40 $len == $ref->{bytelen
} or
41 die "invalid checksum length=$len (expected $ref->{bytelen})";
45 checksum
=> $checksum,
46 checksumtype
=> $NAME2TYPE{$checksumname},
52 my $type = $self->{checksumtype
};
53 my $name = $TYPE2NAME{$type} or die "checksumtype=$type unknown";
60 my $sto = Mgd
::get_store
();
62 $sto->set_checksum($self->{fidid
}, $self->{checksumtype
}, $self->{checksum
});
66 my ($self, $dmid, $classid) = @_;
67 my $class = eval { Mgd
::class_factory
()->get_by_id($dmid, $classid) };
69 # $class may be undef as it could've been deleted between
70 # create_open and create_close, we've never verified this before...
71 if ($class && $self->{checksumtype
} eq $class->{checksumtype
}) {
79 unpack("H*", $self->{checksum
});
84 my $name = $self->checksumname;
85 my $hexdigest = $self->hexdigest;
87 "Checksum[f=$self->{fidid};$name=$hexdigest]"
93 $self->checksumname . ':' . $self->hexdigest;