Perl SHA-256 algorithm generates output different from the equivalent Java
Clash Royale CLAN TAG#URR8PPP
Perl SHA-256 algorithm generates output different from the equivalent Java
I have the following Java code:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class MyClass
public static void main(String args)
byte salt =
84, 65, -51, 83, -4, -17, -32, 61,
-26, 33, -106, -81, -14, 70, -30, 59,
41, -19, -1, 19, -104, -100, -31, 31,
57, -6, -115, -99, 0, 38, -123, -11
;
byte password = 100, 112, 107, 57, 52, 110, 50 ;
try
MessageDigest messageDigest;
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.reset();
messageDigest.update(salt);
byte hash = messageDigest.digest(password);
for (int i = 1; i < 1000; i++)
messageDigest.reset();
hash = messageDigest.digest(hash);
StringBuffer stringBuffer = new StringBuffer(hash.length * 2);
for (int i = 0; i < hash.length; i++)
stringBuffer.append(Character.forDigit(hash[i] >> 4 & 0xF, 16));
stringBuffer.append(Character.forDigit(hash[i] & 0xF, 16));
System.out.println("HashArray: " + Arrays.toString(hash));
System.out.println("HashString: " + stringBuffer.toString());
catch (NoSuchAlgorithmException e)
e.printStackTrace();
The output is
HashArray: [-127, -38, -46, -2, 51, -2, -83, -42, 3, 83, -78, -72, -115, -28, 108, 58, -76, -35, -13, 33, 63, -96, 104, 101, -76, 23, 102, -28, -60, 4, 85, 82]
and
HashString: 81dad2fe33feadd60353b2b88de46c3ab4ddf3213fa06865b41766e4c4045552
I tried to translate the Java code to Perl, but I have a understanding problem. In Java, I use two arrays to generate the hash. If I use the same arrays in Perl, I don't get the same result.
Here is my Perl code:
use strict;
use warnings;
use Digest::SHA 'sha256';
my @salt = (
"84", "65", -"51", "83", "-4", "-17", "-32", "61",
"-26", "33", "-106", "-81", "-14", "70", "-30", "59",
"41", "-19", "-1", "19", "-104", "-100", "-31", "31",
"57", "-6", "-115", "-99", "0", "38", "-123", "-11"
);
my @password = ( "100", "112", "107", "57", "52", "110", "50" );
my $alg = 256;
my $sha = Digest::SHA->new($alg);
$sha->add(@salt);
$sha->add(@password);
for ( my $i = 1; $i < 1000; $i++ )
$sha->add(@password)
my $output = $sha->hexdigest;
print "nHash: $outputn";
Output is
Hash: 3883ae8f7438cc7e8fac86d25aa5789c4434294a70ea13d5e8f547fc7a8e45c2
Can someone explain how to get the Perl code to produce the same output as the Java?
-"51"
"-51"
I don't know perl but the minus sign of the 3rd element of salt is not between the quotes
– jhamon
Aug 13 at 8:34
You're hashing completely different values (bytes versus strings of characters), and in different ways. Of course the two are going to be different.
– Shawn
Aug 13 at 8:35
It gives the same result if you remove all quotes from the numbers.
– choroba
Aug 13 at 8:35
@RobAu "-51" is correct
– WhistleWhite
Aug 13 at 9:48
1 Answer
1
Two problems:
The Java version works with bytes, the Perl version with strings of digits. Use pack to convert the strings to their byte equivalents.
In the Java version, you are applying the hash to the hash computed so far, in the Perl code, you just hash the same value again and again.
#!/usr/bin/perl
use warnings;
use strict;
use Digest::SHA 'sha256';
my @salt = (84, 65, -51, 83, -4, -17, -32, 61, -26, 33, -106, -81,
-14, 70, -30, 59, 41, -19, -1, 19, -104, -100, -31, 31,
57, -6, -115, -99, 0, 38, -123, -11);
my @password = (100, 112, 107, 57, 52, 110, 50);
my $alg = 256;
my $sha = Digest::SHA->new($alg);
$sha->add(pack 'c*', @salt);
$sha->add(pack 'c*', @password);
$sha->add($sha->digest) for 1 .. 999;
my $output = $sha->hexdigest;
print "nHash: $outputn";
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
-"51"
is that similar to"-51"
?– RobAu
Aug 13 at 8:34