Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Compact MD5 representation

by YoungPups (Beadle)
on Mar 20, 2001 at 22:03 UTC ( [id://65763]=note: print w/replies, xml ) Need Help??


in reply to Compact MD5 representation

I got bored, and tried to write my own base converter (without having first checked CPAN where I would have found Math::BaseCalc which would do what you want just fine, I think). It runs into problems with large numbers (The md5 value listed in your post, for example).

But if anyone's interested in tinkering, I'll post what I came up with... I think I could fix it by using Math::BigInt, but haven't tried just yet.

sub convert_base { my($input_string,$from_base,$to_base) = @_; my($working_number,$current_digit,$i); my($quotient,converted_number); # # Convert input string to dec. # for ( $i = 0; $i < length($input_string); $i++ ) { $current_digit = uc(substr($input_string,$i,1)); if ( $current_digit !~ m/[0-9]/ ) { $current_digit = ord($current_digit) - ord("A") + 10; } $working_number += $current_digit * ($from_base ** (length($input_string) - $i - 1)); } # Figure out dec value of this digit. # # Convert int to new base. # while ( $quotient = int($working_number / $to_base) ) { $current_digit = $working_number % $to_base; if ( $current_digit > 9 ) { $current_digit = chr($current_digit - 10 + ord("A")); } $converted_number = $current_digit . $converted_number; $working_number = $quotient; } # # We've dropped out because there's only a bit of remainder left. # $current_digit = $working_number; if ( $current_digit > 9 ) { $current_digit = chr($current_digit - 10 + ord("A")); } $converted_number = $current_digit . $converted_number; return $converted_number; }

Replies are listed 'Best First'.
Re: Re: Compact MD5 representation
by paulbort (Hermit) on Mar 20, 2001 at 22:32 UTC
    You could use Math::BaseCalc (as suggested above) on the the first half and last half of the MD5, and then concatenate them. If you convert to base32 or base64, you wouldn't waste any bits. (And if you convert to base62 0-9a-zA-Z, you waste a couple bits, but it's portable just about everywhere.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://65763]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-03-19 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found