I'm trying to implement the authentication described here.

In step 7, the integer/string (it's not clear) that represents the user ID is 1. That's converted into the bytes represented by 0x00000000 00000001 (8 bytes). I'm trying to write the Perl that would do that conversion, so that those bytes can then be passed to Digest::SHA as part of the message. The doc describes this ID as "8-byte (big-endian) user identifier". I concluded the Encode module isn't right as this should be perl internal string -> binary, without a character encoding in-between. I thought that unpack is the right way to go, but I'm not having much luck. I thought maybe I can have it represented as hex (something that Digest::SHA accepts as a data format for the message):
my $user_id_h = unpack("H*", $user_id); print(dump($user_id_h));
results in 31. OK, that's ASCII code for the character "1". But why is unpack treating it as a string? Probably because Perl doesn't differentiate internally between strings and numbers. So can I tell Perl it's a number? Treating it as an integer, such as
unpack("l>", 1);
results in undefined. I feel like there are probably some concepts I am missing here. Am I on the right lines with unpacl? How do I get from a perl number hard-coded or loaded from a config file to the big-endian binary octets that represent that number to pass to Digest::SHA? I am on (little-endian) Intel. In this Java implementation at line 385 the User ID is treated as a java Long, and then the ByteBuffer method putLong() is used to just do the right thing, whatever that is.

In reply to Converting integer to bytes by Anonymous Monk

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, details, 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, summary, 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.