Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help with "pack" for perl and php
    text
    copied!<p>I've the task to convert a crypt function someone made in perl into php code. Everything works okay except this:</p> <p>Perl:</p> <pre><code>$wert = Encode::encode( "utf8", $wert ); $len=length $wert; $pad = ($len % 16)?"0".chr(16 - ($len % 16)):"10"; $fuell = pack( "H*", $pad x (16 - $len % 16)); </code></pre> <p>PHP:</p> <pre><code>$wert = utf8_encode($wert); $len = mb_strlen($wert); $pad = ( $len%16 ) ? '0'.chr(16 - ($len%16)) : '10'; $fuell = pack("H*", str_repeat($pad, (16 - $len % 16))); </code></pre> <p>The php version works okay for some strings. But when I have something like '2010-01-01T00:00:00.000' the perl version works without any error and the php version prints "PHP Warning: pack(): Type H: illegal hex digit".</p> <p>I'm very grateful if someone can spot the error in the php version.</p> <p><strong>Edit:</strong></p> <p>This is the complete function I've to convert into php. It was made by a programmer of a company which doesn't work for us anymore so I can't really tell what the original intention was.</p> <pre><code>sub crypt { my $self = shift; my ($wert,$pw)= @_; $wert = Encode::encode( "utf8", $wert ); $pw = Encode::encode( "utf8", $pw ); $len=length $wert; $pad = ($len % 16)?"0".chr(16 - ($len % 16)):"10"; $fuell = pack( "H*", $pad x (16 - $len % 16)); $wert=$wert.$fuell; $lenpw=length $pw; $fuell = ($lenpw % 16)? pack ("H*", "00" x (16 - $lenpw % 16)):""; $pw=$pw.$fuell; $cipher = new Crypt::Rijndael $pw, Crypt::Rijndael::MODE_CBC; $cipher-&gt;set_iv($pw); $crypted = encode_base64($cipher-&gt;encrypt($wert),""); return $crypted; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload