in reply to Re: Rewriting a C# Encryption Function
in thread Rewriting a C# Encryption Function

The hash has to match with the old. IE if the data submitted is 1234 then the encrypted data/base64 needs to equal E59pyTwEJJao6VjsWTBmLGzMr78. How it gets there isn't as important.

The perl function doesn't have to convert the data to unicode and then hash it, if it is simpler it skip certain parts than I'm more than happy to do that. Its the end result that has to match.
  • Comment on Re^2: Rewriting a C# Encryption Function

Replies are listed 'Best First'.
Re^3: Rewriting a C# Encryption Function
by jethro (Monsignor) on Aug 22, 2008 at 21:55 UTC

    Sadly you can skip unicode madness only if you know that the strings you have to convert are all from a very limited set of chars (a-z,A-Z,0-9 and a few special chars). Then using just the pack method would be enough.

    But the link I provided seems to have the solution on a platter:

    my $octets= encode("utf16", '1234'); $octets= substr($octets,2); for my $i (0..(length($octets)/2-1)) { my $n= substr($octets,$i*2,1); substr( $octets,$i*2,1)= substr($octets,$i*2+1,1); substr($octets,$i*2+1,1)=$n; } print sha1_base64($octets);

    prints out 'E59py...'. The really ugly for loop swaps the bytes of one 16 bit value since the encoding was swapped on my machine. If you don't get the same result, drop the for loop and check again.

      First off, you shouldn't forget to mention use Encode; -- believe it or not, some folks might still not be aware that "encode" is not a perl built-in function, and/or might not know what module it comes from. (Update: but on closer inspection, I see the OP does know about Encode, so I apologize for the nit-pick.)

      Second, you really should not use "utf16" in this case -- better to specify what byte order you really want for the job, because that way, perl won't automatically add the BOM at the start of the string, and you won't have to byte-swap later:

      $ perl -MEncode -e '$o=encode("utf16","1234"); print $o' | xxd -g1 0000000: fe ff 00 31 00 32 00 33 00 34 ...1.2.3.4 $ perl -MEncode -e '$o=encode("utf16le","1234"); print $o' | xxd -g1 0000000: 31 00 32 00 33 00 34 00 1.2.3.4. $ perl -MEncode -e '$o=encode("utf16be","1234"); print $o' | xxd -g1 0000000: 00 31 00 32 00 33 00 34 .1.2.3.4