Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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):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 asmy $user_id_h = unpack("H*", $user_id); print(dump($user_id_h));
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.unpack("l>", 1);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Converting integer to bytes
by choroba (Cardinal) on Jan 07, 2021 at 11:36 UTC | |
|
Re: Converting integer to bytes
by shmem (Chancellor) on Jan 07, 2021 at 11:35 UTC | |
|
Re: Converting integer to bytes
by realflash (Acolyte) on Jan 07, 2021 at 11:11 UTC | |
by Marshall (Canon) on Jan 09, 2021 at 16:57 UTC | |
|
Re: Converting integer to bytes
by realflash (Acolyte) on Jan 11, 2021 at 09:35 UTC |