Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
borisz,
This was the approach I was working on when I saw your post. Can you tell me what I am doing wrong?
#!/usr/bin/perl use strict; use warnings; use Digest::MD5; use Math::BaseCalc; my $md5_b64 = Digest::MD5->new; $md5_b64->add( 'foo bar' ); my $md5_hex = Digest::MD5->new; $md5_hex->add( 'foo bar' ); my $b64_digest = $md5_b64->b64digest; my $hex_digest = $md5_hex->hexdigest; my $base_64 = new Math::BaseCalc ( digits => [ 'A' .. 'Z', 'a' .. 'z', 0 .. 9, '+' , '/' ] ); my $base_16 = new Math::BaseCalc ( digits => [ 0 .. 9, 'a' .. 'f' ] ); my $b64_string = $base_16->to_base( $base_64->from_base( $b64_digest + ) ); print "Base 64 : $b64_digest\n"; print "Hex : $hex_digest\n\n"; __END__ Base 64 : MntvB0NYESObxH4VRDUycw Hex : 327b6f07435811239bc47e1544353273 Converted : 327b6f074358120000000000000000000
It doesn't seem to work correctly? L~R

Update: After talking with tye in the CB, he seems to think that Math::BaseCalc expects the number being converted to fit into a double as well as an endian problem. He suggested using Math::Fleximal instead and I stumbled on to converting from b64 -> b16 to correct for the endian problem encountered going the other way.

#!/usr/bin/perl use strict; use warnings; use Digest::MD5; use Math::Fleximal; my $md5_b64 = Digest::MD5->new; $md5_b64->add( 'foo bar' ); my $md5_hex = Digest::MD5->new; $md5_hex->add( 'foo bar' ); my $b64_digest = $md5_b64->b64digest; my $hex_digest = $md5_hex->hexdigest; my $b16_string = Math::Fleximal->new ( $b64_digest , [ 'A'..'Z', 'a'..'z', '0'..'9', '+', '/'] )->change_flex ( [ 0..9, 'a'..'f' ] )->to_str() ; print "Base 64 : $b64_digest\n"; print "Hex : $hex_digest\n\n"; print "Converted : ", substr($b16_string, 0, 32), "\n"; __END__ Base 64 : MntvB0NYESObxH4VRDUycw Hex : 327b6f07435811239bc47e1544353273 Converted : 327b6f07435811239bc47e1544353273
You probably want to look into the endian problem further instead of just relying on it working from b64 -> b16 though.

In reply to Re: Re: base64 and hex question by Limbic~Region
in thread base64 and hex question by zakzebrowski

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2020-08-13 20:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (75 votes). Check out past polls.

      Notices?