1 package MogileFS
::Checksum
;
4 use overload
'""' => \
&as_string
;
7 "MD5" => { type
=> 1, bytelen
=> 128 / 8 },
8 "SHA-1" => { type
=> 2, bytelen
=> 160 / 8 },
10 # see POD for rationale below
11 # "SHA-224" => { type => 3, bytelen => 224 / 8 },
12 # "SHA-256" => { type => 4, bytelen => 256 / 8 },
13 # "SHA-384" => { type => 5, bytelen => 384 / 8 },
14 # "SHA-512" => { type => 6, bytelen => 512 / 8 },
17 our %NAME2TYPE = map { $_ => $TYPE{$_}->{type
} } keys(%TYPE);
18 our %TYPE2NAME = map { $NAME2TYPE{$_} => $_ } keys(%NAME2TYPE);
21 my ($class, $alg) = @_;
23 defined($alg) && defined($TYPE{$alg});
27 my ($class, $row) = @_;
30 checksum
=> $row->{checksum
},
31 hashtype
=> $row->{hashtype
}
37 # $string = "MD5:d41d8cd98f00b204e9800998ecf8427e"
39 my ($class, $fidid, $string) = @_;
40 $string =~ /\A([\w-]+):([a-fA-F0-9]{32,128})\z/ or
41 die "invalid checksum string";
44 my $ref = $TYPE{$hashname} or
45 die "invalid checksum name ($hashname) from $string";
46 my $checksum = pack("H*", $hexdigest);
47 my $len = length($checksum);
48 $len == $ref->{bytelen
} or
49 die "invalid checksum length=$len (expected $ref->{bytelen})";
53 checksum
=> $checksum,
54 hashtype
=> $NAME2TYPE{$hashname},
60 my $type = $self->{hashtype
};
61 my $name = $TYPE2NAME{$type} or die "hashtype=$type unknown";
68 my $sto = Mgd
::get_store
();
70 $sto->set_checksum($self->{fidid
}, $self->{hashtype
}, $self->{checksum
});
74 my ($self, $dmid, $classid) = @_;
75 my $class = eval { Mgd
::class_factory
()->get_by_id($dmid, $classid) };
77 # $class may be undef as it could've been deleted between
78 # create_open and create_close, we've never verified this before...
79 # class->{hashtype} is also undef, as we allow create_close callers
80 # to specify a hash regardless of class.
81 if ($class && defined($class->{hashtype
}) && $self->{hashtype
} eq $class->{hashtype
}) {
89 unpack("H*", $self->{checksum
});
94 my $name = $self->hashname;
95 my $hexdigest = $self->hexdigest;
97 "Checksum[f=$self->{fidid};$name=$hexdigest]"
103 $self->hashname . ':' . $self->hexdigest;
112 MogileFS::Checksum - Checksums handling for MogileFS
116 MogileFS supports optional MD5 checksums. Checksums can be stored
117 in the database on a per-class basis or they can be enabled globally
120 Enabling checksums will greatly increase the time and I/O required for
121 fsck as all files on all devices to be checksummed need to be reread.
123 Fsck and replication will use significantly more network bandwidth if
124 the mogstored is not used or if mogstored_stream_port is
125 not configured correctly. Using Perlbal to 1.80 or later for HTTP
126 will also speed up checksum verification during replication using
127 the Content-MD5 HTTP header.
131 While we can easily enable some or all of the SHA family of hash
132 algorithms, they all perform worse than MD5. Checksums are intended to
133 detect unintentional corruption due to hardware/software errors, not
134 malicious data replacement. Since MogileFS does not implement any
135 security/authorization on its own to protect against malicious use, the
136 use of checksums in MogileFS to protect against malicious data
137 replacement is misguided.
139 If you have a use case which requires a stronger hash algorithm,
140 please speak up on the mailing list at L<mogile@googlegroups.com>.