use Carp; use Compress::Zlib; use constant SHORT_TEXT => 50; # This takes text that might or might not be encrypted and # returns it decrypted. Assumes that the text was produced # by a human or get_encrypted_code, and that decrypt will # reverse encrypt. sub get_decrypted_code { my ($text, @pass_through) = @_; (length($text) < SHORT_TEXT or text_is_normal($text)) ? $text : decrypt($text, @pass_through); } # This takes text that might or might not be encrypted and # returns it encrypted. Assumes that encrypt() does # not compress text and the text was produced by # get_encrypted_code if it is encrypted. sub get_encrypted_code { my ($text, @pass_through) = @_; if (length($text) < SHORT_TEXT) { # Need to encrypt equivalent but longer text to be # sure our test works later. return encrypt(("#\n" x SHORT_TEXT) . $text, @pass_through); } elsif (text_is_normal($text)) { return encrypt($text, @pass_through); } else { return $text; } } # Here is the test function. It takes a string and returns # whether or not it looks like it probably is # human-generated text. It is a fatal error if the text # is too short to tell. sub text_is_normal { my $text = shift; length($text) < SHORT_TEXT ? confess("Too little text for a reliable test") : (length(compress($text)) < 0.8*length($text)); }