in reply to Re: Convert Java to Perl -> Script attached
in thread Convert Java to Perl -> Script attached

Hey Guys, Sorry about the lame reply earlier .. I had to rush off ..
PERL: UTF-8 : 11297115115119111114100 Encrypted Bytes : 1481382315991371591892351772197193603919425451170179 JAVA: UTF-8 : 11297115115119111114100 Encrypted Bytes : 91-8697-28-55-7163636-1263711108-85127126-26-113-40 Encoded: W6ph5Mm5Pz8GgiULbPgzG37mj9g=
I can't get the encrypted bytes to match. Ignore the encoded, its what it needs to be eventually.

Replies are listed 'Best First'.
Re^3: Convert Java to Perl -> Script attached
by almut (Canon) on Mar 03, 2009 at 14:48 UTC

    I'd go with moritz's solution and simply use Digest::SHA1::sha1_base64(), but just in case you want to know why your attempt didn't work, here's why:

    $sha->add(@ascii);

    should've been

    $sha->add(encode_utf8($x));

    You just need to add the (UTF-8) string itself, not a list of stringified byte values such as "112", "97", "115"...  (i.e. you did compute the digest of the string "11297115115119111114100").

    Also, in your Java code, you're printing out the digest as signed bytes. To get the same behaviour in Perl, you'd need to

    @z = unpack("c*",$sha->digest);

    which would then print out as expected (when adding spaces for better readability (print "@z\n") ):

    91 -86 97 -28 -55 -71 63 63 6 -126 37 11 108 -8 51 27 126 -26 -113 -40
Re^3: Convert Java to Perl -> Script attached
by smoky (Novice) on Mar 03, 2009 at 13:25 UTC
    BTW, here are two quick versions for running: JAVA
    import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import sun.misc.BASE64Encoder; public class enc{ public static void main(String[] args){ String plaintext = "password"; MessageDigest md = null; try { md = MessageDigest.getInstance("SHA"); } catch(NoSuchAlgorithmException e) { System.exit(1); } try { //Encoding into UTF-8 System.out.print("UTF-8 : "); for(byte b : plaintext.getBytes("UTF-8")){ System.out.print(b); } System.out.println(); md.update(plaintext.getBytes("UTF-8")); } catch(UnsupportedEncodingException e) { System.exit(1); } byte raw[] = md.digest(); System.out.print("Encrypted Bytes : "); for(byte b : raw){ System.out.print(b); } System.out.println(); String hash = (new BASE64Encoder()).encode(raw); System.out.println("Encoded: " + hash); }}
    PERL
    use Digest::SHA; use utf8; no utf8; use MIME::Base64 qw(encode_base64 decode_base64); $x = "password"; utf8::encode($x); @ascii = unpack("C*", $x); print "UTF-8 : "; foreach $val (@ascii) { print $val; } print "\n"; $sha = Digest::SHA->new; $sha->add(@ascii); @z = unpack("C*",$sha->digest); print "Encrypted Bytes : "; foreach $val (@z) { print $val; } print "\n"; #$x = "jsmith"; #utf8::encode($x); # #print "0. \t\t" . $x . "\n"; #Encrypt, into bytes #@y = $sha->digest; #print "1. \t\t" . $y . "\n"; #Encode #$z = encode_base64($y); #print "2. \t\t" . $z . "\n"; #Print #print "Needs to be: \t5yfRRkrhJDbomacm2lsvEdg4GyY=";
    name them as enc.java and enc.pl ;)